<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body><div style="font-family: sans-serif;"><div class="markdown" style="white-space: normal;">
<p dir="auto">P.S. I trust it is clear how the single service method below would be used in the <code style="margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7;"><clinit></code> 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 <code style="margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7;">MethodHandle enumMemberCreator</code> 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.</p>
<p dir="auto">On 11 Oct 2023, at 18:01, John Rose wrote:</p>
</div><div class="plaintext" style="white-space: normal;"><blockquote style="margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777;"><p dir="auto">```
<br>
public static void initializeEnumClass(Lookup enumClassLU, MethodHandle enumMemberCreator) {
<br>
  int ordinal = 0;
<br>
  if (!enumClassLU.hasPrivateAccess())  throw (IAE);
<br>
  Class<? extends Enum> ec = enumClassLU.lookupClass().asSubClass(Enum.class);
<br>
  for (Field f : ec.getDeclaredFields()) {  //order significant here
<br>
    if (f is an enum member) {
<br>
      Object e = enumMemberCreator.invokeExact(f, ordinal++);
<br>
      // next stuff can be done more directly by Unsafe
<br>
      assert(f.get(null) == null);  //caller resp.
<br>
      f.setAccessible(true);
<br>
      f.set(null, e);
<br>
    }
<br>
  }
<br>
}
<br>
```</p>
<p dir="auto">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.)</p>
<p dir="auto">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).</p>
</blockquote></div>
<div class="markdown" style="white-space: normal;">

</div></div></body>

</html>