condy and enums

John Rose john.r.rose at oracle.com
Thu Oct 12 01:07:59 UTC 2023


P.S. I trust it is clear how the single service method below would be 
used in the `<clinit>` of each client enum.  One other thing occurred to 
me:  Enums which have bootstrap entanglements with the MethodHandle 
class would need special treatment.  For that reason, it might be better 
to not pass the argument `MethodHandle enumMemberCreator` but rather 
just call a private static method of a fixed name and signature, within 
the same enum.  That can be easily done with method handles, if those 
are “on line”, but can also be done with core reflection, which 
boots up sooner.

On 11 Oct 2023, at 18:01, John Rose wrote:

> ```
> public static void initializeEnumClass(Lookup enumClassLU, 
> MethodHandle enumMemberCreator) {
>   int ordinal = 0;
>   if (!enumClassLU.hasPrivateAccess())  throw (IAE);
>   Class<? extends Enum> ec = 
> enumClassLU.lookupClass().asSubClass(Enum.class);
>   for (Field f : ec.getDeclaredFields()) {  //order significant here
>     if (f is an enum member) {
>       Object e = enumMemberCreator.invokeExact(f, ordinal++);
>       // next stuff can be done more directly by Unsafe
>       assert(f.get(null) == null);  //caller resp.
>       f.setAccessible(true);
>       f.set(null, e);
>     }
>   }
> }
> ```
>
> The creation of the values array should be done in `<clinit>`, as 
> well, or as a condy (yes, that’s a good usage of condy!) and cloned 
> as a fresh copy for each call to `values()`.  And it can be done 
> reflectively as well.  Just iterate over all the fields and store them 
> into the array.  (Use the `ordinal()` as the array index, or just 
> assert that the fields are in the correct order already.)
>
> With those two adjustments, to bind enums and build the values array 
> reflectively, your enum would be limited only by the maximum size of 
> the constant pool.  That is, you could have up to about 65k enums (but 
> not the whole 2^16).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20231011/74a6da3e/attachment-0001.htm>


More information about the compiler-dev mailing list