Skip to content

Commit

Permalink
Merge branch '1.0.x' into merge-1.0.x-into-1.1.x
Browse files Browse the repository at this point in the history
* 1.0.x:
  Make PositionImpl thread-safe
  Support scala 2.13.0-pre-* & 2.13.0-M1
  • Loading branch information
dwijnand committed Dec 13, 2017
2 parents 366eaff + 3ccb6c4 commit 494fdda
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PositionImpl is a private class only invoked in the same source.
ProblemFilters.exclude[FinalClassProblem]("sbt.internal.inc.javac.DiagnosticsReporter$PositionImpl")
ProblemFilters.exclude[DirectMissingMethodProblem]("sbt.internal.inc.javac.DiagnosticsReporter#PositionImpl.this")
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import javax.tools.Diagnostic.NOPOS
* @param reporter
*/
final class DiagnosticsReporter(reporter: Reporter) extends DiagnosticListener[JavaFileObject] {
import DiagnosticsReporter._

val END_OF_LINE_MATCHER = "(\r\n)|[\r]|[\n]"
val EOL = System.getProperty("line.separator")

Expand Down Expand Up @@ -64,85 +66,118 @@ final class DiagnosticsReporter(reporter: Reporter) extends DiagnosticListener[J

import sbt.util.InterfaceUtil.problem
val msg = fixedDiagnosticMessage(d)
val pos: xsbti.Position = new PositionImpl(d)
val pos: xsbti.Position = PositionImpl(d)
if (severity == Severity.Error) errorEncountered = true
reporter.log(problem("", pos, msg, severity))
}
}

private class PositionImpl(d: Diagnostic[_ <: JavaFileObject]) extends xsbti.Position {
// https://docs.oracle.com/javase/7/docs/api/javax/tools/Diagnostic.html
// Negative values (except NOPOS) and 0 are not valid line or column numbers,
// except that you can cause this number to occur by putting "abc {}" in A.java.
// This will cause Invalid position: 0 masking the actual error message
// a/A.java:1: class, interface, or enum expected
private[this] def checkNoPos(n: Long): Option[Long] =
n match {
case NOPOS => None
case x if x <= 0 => None
case x => Option(x)
}

override val line: Optional[Integer] = o2jo(checkNoPos(d.getLineNumber) map { x =>
new Integer(x.toInt)
})
def startPosition: Option[Long] = checkNoPos(d.getStartPosition)
def endPosition: Option[Long] = checkNoPos(d.getEndPosition)
override val offset: Optional[Integer] = o2jo(checkNoPos(d.getPosition) map { x =>
new Integer(x.toInt)
})
override def lineContent: String = {
def getDiagnosticLine: Option[String] =
try {
// See com.sun.tools.javac.api.ClientCodeWrapper.DiagnosticSourceUnwrapper
val diagnostic = d.getClass.getField("d").get(d)
// See com.sun.tools.javac.util.JCDiagnostic#getDiagnosticSource
val getDiagnosticSourceMethod =
diagnostic.getClass.getDeclaredMethod("getDiagnosticSource")
val getPositionMethod = diagnostic.getClass.getDeclaredMethod("getPosition")
(Option(getDiagnosticSourceMethod.invoke(diagnostic)),
Option(getPositionMethod.invoke(diagnostic))) match {
case (Some(diagnosticSource), Some(position: java.lang.Long)) =>
// See com.sun.tools.javac.util.DiagnosticSource
val getLineMethod = diagnosticSource.getClass.getMethod("getLine", Integer.TYPE)
Option(getLineMethod.invoke(diagnosticSource, new Integer(position.intValue())))
.map(_.toString)
case _ => None
}
} catch {
// TODO - catch ReflectiveOperationException once sbt is migrated to JDK7
case _: Throwable => None
}

def getExpression: String =
Option(d.getSource) match {
case Some(source: JavaFileObject) =>
(Option(source.getCharContent(true)), startPosition, endPosition) match {
case (Some(cc), Some(start), Some(end)) =>
cc.subSequence(start.toInt, end.toInt).toString
case _ => ""
}
case _ => ""
}
object DiagnosticsReporter {

getDiagnosticLine.getOrElse(getExpression)
}
private val sourceUri: Option[String] = fixSource(d.getSource)
/**
* Strict and immutable implementation of Position.
*/
private[sbt] final class PositionImpl private (
sourceUri: Option[String],
override val line: Optional[Integer],
override val lineContent: String,
override val offset: Optional[Integer],
val startPosition: Option[Long],
val endPosition: Option[Long]
) extends xsbti.Position {
override val sourcePath: Optional[String] = o2jo(sourceUri)
override val sourceFile: Optional[File] = o2jo(sourceUri.map(new File(_)))
override val pointer: Optional[Integer] = o2jo(Option.empty[Integer])
override val pointerSpace: Optional[String] = o2jo(Option.empty[String])

override def toString: String =
if (sourceUri.isDefined) s"${sourceUri.get}:${if (line.isPresent) line.get else -1}"
else ""
private def fixSource[T <: JavaFileObject](source: T): Option[String] = {
try Option(source).map(_.toUri.normalize).map(new File(_)).map(_.getAbsolutePath)
catch {
case _: IllegalArgumentException =>
// Oracle JDK6 has a super dumb notion of what a URI is. In fact, it's not even a legimitate URL, but a dump
// of the filename in a "I hope this works to toString it" kind of way. This appears to work in practice
// but we may need to re-evaluate.
Option(source).map(_.toUri.toString)
}

private[sbt] object PositionImpl {

/**
* Extracts PositionImpl from a Java Diagnostic.
* The previous implementation of PositionImpl held on to the Diagostic object as a wrapper,
* and calculated the lineContent on the fly.
* This caused race condition on the Diagnostic object, resulting to NullPointerException.
* See https://github.com/sbt/sbt/issues/3623
*/
def apply(d: Diagnostic[_ <: JavaFileObject]): PositionImpl = {
// https://docs.oracle.com/javase/7/docs/api/javax/tools/Diagnostic.html
// Negative values (except NOPOS) and 0 are not valid line or column numbers,
// except that you can cause this number to occur by putting "abc {}" in A.java.
// This will cause Invalid position: 0 masking the actual error message
// a/A.java:1: class, interface, or enum expected
def checkNoPos(n: Long): Option[Long] =
n match {
case NOPOS => None
case x if x <= 0 => None
case x => Option(x)
}

val line: Optional[Integer] = o2jo(checkNoPos(d.getLineNumber) map { x =>
new Integer(x.toInt)
})
val startPosition: Option[Long] = checkNoPos(d.getStartPosition)
val endPosition: Option[Long] = checkNoPos(d.getEndPosition)
val offset: Optional[Integer] = o2jo(checkNoPos(d.getPosition) map { x =>
new Integer(x.toInt)
})
def lineContent: String = {
def getDiagnosticLine: Option[String] =
try {
// See com.sun.tools.javac.api.ClientCodeWrapper.DiagnosticSourceUnwrapper
val diagnostic = d.getClass.getField("d").get(d)
// See com.sun.tools.javac.util.JCDiagnostic#getDiagnosticSource
val getDiagnosticSourceMethod =
diagnostic.getClass.getDeclaredMethod("getDiagnosticSource")
val getPositionMethod = diagnostic.getClass.getDeclaredMethod("getPosition")
(Option(getDiagnosticSourceMethod.invoke(diagnostic)),
Option(getPositionMethod.invoke(diagnostic))) match {
case (Some(diagnosticSource), Some(position: java.lang.Long)) =>
// See com.sun.tools.javac.util.DiagnosticSource
val getLineMethod = diagnosticSource.getClass.getMethod("getLine", Integer.TYPE)
Option(getLineMethod.invoke(diagnosticSource, new Integer(position.intValue())))
.map(_.toString)
case _ => None
}
} catch {
// TODO - catch ReflectiveOperationException once sbt is migrated to JDK7
case _: Throwable => None
}

def getExpression: String =
Option(d.getSource) match {
case Some(source: JavaFileObject) =>
(Option(source.getCharContent(true)), startPosition, endPosition) match {
case (Some(cc), Some(start), Some(end)) =>
cc.subSequence(start.toInt, end.toInt).toString
case _ => ""
}
case _ => ""
}

getDiagnosticLine.getOrElse(getExpression)
}

def fixSource[T <: JavaFileObject](source: T): Option[String] = {
try Option(source).map(_.toUri.normalize).map(new File(_)).map(_.getAbsolutePath)
catch {
case _: IllegalArgumentException =>
// Oracle JDK6 has a super dumb notion of what a URI is. In fact, it's not even a legimitate URL, but a dump
// of the filename in a "I hope this works to toString it" kind of way. This appears to work in practice
// but we may need to re-evaluate.
Option(source).map(_.toUri.toString)
}
}
new PositionImpl(fixSource(d.getSource),
line,
lineContent,
offset,
startPosition,
endPosition)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ private[sbt] object ZincComponentCompiler {
private val CompileConf = Some(Configurations.Compile.name)
private[sbt] def getDefaultBridgeModule(scalaVersion: String): ModuleID = {
def compilerBridgeId(scalaVersion: String) = {
// Defaults to bridge for 2.12 for Scala versions bigger than 2.12.x
scalaVersion match {
case sc if (sc startsWith "2.10.") => "compiler-bridge_2.10"
case sc if (sc startsWith "2.11.") => "compiler-bridge_2.11"
case sc if (sc startsWith "2.12.") => "compiler-bridge_2.12"
case sc if (sc startsWith "2.13.0-M") => "compiler-bridge_2.13.0-M2"
case sc if (sc startsWith "2.13.0-RC") => "compiler-bridge_2.13.0-M2"
case _ => "compiler-bridge_2.13"
case sc if (sc startsWith "2.10.") => "compiler-bridge_2.10"
case sc if (sc startsWith "2.11.") => "compiler-bridge_2.11"
case sc if (sc startsWith "2.12.") => "compiler-bridge_2.12"
case "2.13.0-M1" => "compiler-bridge_2.12"
case sc if (sc startsWith "2.13.0-pre-") => "compiler-bridge_2.13.0-M2"
case sc if (sc startsWith "2.13.0-M") => "compiler-bridge_2.13.0-M2"
case sc if (sc startsWith "2.13.0-RC") => "compiler-bridge_2.13.0-M2"
case _ => "compiler-bridge_2.13"
}
}
import xsbti.ArtifactInfo.SbtOrganization
Expand Down

0 comments on commit 494fdda

Please sign in to comment.