Skip to content

Auto detect main methods inherited from Java sources (including JEP 512 ones) - #4425

Open
Gedochao wants to merge 2 commits into
VirtusLab:mainfrom
Gedochao:feature/detect-inherited-main-methods-2
Open

Auto detect main methods inherited from Java sources (including JEP 512 ones)#4425
Gedochao wants to merge 2 commits into
VirtusLab:mainfrom
Gedochao:feature/detect-inherited-main-methods-2

Conversation

@Gedochao

@Gedochao Gedochao commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #4418 (comment)

This enables auto-detection of main methods inherited from Java classes/interfaces. Including ones added by JEP 512.

// C.java
public abstract class C {
    void main() {
        System.out.println("Hello from an instance main");
    }
}
// A.java
public class A extends C

Checklist

  • tested the solution locally and it works
  • ran the code formatter (scala-cli fmt .)
  • ran scalafix (./mill -i __.fix)

How much have your relied on LLM-based tools in this contribution?

extensively

How was the solution tested?

added automated tests

@Gedochao
Gedochao requested a review from warcholjakub August 13, 2026 08:45
@Gedochao
Gedochao marked this pull request as ready for review August 13, 2026 08:45

@warcholjakub warcholjakub left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two main things I found:

  • First, not caused by this PR:
    We don't check whether the main method is protected (and it's not allowed for non JEP512 Java versions). And in result:
public class Test {
  protected static void main(String[] args) {
    System.out.println("hello");
  }
}

yields:

./mill -i scala run /tmp/test-repro/Test.java --jvm 17
[...]
Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
924/925, 1 FAILED] /Users/jwarchol/Documents/GitHub/scala-cli/millw scala run /tmp/test-repro/Test.java --jvm 17 3s

I'll try to prepare a fix.
EDIT1: #4430
EDIT2: We'll probably have to make sure it works properly in inheritance too. So, imho, it'd probably be best to merge it before this PR, and address it here.

  • Second, related to the changes:
package far;

public abstract class FarBase {
  static void main(String[] args) {
    System.out.println("FarBase.main");
  }
}
package near;

public abstract class NearBase extends far.FarBase {
  private static void main(String[] args) {
    System.out.println("NearBase.main");
  }
}
package near;

public class Child extends NearBase {}

results in Child being incorrectly classified as a main class.

@Gedochao
Gedochao marked this pull request as draft August 18, 2026 12:20
@Gedochao
Gedochao force-pushed the feature/detect-inherited-main-methods-2 branch from 343c469 to 5519ecf Compare August 18, 2026 12:52
@Gedochao
Gedochao force-pushed the feature/detect-inherited-main-methods-2 branch from 5519ecf to 8c333e6 Compare August 18, 2026 14:01
@Gedochao
Gedochao marked this pull request as ready for review August 19, 2026 11:12
@Gedochao
Gedochao requested a review from warcholjakub August 19, 2026 11:12
@Gedochao

Copy link
Copy Markdown
Contributor Author

Rebased on top of #4430 plus added handling for the missing case.
Should be ready to re-review.

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.

2 participants