Define classes with circular dependency?

Michael van Acken michael.van.acken at gmail.com
Sat Mar 15 09:53:20 UTC 2025


As far as I know, at time of definition only the class being extended must
be available.
>From your example, this seems to be j.l.Object both times, so this should
not be the problem.

But your case triggers a vague recollection, where I had the same behaviour
of
Lookup.defineClass() in some gnarly unit test of my compiler -- with a
NoCDFE that
I was not able to explain.

Out of curiosity: what happens when you catch the exception and do a
findClass using
the same lookup and the dotted class name of the class you just tried to
define?  In
my case, I got back the class instance in the catch clause, suggesting the
defineClass
completed after all.

-- mva






Am Sa., 15. März 2025 um 10:19 Uhr schrieb John Hendrikx <john at int4.org>:

> Hi list,
>
> 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:
>
> public class Sample {
>
> private final Property b = new Property(A);
>
> private static final CssMetaData A = new CssMetaData() {
>
> @Override
>
> public Property getProperty(Object obj) {
>
> return ((Sample)obj).b;
>
> }
>
> };
>
> }
>
> abstract class CssMetaData {
>
> abstract Property getProperty(Object obj);
>
> }
>
> class Property {
>
> public Property(CssMetaData a) {
>
> }
>
> }
>
> 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.
>
> 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.
>
> I'd appreciate any insights!
>
> --John
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20250315/5713355f/attachment.htm>


More information about the classfile-api-dev mailing list