> De: "stevenselectronicmail" <[email protected]>
> À: "mechanical-sympathy" <[email protected]>
> Envoyé: Vendredi 1 Mai 2020 17:51:47
> Objet: Re: Best way to dynamically spin Java classes?

> Huh
> I've often thought about doing similar.
> Two nits:

> 1.
> RT.java is clever, I know ClassValue was probably the way to do things but
> couldn't figure out how to do it. But it's simpler to just write to a static
> field to the class anyway.

it's not that easy if you want the field to be final too, because you don't 
control when a static block is executed, and if it is not final, users of the 
proxy API can mess with it because the API provide them a lookup to the proxy 

> One advantage of creating new classloaders over defineHiddenClass is you can
> just pass in the data as part of the classloader.

Yes, 
using the classloader is what most of the dynamic languages JRuby or Groovy do. 

If you take a look the source of MethodHandles.Lookup, internally the VM use a 
something called the classData that is taken as parameter of define(...) and 
stored in a static field by the VM (it's not a true static field because you 
can not access it using regular bytecode but it is stored in the same space). 
When it will become part of the offcial API, defineHiddenClass will becomes 
even easier to use. 

> 2.
> I wouldn't even bother with a traditional constructor but just use 
> MethodHandles
> to set the delegate field.

I want the field to be final. Final fields of Hidden Class are truly final for 
the JIT, so i get better perf if a proxy is, by example, stored in a static 
final, so has a good citizen, i'm using a constructor :) 
Also because final really means final, you can not use reflection or 
java.lang.invoke to set it after the fact. 

> Anyway very cool what defineHiddenClass will enable.

yes, all these features were available but only internally for the jdk, i think 
since jdk 7, finding a safe way to expose them take times. 

regards, 
Rémi 

> On Sunday, April 26, 2020 at 10:31:20 AM UTC-7, Remi Forax wrote:
>> Shameless plug,
>> i've written a simple library that shows how lookup.defineHiddenClass [1] 
>> can be
>> used to implement dynamic proxies
>> [ https://github.com/forax/hidden_proxy | 
>> https://github.com/forax/hidden_proxy
>> ]

>> regards,
>> Rémi

>> [1] [
>> https://download.java.net/java/early_access/jdk15/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#defineHiddenClass(byte%5B%5D,boolean,java.lang.invoke.MethodHandles.Lookup.ClassOption...)
>> |
>> https://download.java.net/java/early_access/jdk15/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#defineHiddenClass(byte%5B%5D,boolean,java.lang.invoke.MethodHandles.Lookup.ClassOption...)
>> ]

>>> De: "Dr Heinz M. Kabutz" < [ javascript-blocked: | [email protected] 
>>> ] >
>>> À: "mechanical-sympathy" < [ javascript-blocked: | 
>>> [email protected]
>>> ] >
>>> Envoyé: Dimanche 26 Avril 2020 19:11:53
>>> Objet: Re: Best way to dynamically spin Java classes?

>>> In Java 15 we will have hidden classes, which seem like the right model for 
>>> what
>>> you are trying to do.

>>> On Sun, 26 Apr 2020 at 19:36, Steven Stewart-Gallus < [ javascript-blocked: 
>>> |
>>> [email protected] ] > wrote:

>>>> I'm spinning a lot of classes dynamically for an interpreter.
>>>> I'm not sure the best way to load the classes into the VM.

>>>> Aside from Unsafe's defineAnonymousClass what's the best way to load 
>>>> classes
>>>> into the VM?

>>>> I believe I could create a new classloader for each spun class but IIRC
>>>> classloaders are expensive.

>>>> I believe as much as possible I'd want to reuse MethodHandle and
>>>> LambdaMetafactory to rely on the JVMs use of defineAnonymousClass.

>>>> --
>>>> You received this message because you are subscribed to the Google Groups
>>>> "mechanical-sympathy" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>>> email
>>>> to [ javascript-blocked: | 
>>>> [email protected] ] .
>>>> To view this discussion on the web, visit [
>>>> https://groups.google.com/d/msgid/mechanical-sympathy/8ded7316-7675-4747-afe5-5150a7b6009e%40googlegroups.com
>>>> |
>>>> https://groups.google.com/d/msgid/mechanical-sympathy/8ded7316-7675-4747-afe5-5150a7b6009e%40googlegroups.com
>>>> ] .

>>> --
>>> Dr Heinz M. Kabutz (PhD CompSci)
>>> Author of "The Java(tm) Specialists' Newsletter"
>>> Sun/Oracle Java Champion
>>> JavaOne Rockstar Speaker
>>> [ http://www.javaspecialists.eu/ | http://www.javaspecialists.eu ]
>>> Tel: +30 69 75 595 262
>>> Skype: kabutz
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "mechanical-sympathy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email
>>> to [ javascript-blocked: | [email protected] 
>>> ] .
>>> To view this discussion on the web, visit [
>>> https://groups.google.com/d/msgid/mechanical-sympathy/CACLL95rS-FtOi2511BqezhszD2ejxPp9BPUBFNUOnNNFndco_Q%40mail.gmail.com?utm_medium=email&utm_source=footer
>>> |
>>> https://groups.google.com/d/msgid/mechanical-sympathy/CACLL95rS-FtOi2511BqezhszD2ejxPp9BPUBFNUOnNNFndco_Q%40mail.gmail.com
>>> ] .

> --
> You received this message because you are subscribed to the Google Groups
> "mechanical-sympathy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email
> to [ mailto:[email protected] |
> [email protected] ] .
> To view this discussion on the web, visit [
> https://groups.google.com/d/msgid/mechanical-sympathy/b14b88e4-3a30-447d-a29b-ca7ce7fe738d%40googlegroups.com?utm_medium=email&utm_source=footer
> |
> https://groups.google.com/d/msgid/mechanical-sympathy/b14b88e4-3a30-447d-a29b-ca7ce7fe738d%40googlegroups.com
> ] .

-- 
You received this message because you are subscribed to the Google Groups 
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/mechanical-sympathy/1535967355.800160.1588350354833.JavaMail.zimbra%40u-pem.fr.

Reply via email to