Issue #4461: only recognize instance main methods in instantiable classes - #4532
Open
bhargavikvmpl-2001 wants to merge 1 commit into
Open
bhargavikvmpl-2001 wants to merge 1 commit into
bhargavikvmpl-2001 wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
… in instantiable classes Since Java 25 support (GoogleContainerTools#4432), MainClassFinder accepts instance main methods. Scala 2 compiles `object Main` into a `Main$` class with an instance main(String[]) but a private constructor, so both Main and Main$ were reported as main classes and main class inference failed. JEP 512 only launches an instance main if the class can be instantiated through a non-private no-args constructor. Apply that rule: instance mains now count only for concrete classes with such a constructor. Static mains are unaffected.
bhargavikvmpl-2001
force-pushed
the
fix-4461-scala-instance-main
branch
from
September 29, 2026 22:03
c93a198 to
1f8df03
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4461 🛠️
Problem
Since Java 25 support was added in #4432,
MainClassFinderaccepts instancemainmethods. Scala 2 compilesobject Maininto two classes:Main, with a static forwardermain, andMain$, the module class, which has an instancemain(String[])and a private constructor. Both classes were reported as main classes, so main class inference failed with multiple candidates on Scala 2.13 + JDK 25.Solution
JEP 512 only launches an instance
mainif the class can be instantiated through a non-private constructor with no parameters.MainClassVisitornow records static and instance mains separately, along with whether the class is concrete (not abstract, not an interface) and has a non-private no-args<init>. An instancemaincounts only when both hold. Static mains, and class files older than Java 25, are unaffected. I described the cause and this approach in the issue before opening the PR.Tests
New fixtures in
class-finder-tests/java25-instance-main-requirements(class file version 69):ScalaObjectMain+ScalaObjectMain$: finds exactlyScalaObjectMain(the case from Scala2 JDK25 MainClassFinder finds the singleton implementation class name(Main$) along with the correct class name(Main) #4461)PrivateConstructorInstanceMain: instance main with only a private constructor, not a main classAbstractInstanceMain: instance main in an abstract class, not a main classThese class files were generated with ASM to match the structure Scala 2 emits (private constructor,
MODULE$field, instancemain), since the build doesn't include a Scala or JDK 25 compiler. I'm happy to share the generator or swap inscalacoutput if you prefer.MainClassFinderTesttests pass with it./gradlew :jib-core:testpasses (606 tests), plusverifyGoogleJavaFormatand checkstyle, on JDK 11🤖 Generated with Claude Code