On Thu, 12 Mar 2026 15:36:07 GMT, Jan Lahoda <[email protected]> wrote:

> Consider these classes:
> 
> package p;
> public class Lib {
>     void main(String... args) {
>         System.err.println("Lib!");
>     }
> }
> 
> and:
> 
> import p.Lib;
> public class Main extends Lib {
>     public void main() {
>         System.err.println("Main!");
>     }
> }
> 
> 
> Note the classes are in different packages. Running this on JDK 26 yields:
> 
> $ jdk-26/bin/java Main.java
> Lib!
> 
> 
> that is not correct - the method `Lib.main(String[])` is package private, and 
> is not inherited to `Main`, i.e. not a member of `Main`, and hence the 
> launcher should not use it. The launcher should only inspect methods that are 
> members (direct or inherited) of `Main`.
> 
> This PR fixes that by only using package-private methods in they are declared 
> in the same class as is the main class. Testing is enhanced to cover all 
> related cases I/we were able to find.
> 
> Also please review the corresponding CSR:
> https://bugs.openjdk.org/browse/JDK-8378555

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()`

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

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

Reply via email to