Skip to content

Commit

Permalink
Fix handling deeply nested java classes.
Browse files Browse the repository at this point in the history
Without fix it reports dependencies to jar/classes produced by same project.
  • Loading branch information
romanowski committed Oct 10, 2017
1 parent 8c4b996 commit e792676
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ private[sbt] object Analyze {
binaryClassName = classFile.className;
loadedClass <- load(binaryClassName, Some("Error reading API from class file"))) {
binaryClassNameToLoadedClass.update(binaryClassName, loadedClass)
val srcClassName = binaryToSourceName(loadedClass)
.orElse(Option(loadedClass.getEnclosingClass).flatMap(binaryToSourceName))

def loadEnclosingClass(clazz: Class[_]): Option[String] = {
binaryToSourceName(clazz) match {
case None if clazz.getEnclosingClass != null =>
loadEnclosingClass(clazz.getEnclosingClass)
case other => other
}
}

val srcClassName = loadEnclosingClass(loadedClass)

srcClassName match {
case Some(className) =>
Expand Down
15 changes: 15 additions & 0 deletions zinc/src/test/resources/sources/NestedJavaClasses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class NestedJavaClasses {
public NestedJavaClasses nest() {
return new NestedJavaClasses() {
@Override
public NestedJavaClasses nest() {
return new NestedJavaClasses() {
@Override
public NestedJavaClasses nest() {
throw new RuntimeException("Way too deep!");
}
};
}
};
}
}
20 changes: 20 additions & 0 deletions zinc/src/test/scala/sbt/inc/NestedJavaClassSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sbt.inc

import sbt.internal.inc.Analysis
import sbt.io.IO

class NestedJavaClassSpec extends BaseCompilerSpec {
it should "handle nested Java classes" in {
IO.withTemporaryDirectory { tempDir =>
val projectSetup =
ProjectSetup.simple(tempDir.toPath, Seq("NestedJavaClasses.java"))

val result = projectSetup.createCompiler().doCompile()
result.analysis() match {
case analysis: Analysis =>
analysis.relations.libraryDep._2s
.filter(_.toPath.startsWith(tempDir.toPath)) shouldBe 'empty
}
}
}
}

0 comments on commit e792676

Please sign in to comment.