Skip to content
This repository has been archived by the owner. It is now read-only.

Commit e133763

Browse files
committed
Fix #102: Better main class detection
Previously, the main class detection was handled by https://github.com/sbt/zinc/blob/1.0/internal/zinc-apiinfo/src/main/scala/xsbt/api/Discovery.scala which looks for a main method with the correct signature in the extracted API. This is imperfect because it relies on ExtractAPI dealiasing types (because Discovery will look for a main method with a parameter type of `java.lang.String` and won't recognize `scala.Predef.String`), dealiasing means that the extracted API looses information and thus can lead to undercompilation. This commit partially fixes this by adding a new callback to AnalysisCallback: void mainClass(File sourceFile, String className) that is used to explicitly register main entry points. This way, tools do not need to interpret the extracted API, this is much better since it makes it easier for zinc to evolve the API representation. This commit does not actually changes ExtractAPI to not dealias, this can be done in a later PR. Note that there is another usecase for xsbt.api.Discovery that this PR does not replace: discovering tests. This is more complicated because different test frameworks have different ways to discover tests. For more information, grep for "Fingerprint" in https://github.com/sbt/sbt and https://github.com/sbt/junit-interface
1 parent 009b567 commit e133763

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/main/java/xsbti/AnalysisCallback.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ void generatedNonLocalClass(File source,
110110
*/
111111
void api(File sourceFile, xsbti.api.ClassLike classApi);
112112

113+
/**
114+
* Register a class containing an entry point coming from a given source file.
115+
*
116+
* A class is an entry point if its bytecode contains a method with the
117+
* following signature:
118+
* <pre>
119+
* public static void main(String[] args);
120+
* </pre>
121+
*
122+
* @param sourceFile Source file where <code>className</code> is defined.
123+
* @param className A class containing an entry point.
124+
*/
125+
void mainClass(File sourceFile, String className);
126+
113127
/**
114128
* Register the use of a <code>name</code> from a given source class name.
115129
*
@@ -158,4 +172,4 @@ void problem(String what,
158172
* phase defined by <code>xsbt-analyzer</code> should be added.
159173
*/
160174
boolean enabled();
161-
}
175+
}

src/main/java/xsbti/compile/analysis/SourceInfo.java

+7
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,12 @@ public interface SourceInfo {
3030
* @return The compiler reported problems.
3131
*/
3232
public Problem[] getUnreportedProblems();
33+
34+
/**
35+
* Returns the main classes found in this compilation unit.
36+
*
37+
* @return The full name of the main classes, like "foo.bar.Main"
38+
*/
39+
public String[] getMainClasses();
3340
}
3441

src/test/scala/xsbti/TestCallback.scala

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class TestCallback extends AnalysisCallback {
6464
()
6565
}
6666

67+
def mainClass(source: File, className: String): Unit = ()
68+
6769
override def enabled(): Boolean = true
6870

6971
def problem(category: String,

0 commit comments

Comments
 (0)