Fix main method detection involving Application

This commit is contained in:
Mark Harrah 2010-01-30 21:40:25 -05:00
parent a5732b56ab
commit a2758afd43
4 changed files with 16 additions and 7 deletions

View File

@ -155,7 +155,6 @@ final class Analyzer(val global: Global, val callback: AnalysisCallback) extends
}
private def isVisible(sym: Symbol) = sym != NoSymbol && sym.isPublic && !sym.isDeferred
private def isMainType(tpe: Type): Boolean =
{
tpe match
{
// singleArgument is of type Symbol in 2.8.0 and type Type in 2.7.x
@ -163,13 +162,15 @@ final class Analyzer(val global: Global, val callback: AnalysisCallback) extends
case PolyType(typeParams, result) => isMainType(result)
case _ => false
}
}
private lazy val StringArrayType = appliedType(definitions.ArrayClass.typeConstructor, definitions.StringClass.tpe :: Nil)
// isStringArray is overloaded to handle the incompatibility between 2.7.x and 2.8.0
private def isStringArray(tpe: Type): Boolean = tpe =:= StringArrayType
private def isStringArray(tpe: Type): Boolean =
tpe =:= StringArrayType ||
// needed for main defined in parent trait, not sure why
tpe.typeSymbol == definitions.ArrayClass && tpe.typeArgs.length == 1 && tpe.typeArgs(0).typeSymbol == definitions.StringClass
private def isStringArray(sym: Symbol): Boolean = isStringArray(sym.tpe)
private def isUnitType(tpe: Type) = tpe.typeSymbol == definitions.UnitClass
// required because the 2.8 way to find a class is:
// classPath.findClass(name).flatMap(_.binary)
// and the 2.7 way is:

View File

@ -92,16 +92,22 @@ object ApplicationsTest extends Specification
type A[T] = Array[String]
def main[T](args: A[T]) {}
}
""" :: """
object MainF1 extends Application { var x = 3; x = 5 }
object MainF2 { def main(args: Array[java.lang.String]) {} }
trait MainF3 { def main(args: Array[String]) {} }
object MainF4 extends MainF3 { }
""" ::
Nil
val sources = for((source, index) <- sourceContent.zipWithIndex) yield new File("Main" + (index+1) + ".scala") -> source
"Analysis plugin should detect applications" in {
WithFiles(sources : _*) { case files @ Seq(main, main2, main3, main4, main5, main6, main7, main8, main9, mainA, mainB, mainC, mainD, mainE) =>
WithFiles(sources : _*) { case files @ Seq(main, main2, main3, main4, main5, main6, main7, main8, main9, mainA, mainB, mainC, mainD, mainE, mainF) =>
for(scalaVersion <- TestCompile.allVersions)
CallbackTest(scalaVersion, files, Nil) { (callback, file, log) =>
val expected = Seq( main -> "Main", main4 -> "Main4", main8 -> "Main8", main9 -> "Main9", mainB -> "MainB",
mainE -> "MainE1", mainE -> "MainE2", mainE -> "MainE3", mainE -> "MainE4", mainE -> "MainE5" )
mainE -> "MainE1", mainE -> "MainE2", mainE -> "MainE3", mainE -> "MainE4", mainE -> "MainE5",
mainF -> "MainF1", mainF -> "MainF2", mainF -> "MainF4")
(callback.applications) must haveTheSameElementsAs(expected)
val loader = new URLClassLoader(Array(file.toURI.toURL), getClass.getClassLoader)
for( (_, className) <- expected) testRun(loader, className)

View File

@ -14,6 +14,8 @@ object CompileTest extends Specification
WithCompiler( "2.7.3" )(testCompileAnalysis)
WithCompiler( "2.7.4" )(testCompileAnalysis)
WithCompiler( "2.7.5" )(testCompileAnalysis)
WithCompiler( "2.7.7" )(testCompileAnalysis)
WithCompiler( "2.8.0.Beta1" )(testCompileAnalysis)
WithCompiler( "2.8.0-SNAPSHOT" )(testCompileAnalysis)
}
}

View File

@ -8,7 +8,7 @@ import FileUtilities.withTemporaryDirectory
object TestCompile
{
// skip 2.7.3 and 2.7.4 for speed
def allVersions = List("2.7.2", "2.7.5", "2.8.0-SNAPSHOT")//List("2.7.2", "2.7.3", "2.7.4", "2.7.5", "2.8.0-SNAPSHOT")
def allVersions = List("2.7.2", "2.7.5", "2.7.7", "2.8.0-SNAPSHOT", "2.8.0.Beta1")//List("2.7.2", "2.7.3", "2.7.4", "2.7.5", "2.8.0-SNAPSHOT")
/** Tests running the compiler interface with the analyzer plugin with a test callback. The test callback saves all information
* that the plugin sends it for post-compile analysis by the provided function.*/
def apply[T](scalaVersion: String, sources: Set[File], outputDirectory: File, options: Seq[String], superclassNames: Seq[String])(f: (TestCallback, CompileLogger) => T): T =