Defining anonymous classes

Mark Roos mroos at roos.com
Thu Aug 14 20:15:24 UTC 2014


        Is there an end-user accessible way of defining anonymous classes 
(by 
        which I mean classes which are kept alive only by explicit 
references or 
        their instances, and not their class loader)?

        Searching for the term "anonymous classes" isn't particularly 
revealing 
        due to the Java-level language construct of the same name.
Look into sun.Misc.Unsafe

 public native Class defineAnonymousClass(Class hostClass,
    byte[] data,
    Object[] cpPatches)
Define a class but do not make it known to the class loader or system 
dictionary.

For each CP entry, the corresponding CP patch must either be null or have
the a format that matches its tag:

        ◦      Integer, Long, Float, Double: the corresponding wrapper 
object type from java.lang
        ◦      Utf8: a string (must have suitable syntax if used as 
signature or name)
        ◦      Class: any java.lang.Class object
        ◦      String: any object (not just a java.lang.String)
        ◦      InterfaceMethodRef: (NYI) a method handle to invoke on 
that call site's arguments

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20140814/884a61cc/attachment.html>


More information about the mlvm-dev mailing list