graal icon indicating copy to clipboard operation
graal copied to clipboard

[GR-27596] ClassLoader class getPackage equal in Java but not equal in Native Image

Open jiekang opened this issue 5 years ago • 5 comments
trafficstars

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 avatar Nov 17 '20 16:11 jiekang

@jiekang thanks for reporting the bug We will look into it

munishchouhan avatar Nov 20 '20 09:11 munishchouhan

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 avatar Nov 20 '20 15:11 jiekang

@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.

munishchouhan avatar Nov 25 '20 13:11 munishchouhan

@mcraj017 Okay sounds good; I appreciate the communication!

jiekang avatar Nov 25 '20 14:11 jiekang

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

roberttoyonaga avatar Apr 23 '24 19:04 roberttoyonaga