Got: IllegalArgumentException: Could not resolve class

Adam Sotona adam.sotona at oracle.com
Mon Apr 29 14:33:08 UTC 2024


FYI: For tracking I've created https://bugs.openjdk.org/browse/JDK-8331317

From: Brian Goetz <brian.goetz at oracle.com>
Date: Monday, 29 April 2024 at 13:54
To: Adam Sotona <adam.sotona at oracle.com>, Øystein Myhre Andersen <o.myhre at gmail.com>, classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: Re: Got: IllegalArgumentException: Could not resolve class
In addition, we should look into making the error mode more obvious; an IAE like this one looks very much like an internal bug.  It would be good if the exception was (a) a more specific type of exception, and (b) had more detail of the sort "I needed class hierarchy information for X but it wasn't provided."
On 4/29/2024 3:54 AM, Adam Sotona wrote:
Your compiler reached the complexity where you reference generated classes from other generated classes.
Class-File API in certain circumstances needs to know some information about the classes referenced from the generated bytecode.
Such information is provided in ClassHierarchyInfo<https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/classfile/ClassHierarchyResolver.ClassHierarchyInfo.html> using functional interface ClassHierarchyResolver<https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/classfile/ClassHierarchyResolver.html>.

By default<https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/classfile/ClassHierarchyResolver.html#defaultResolver()> is the information obtained from system class loader. However, the classes you generate are probably not yet known to the system class loader.
You should specify a custom ClassHierarchyResolver<https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/classfile/ClassHierarchyResolver.html> for your compiler as a Class-File API option `ClassFile.of(ClassFile.ClassHierarchyResolverOption.of(...))`.

Here you have multiple options how to provide the missing information using combinations of ClassHierarchyResolver factory methods and custom code:

For example, if the required classes have been already generated and you can provide a physical access to them, you can compose the ClassHierarchyResolver this way:
`ClassHierarchyResolver.defaultResolver().orElse(ClassHierarchyResolver. ofResourceParsing(Function<ClassDesc, InputStream>).cached())`

Or if you know all the generated classes in advance, you can provide the missing info about the generated classes in a set and map form:
`ClassHierarchyResolver.defaultResolver().orElse(ClassHierarchyResolver. of(Collection<ClassDesc> interfaces, Map<ClassDesc, ClassDesc> classToSuperClass))`

Or in a form of dynamic direct implementation of the ClassHierarchyResolver:
`ClassHierarchyResolver.defaultResolver().orElse(classDesc -> isInterface ? : ClassHierarchyInfo.ofInterface() : ClassHierarchyInfo.ofClass(ClassDesc superClass))`



From: classfile-api-dev <classfile-api-dev-retn at openjdk.org><mailto:classfile-api-dev-retn at openjdk.org> on behalf of Øystein Myhre Andersen <o.myhre at gmail.com><mailto:o.myhre at gmail.com>
Date: Sunday, 28 April 2024 at 12:53
To: classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org> <classfile-api-dev at openjdk.org><mailto:classfile-api-dev at openjdk.org>
Subject: Got: IllegalArgumentException: Could not resolve class
I'm writing a compiler for Simula (the very first oo language).
Simula is block-oriented with nested blocks and each block is compiled into a classFile.
At a certain level in the hierarchy I get an exception at the end of the classfile building.

Exception in thread "main" java.lang.IllegalArgumentException:
             Could not resolve class adHoc000_adHoc000_PBLK39_Floor_activateIdleLift


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20240429/9f94f97d/attachment.htm>


More information about the classfile-api-dev mailing list