Skip to content

Issue #4461: only recognize instance main methods in instantiable classes - #4532

Open
bhargavikvmpl-2001 wants to merge 1 commit into
GoogleContainerTools:masterfrom
bhargavikvmpl-2001:fix-4461-scala-instance-main
Open

bhargavikvmpl-2001 wants to merge 1 commit into
GoogleContainerTools:masterfrom
bhargavikvmpl-2001:fix-4461-scala-instance-main

Conversation

@bhargavikvmpl-2001

Copy link
Copy Markdown

Fixes #4461 🛠️

Problem

Since Java 25 support was added in #4432, MainClassFinder accepts instance main methods. Scala 2 compiles object Main into two classes: Main, with a static forwarder main, and Main$, the module class, which has an instance main(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 main if the class can be instantiated through a non-private constructor with no parameters. MainClassVisitor now 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 instance main counts 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):

These class files were generated with ASM to match the structure Scala 2 emits (private constructor, MODULE$ field, instance main), since the build doesn't include a Scala or JDK 25 compiler. I'm happy to share the generator or swap in scalac output if you prefer.

  • All 3 new tests fail without the change; all 20 MainClassFinderTest tests pass with it
  • ./gradlew :jib-core:test passes (606 tests), plus verifyGoogleJavaFormat and checkstyle, on JDK 11

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Sep 29, 2026

Copy link
Copy Markdown

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
bhargavikvmpl-2001 force-pushed the fix-4461-scala-instance-main branch from c93a198 to 1f8df03 Compare September 29, 2026 22:03

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scala2 JDK25 MainClassFinder finds the singleton implementation class name(Main$) along with the correct class name(Main)

1 participant