On Thu, 19 Mar 2026 19:38:57 GMT, Alan Bateman <[email protected]> wrote:

>> src/java.base/share/classes/jdk/internal/misc/MethodFinder.java line 107:
>> 
>>> 105:                (Modifier.isPublic(mainMethodCandidate.getModifiers()) 
>>> ||
>>> 106:                 
>>> Modifier.isProtected(mainMethodCandidate.getModifiers()) ||
>>> 107:                 initialClass.getPackage() == 
>>> mainMethodCandidate.getDeclaringClass().getPackage());
>> 
>> Hello Jan, `Class.getPackage()` doesn't specify that it will return the same 
>> `Package` instance for multiple calls of that method on the same `Class` 
>> instance. Briefly looking at the implementation, it appears that only 
>> `BootClassLoader` might return the same `Package` instance for multiple 
>> calls.
>> 
>> I was going to suggest that maybe we should use `Package.equals()`, but I 
>> can't find an implementation of that method in `Package`, so it would again 
>> end up being a identity check.
>> 
>> The other option would be checking the package name `equals()`ity but I 
>> think that would also additionally need a ClassLoader equality check for 
>> `initialClass.getClassLoader()` and 
>> `mainMethodCandidate.getDeclaringClass().getClassLoader()`
>
> Right, it should use Class.getClassLoader + Class::getPackageName to check if 
> the two classes are in the same runtime package.

Thanks. I've replaced the `getPackage()` with `getPackageName()` + 
`getClassLoader()`. I added a test verifying the runtime package is respected 
correctly. But I wonder if there are suggestions on a test which would fail 
with `getPackage()` and pass with `getPackageName()+getClassLoader()`, as the 
current test passes with both. I was reading the code for `Class.getPackage()`, 
and I don't see what should I do to get such a test (besides maybe using 
reflection to replace `ClassLoader.packages` with a custom instance of 
`ConcurrentHashMap` or instrumenting one of the relevant method, or something 
similar to these). Thanks!

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/30221#discussion_r2964765048

Reply via email to