Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jiekang opened this issue Nov 17, 2020 · 5 comments
Assignees

Comments

@jiekang
Copy link
Collaborator

jiekang commented Nov 17, 2020

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]
@munishchouhan
Copy link
Contributor

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

@jiekang
Copy link
Collaborator Author

jiekang commented Nov 20, 2020

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.

@munishchouhan
Copy link
Contributor

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

@jiekang
Copy link
Collaborator Author

jiekang commented Nov 25, 2020

@mcraj017 Okay sounds good; I appreciate the communication!

@wirthi wirthi changed the title ClassLoader class getPackage equal in Java but not equal in Native Image [GR-27596] ClassLoader class getPackage equal in Java but not equal in Native Image Feb 1, 2024
@roberttoyonaga
Copy link
Collaborator

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: #8796

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants