graal
graal copied to clipboard
[GR-27596] ClassLoader class getPackage equal in Java but not equal in Native Image
Describe the issue
The default ClassLoader class getPackage is equal in Java but not equal in Native Image. Is this an expected result?
Steps to reproduce the issue
public class PackageTest {
public static void main(String[] args) {
ClassLoader cl = PackageTest.class.getClassLoader();
assert (cl != null);
Class<?> c = cl.getClass();
Package p1 = c.getPackage();
Package p2 = c.getPackage();
assert (p1 != null && p2 != null);
System.err.println(p1.equals(p2));
}
}
javac PackageTest.java
java PackageTest // prints true
mx native-image PackageTest
./packagetest //prints false
Describe GraalVM and your environment:
- CE: commit
4d56547a080c7f97fef5b8e6d19827ed66a0c398 - JDK:
labsjdk-ce-11.0.9-jvmci-20.3-b06 - OS: [Fedora 31]
- Architecture: [AMD64]
@jiekang thanks for reporting the bug We will look into it
Thanks. To be clear, I'm curious if this is intentional or not, but I'd also prefer that behavior was the same between Java and native image.
@jiekang We have added this in our feature list and will be fixed in future releases, please do check out https://www.graalvm.org/ to get update on future releases.
@mcraj017 Okay sounds good; I appreciate the communication!
The reason there is a difference between native and Java mode is that this Native Image substitution causes classes loaded by the bootstrap class loader to construct new packages when Class#getPackage() is called. In regular Java, new package instances are not created.
So in @jiekang's example, 2 independent objects are created and compared. Thus not equal.
Package p1 = c.getPackage();
Package p2 = c.getPackage();
This is related to another open issue here: https://github.com/oracle/graal/issues/8796