2009-07-27 01:15:02 +02:00
|
|
|
/* sbt -- Simple Build Tool
|
2010-06-14 04:59:29 +02:00
|
|
|
* Copyright 2009, 2010 Mark Harrah
|
2009-07-27 01:15:02 +02:00
|
|
|
*/
|
2010-06-14 04:59:29 +02:00
|
|
|
package sbt
|
|
|
|
|
package classfile
|
2009-07-27 01:15:02 +02:00
|
|
|
|
|
|
|
|
import scala.collection.mutable
|
|
|
|
|
import mutable.{ArrayBuffer, Buffer}
|
|
|
|
|
import java.io.File
|
2010-03-28 06:05:40 +02:00
|
|
|
import java.lang.annotation.Annotation
|
|
|
|
|
import java.lang.reflect.Method
|
|
|
|
|
import java.lang.reflect.Modifier.{STATIC, PUBLIC, ABSTRACT}
|
2009-07-27 01:15:02 +02:00
|
|
|
|
2009-07-27 21:38:09 +02:00
|
|
|
private[sbt] object Analyze
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
2010-07-15 01:24:50 +02:00
|
|
|
def apply[T](outputDirectory: Path, sources: Seq[Path], roots: Seq[Path], log: Logger)(analysis: xsbti.AnalysisCallback, loader: ClassLoader)(compile: => Unit)
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
|
|
|
|
val sourceSet = Set(sources.toSeq : _*)
|
|
|
|
|
val classesFinder = outputDirectory ** GlobFilter("*.class")
|
|
|
|
|
val existingClasses = classesFinder.get
|
2010-03-28 06:05:40 +02:00
|
|
|
|
2010-07-15 01:24:50 +02:00
|
|
|
def load(tpe: String, errMsg: => Option[String]): Option[Class[_]] =
|
2010-03-28 06:05:40 +02:00
|
|
|
try { Some(Class.forName(tpe, false, loader)) }
|
2010-07-15 01:24:50 +02:00
|
|
|
catch { case e => errMsg.foreach(msg => log.warn(msg + " : " +e.toString)); None }
|
2010-03-28 06:05:40 +02:00
|
|
|
|
2009-07-27 01:15:02 +02:00
|
|
|
// runs after compilation
|
|
|
|
|
def analyze()
|
|
|
|
|
{
|
|
|
|
|
val allClasses = Set(classesFinder.get.toSeq : _*)
|
2010-07-15 01:24:50 +02:00
|
|
|
val newClasses = allClasses -- existingClasses
|
2009-07-27 01:15:02 +02:00
|
|
|
|
|
|
|
|
val productToSource = new mutable.HashMap[Path, Path]
|
|
|
|
|
val sourceToClassFiles = new mutable.HashMap[Path, Buffer[ClassFile]]
|
2010-03-28 06:05:40 +02:00
|
|
|
|
2009-07-27 01:15:02 +02:00
|
|
|
// parse class files and assign classes to sources. This must be done before dependencies, since the information comes
|
|
|
|
|
// as class->class dependencies that must be mapped back to source->class dependencies using the source+class assignment
|
|
|
|
|
for(newClass <- newClasses;
|
|
|
|
|
path <- Path.relativize(outputDirectory, newClass);
|
2010-06-14 04:59:29 +02:00
|
|
|
classFile = Parser(newClass.asFile);
|
2010-05-11 00:52:22 +02:00
|
|
|
sourceFile <- classFile.sourceFile orElse guessSourceName(newClass.asFile.getName);
|
2009-07-27 14:51:29 +02:00
|
|
|
source <- guessSourcePath(sourceSet, roots, classFile, log))
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
|
|
|
|
analysis.beginSource(source)
|
|
|
|
|
analysis.generatedClass(source, path)
|
|
|
|
|
productToSource(path) = source
|
|
|
|
|
sourceToClassFiles.getOrElseUpdate(source, new ArrayBuffer[ClassFile]) += classFile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get class to class dependencies and map back to source to class dependencies
|
|
|
|
|
for( (source, classFiles) <- sourceToClassFiles )
|
|
|
|
|
{
|
2009-07-27 15:52:38 +02:00
|
|
|
def processDependency(tpe: String)
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
2010-06-14 04:59:29 +02:00
|
|
|
trapAndLog(log)
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
2010-07-15 01:24:50 +02:00
|
|
|
val loaded = load(tpe, Some("Problem processing dependencies of source " + source))
|
2010-06-14 04:59:29 +02:00
|
|
|
for(clazz <- loaded; file <- ErrorHandling.convert(IO.classLocationFile(clazz)).right)
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
2009-07-27 17:13:55 +02:00
|
|
|
if(file.isDirectory)
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
2009-07-27 17:13:55 +02:00
|
|
|
val resolved = resolveClassFile(file, tpe)
|
|
|
|
|
assume(resolved.exists, "Resolved class file " + resolved + " from " + source + " did not exist")
|
|
|
|
|
val resolvedPath = Path.fromFile(resolved)
|
|
|
|
|
if(Path.fromFile(file) == outputDirectory)
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
2009-07-27 17:13:55 +02:00
|
|
|
productToSource.get(resolvedPath) match
|
|
|
|
|
{
|
|
|
|
|
case Some(dependsOn) => analysis.sourceDependency(dependsOn, source)
|
|
|
|
|
case None => analysis.productDependency(resolvedPath, source)
|
|
|
|
|
}
|
2009-07-27 01:15:02 +02:00
|
|
|
}
|
2009-07-27 17:13:55 +02:00
|
|
|
else
|
|
|
|
|
analysis.classDependency(resolved, source)
|
2009-07-27 01:15:02 +02:00
|
|
|
}
|
|
|
|
|
else
|
2009-07-27 17:13:55 +02:00
|
|
|
analysis.jarDependency(file, source)
|
2009-07-27 01:15:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
classFiles.flatMap(_.types).foreach(processDependency)
|
|
|
|
|
analysis.endSource(source)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-15 01:24:50 +02:00
|
|
|
compile
|
|
|
|
|
analyze()
|
|
|
|
|
}
|
|
|
|
|
private def trapAndLog(log: Logger)(execute: => Unit)
|
|
|
|
|
{
|
|
|
|
|
try { execute }
|
|
|
|
|
catch { case e => log.trace(e); log.error(e.toString) }
|
2009-07-27 01:15:02 +02:00
|
|
|
}
|
2010-05-11 00:52:22 +02:00
|
|
|
private def guessSourceName(name: String) = Some( takeToDollar(trimClassExt(name)) )
|
|
|
|
|
private def takeToDollar(name: String) =
|
|
|
|
|
{
|
|
|
|
|
val dollar = name.indexOf('$')
|
|
|
|
|
if(dollar < 0) name else name.substring(0, dollar)
|
|
|
|
|
}
|
|
|
|
|
private final val ClassExt = ".class"
|
|
|
|
|
private def trimClassExt(name: String) = if(name.endsWith(ClassExt)) name.substring(0, name.length - ClassExt.length) else name
|
|
|
|
|
private def resolveClassFile(file: File, className: String): File = (file /: (className.replace('.','/') + ClassExt).split("/"))(new File(_, _))
|
2010-07-15 01:24:50 +02:00
|
|
|
private def guessSourcePath(sources: scala.collection.Set[Path], roots: Iterable[Path], classFile: ClassFile, log: Logger) =
|
2009-07-27 01:15:02 +02:00
|
|
|
{
|
2009-07-27 14:51:29 +02:00
|
|
|
val classNameParts = classFile.className.split("""\.""")
|
|
|
|
|
val lastIndex = classNameParts.length - 1
|
2009-07-27 15:52:38 +02:00
|
|
|
val pkg = classNameParts.take(lastIndex)
|
|
|
|
|
val simpleClassName = classNameParts(lastIndex)
|
2009-07-27 14:51:29 +02:00
|
|
|
val sourceFileName = classFile.sourceFile.getOrElse(simpleClassName.takeWhile(_ != '$').mkString("", "", ".java"))
|
|
|
|
|
val relativeSourceFile = (pkg ++ (sourceFileName :: Nil)).mkString("/")
|
2009-07-27 01:15:02 +02:00
|
|
|
val candidates = roots.map(root => Path.fromString(root, relativeSourceFile)).filter(sources.contains).toList
|
|
|
|
|
candidates match
|
|
|
|
|
{
|
2009-07-27 14:51:29 +02:00
|
|
|
case Nil => log.warn("Could not determine source for class " + classFile.className)
|
2009-07-27 01:15:02 +02:00
|
|
|
case head :: Nil => ()
|
2009-07-27 14:51:29 +02:00
|
|
|
case _ =>log.warn("Multiple sources matched for class " + classFile.className + ": " + candidates.mkString(", "))
|
2009-07-27 01:15:02 +02:00
|
|
|
}
|
|
|
|
|
candidates
|
|
|
|
|
}
|
|
|
|
|
private def isTopLevel(classFile: ClassFile) = classFile.className.indexOf('$') < 0
|
2010-03-28 06:05:40 +02:00
|
|
|
private lazy val unit = classOf[Unit]
|
|
|
|
|
private lazy val strArray = List(classOf[Array[String]])
|
|
|
|
|
|
|
|
|
|
private def isMain(method: Method): Boolean =
|
|
|
|
|
method.getName == "main" &&
|
|
|
|
|
isMain(method.getModifiers) &&
|
|
|
|
|
method.getReturnType == unit &&
|
|
|
|
|
method.getParameterTypes.toList == strArray
|
|
|
|
|
private def isMain(modifiers: Int): Boolean = (modifiers & mainModifiers) == mainModifiers && (modifiers & notMainModifiers) == 0
|
|
|
|
|
|
|
|
|
|
private val mainModifiers = STATIC | PUBLIC
|
|
|
|
|
private val notMainModifiers = ABSTRACT
|
2009-07-27 01:15:02 +02:00
|
|
|
}
|