java.lang.IllegalArgumentException: Class not in same package as lookup class when compiling classes in subpackage of caller class in JDK 11
lukaseder opened this issue · 0 comments
When compiling classes in JDK 9+, the following exception can be thrown from tests:
org.joor.ReflectException: Error while compiling org.joor.test.CompileSameClassName
at org.joor.Compile.compile(Compile.java:148)
at org.joor.Reflect.compile(Reflect.java:102)
at org.joor.Reflect.compile(Reflect.java:77)
at org.joor.test.CompileTest.testRecompileSameClassName(CompileTest.java:92)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Class not in same package as lookup class
at java.base/java.lang.invoke.MethodHandles$Lookup.defineClass(MethodHandles.java:955)
at org.joor.Compile.compile(Compile.java:125)
... 25 more
This happens when the caller class has a package name x.y
, and the compiled class is in a subpackage x.y.z.Klass
The failing logic just checks for package name prefixes:
if (className.startsWith(caller.getPackageName() + ".")) {
result = MethodHandles
.privateLookupIn(caller, lookup)
.defineClass(fileManager.o.getBytes());
}
This is obviously wrong, a prefix is not sufficient. Unfortunately, at this point, className
is but a string, and we don't have any Class
reference yet (loading this reference being the point of this logic), so we cannot formally compare package names.
A simple heuristic would be to check if Character.isUpperCase(className.charAt(caller.getPackageName().length() + 1))
, because in Java, the convention requires package names to be lower case and class names to be upper case. This would work for most classes.
Note: We can't simply check for additional .
in the suffix after the caller.getPackageName().length() + 1
, because that would exclude nested classes from being permitted here. So, there is some risk of not handling correctly classes that do not follow the naming convention.