<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>Hello,</div><div>If you need to define dynamically more than one class, the usual trick is to use an invokedynamic or a constant dynamic so the resolution of the other classes are not done using the ClassLoader but by the code of the bootstrap method of invokedynamic/constant dynamic.</div><div><br data-mce-bogus="1"></div><div>In you case, you can use constant dynamic to initialize the CSSMetaData, by emiting an LDC constant dynamic in the static <clinit> to initialise the static field ('A' in the example).</div><div><br data-mce-bogus="1"></div><div>regards,</div><div>RĂ©mi</div><div><br data-mce-bogus="1"></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"John Hendrikx" <john@int4.org><br><b>To: </b>"classfile-api-dev" <classfile-api-dev@openjdk.org><br><b>Sent: </b>Saturday, March 15, 2025 10:18:48 AM<br><b>Subject: </b>Define classes with circular dependency?<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><p>Hi list,<br>
      <br>
      I'm trying to use the ClassFile API to automatically implement
      control classes (as found in JavaFX). These classes define inner
      class CssMetaData implementations that refer back to the outer
      class, and the outer class refers to these implementations via
      static fields.  When I define one of the inner types using
      Lookup::defineClass I get a NoClassDefFoundError for the outer
      type.  When I define the outer type first, I get a
      NoClassDefFoundError for one of the inner types.  The situation is
      essentially this:</p>
    <div style="background-color:#ffffff;padding:0px 0px 0px 2px;">
      <div style="color:#000000;background-color:#ffffff;font-family:"Consolas";font-size:11pt;white-space:pre;"><p style="margin:0;"><span style="color:#000000;">    </span><span style="color:#0000a0;font-weight:bold;">public</span><span style="color:#000000;"> </span><span style="color:#0000a0;font-weight:bold;">class</span><span style="color:#000000;"> Sample {</span></p><p style="margin:0;"><span style="color:#000000;">      </span><span style="color:#0000a0;font-weight:bold;">private</span><span style="color:#000000;"> </span><span style="color:#0000a0;font-weight:bold;">final</span><span style="color:#000000;"> Property </span><span style="color:#0000c0;">b</span><span style="color:#000000;"> = </span><span style="color:#0000a0;font-weight:bold;">new</span><span style="color:#000000;"> Property(</span><span style="color:#0000c0;">A</span><span style="color:#000000;">);</span></p><p style="margin:0;"><span style="color:#000000;">    </span></p><p style="margin:0;"><span style="color:#000000;">      </span><span style="color:#0000a0;font-weight:bold;">private</span><span style="color:#000000;"> </span><span style="color:#0000a0;font-weight:bold;">static</span><span style="color:#000000;"> </span><span style="color:#0000a0;font-weight:bold;">final</span><span style="color:#000000;"> CssMetaData </span><span style="color:#0000c0;">A</span><span style="color:#000000;"> = </span><span style="color:#0000a0;font-weight:bold;">new</span><span style="color:#000000;"> CssMetaData() {</span></p><p style="margin:0;"><span style="color:#000000;">        </span><span style="color:#646464;">@Override</span></p><p style="margin:0;"><span style="color:#000000;">        </span><span style="color:#0000a0;font-weight:bold;">public</span><span style="color:#000000;"> Property getProperty(Object obj) {</span></p><p style="margin:0;"><span style="color:#000000;">          </span><span style="color:#7f0055;font-weight:bold;">return</span><span style="color:#000000;"> ((Sample)obj).</span><span style="color:#0000c0;">b</span><span style="color:#000000;">;</span></p><p style="margin:0;"><span style="color:#000000;">        }</span></p><p style="margin:0;"><span style="color:#000000;">      };</span></p><p style="margin:0;"><span style="color:#000000;">    }</span></p><p style="margin:0;">
</p><p style="margin:0;"><span style="color:#000000;">    </span><span style="color:#0000a0;font-weight:bold;">abstract</span><span style="color:#000000;"> </span><span style="color:#0000a0;font-weight:bold;">class</span><span style="color:#000000;"> CssMetaData {</span></p><p style="margin:0;"><span style="color:#000000;">      </span><span style="color:#0000a0;font-weight:bold;">abstract</span><span style="color:#000000;"> Property getProperty(Object obj);</span></p><p style="margin:0;"><span style="color:#000000;">    }</span></p><p style="margin:0;"><span style="color:#000000;">  </span></p><p style="margin:0;"><span style="color:#000000;">    </span><span style="color:#0000a0;font-weight:bold;">class</span><span style="color:#000000;"> Property {</span></p><p style="margin:0;"><span style="color:#000000;">      </span><span style="color:#0000a0;font-weight:bold;">public</span><span style="color:#000000;"> Property(CssMetaData </span><span style="color:#000000;text-decoration:underline;text-decoration-color:#f4c82d;text-decoration-style:wavy;">a</span><span style="color:#000000;">) {</span></p><p style="margin:0;"><span style="color:#000000;">      }</span></p><p style="margin:0;"><span style="color:#000000;">    }</span></p></div>
    </div>
    <p></p>
    <p>I'm trying to generate the Sample class.  The classes CssMetaData
      and Propery are pre-existing.  As you can see, Sample refers to A
      in a property it creates, while A refers to that property by
      direct field access after a cast.</p>
    <p>Note that the above is perfectly legal as a Java class, and I
      think the bytecode I generate is correct.  It seems I would need
      to be able to define both classes at the same time, but Lookup
      doesn't seem to have anything for this purpose.</p>
    <p>I'd appreciate any insights!</p>
    <p>--John<br>
    </p>
    <p></p><br></blockquote></div></div></body></html>