diff --git a/.jvmopts b/.jvmopts index 7fb1642e..cc6fa202 100644 --- a/.jvmopts +++ b/.jvmopts @@ -1,4 +1,4 @@ -Xss2m -Xms1G -Xmx4G --Dfile.encoding=UTF-8 \ No newline at end of file +-Dfile.encoding=UTF-8 diff --git a/bin/coursier b/bin/coursier index e1a8faf7..a664bdf8 100755 Binary files a/bin/coursier and b/bin/coursier differ diff --git a/build.sbt b/build.sbt index 7fab94c5..1bfd888a 100644 --- a/build.sbt +++ b/build.sbt @@ -4,6 +4,7 @@ import java.io.File import java.nio.file.Files import java.util.Properties import scala.collection.mutable.ListBuffer +import scala.util.control.NoStackTrace lazy val V = new { @@ -271,12 +272,9 @@ commands += def minimizedSourceDirectory = file("tests/minimized/src/main/java").getAbsoluteFile -lazy val minimizedSettings = List[Def.Setting[_]]( - autoScalaLibrary := false, - (publish / skip) := true, - (run / fork) := true, - (Compile / unmanagedSourceDirectories) += minimizedSourceDirectory, - (Compile / javacOptions) ++= + +def semanticdbCompileOptions = + Def.setting { List[String]( s"-Arandomtimestamp=${System.nanoTime()}", List( @@ -288,6 +286,13 @@ lazy val minimizedSettings = List[Def.Setting[_]]( s"-targetroot:${(Compile / semanticdbTargetRoot).value}" ).mkString(" ") ) + } +lazy val minimizedSettings = List[Def.Setting[_]]( + autoScalaLibrary := false, + (publish / skip) := true, + (run / fork) := true, + (Compile / unmanagedSourceDirectories) += minimizedSourceDirectory, + (Compile / javacOptions) ++= semanticdbCompileOptions.value ) lazy val minimized = project @@ -302,13 +307,65 @@ lazy val minimized8 = project .dependsOn(agent, plugin) .disablePlugins(JavaFormatterPlugin) +def javaModuleExports = + List( + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" + ) + +def compileWithJava17 = + Def.task { + val args = ListBuffer.empty[String] + val javaSources = (minimized8 / Compile / sources) + .value + .map(_.getAbsolutePath) + val javac = (Compile / javaHome) + .value + .get + .toPath + .resolve("bin") + .resolve("javac") + .toString + val compileClasspath = List( + "-classpath", + (Compile / dependencyClasspath) + .value + .map(_.data.getAbsolutePath) + .mkString(java.io.File.pathSeparator) + ) + val outputDirectory = (Compile / classDirectory).value + IO.delete(outputDirectory) + + args += javac + args ++= javaModuleExports + args ++= List("-d", outputDirectory.getAbsolutePath) + args ++= semanticdbCompileOptions.value + args ++= compileClasspath + args ++= javaSources + val exit = scala.sys.process.Process(args.toList).! + if (exit != 0) { + throw new RuntimeException("javac compilation failed") with NoStackTrace + } + } + lazy val minimized17 = project - .in(file("tests/minimized/.j15")) + .in(file("tests/minimized/.j17")) .settings( minimizedSettings, - // Non-zulu release is blocked by https://github.com/coursier/jvm-index/pull/53 - javaToolchainVersion := "zulu:17", - Compile / javaHome := None + Compile / sources := Nil, + javaToolchainJvmIndex := + Some("https://github.com/coursier/jvm-index/blob/master/index.json"), + javaToolchainVersion := "temurin:17", + Compile / compile := + (Compile / compile).dependsOn(compileWithJava17).value ) .dependsOn(agent, plugin) .disablePlugins(JavaFormatterPlugin) diff --git a/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/JavaVersion.java b/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/JavaVersion.java index d07276a6..b125f702 100644 --- a/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/JavaVersion.java +++ b/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/JavaVersion.java @@ -15,14 +15,18 @@ public class JavaVersion { public final boolean isJava8; public final JdkPackage pkg; - private static PathMatcher CLASS_PATTERN = + private static final PathMatcher CLASS_PATTERN = FileSystems.getDefault().getPathMatcher("glob:**.class"); - private static PathMatcher JAR_PATTERN = FileSystems.getDefault().getPathMatcher("glob:**.jar"); + private static final PathMatcher JAR_PATTERN = + FileSystems.getDefault().getPathMatcher("glob:**.jar"); - public static int JAVA8_VERSION = 8; - public static int JAVA11_VERSION = 11; - public static int DEFAULT_JAVA_VERSION = JAVA8_VERSION; - private static int JAVA0_MAJOR_VERSION = 44; + public static final int JAVA8_VERSION = 8; + public static final int JAVA11_VERSION = 11; + public static final int JAVA17_VERSION = 17; + public static final int DEFAULT_JAVA_VERSION = JAVA8_VERSION; + + @SuppressWarnings("FieldCanBeLocal") + private static final int JAVA0_MAJOR_VERSION = 44; public JavaVersion() { this(System.getProperty("java.version")); @@ -40,9 +44,11 @@ private String javaVersion(String version) { else return version; } + @SuppressWarnings("ManualMinMaxCalculation") public static int roundToNearestStableRelease(int version) { if (version <= JAVA8_VERSION) return JAVA8_VERSION; if (version <= JAVA11_VERSION) return JAVA11_VERSION; + if (version <= JAVA17_VERSION) return JAVA17_VERSION; return version; } diff --git a/project/JavaToolchainPlugin.scala b/project/JavaToolchainPlugin.scala index 0b9c47fd..3de13a14 100644 --- a/project/JavaToolchainPlugin.scala +++ b/project/JavaToolchainPlugin.scala @@ -20,6 +20,9 @@ object JavaToolchainPlugin extends AutoPlugin { lazy val javaToolchainVersion = settingKey[String]( "The version of the Java" ) + lazy val javaToolchainJvmIndex = settingKey[Option[String]]( + "The JVM index to use" + ) } import autoImport._ @@ -36,14 +39,21 @@ object JavaToolchainPlugin extends AutoPlugin { (doc / javacOptions) --= List("-target", "1.8"), (doc / javacOptions) --= bootclasspathSettings(javaToolchainVersion.value), (doc / javacOptions) --= List("-g"), - javaHome := Some(getJavaHome(javaToolchainVersion.value)), + javaHome := + Some( + getJavaHome(javaToolchainVersion.value, javaToolchainJvmIndex.value) + ), javacOptions ++= bootclasspathSettings(javaToolchainVersion.value), javaOptions ++= bootclasspathSettings(javaToolchainVersion.value) ) override lazy val projectSettings: Seq[Def.Setting[_]] = List(Compile, Test).flatMap(c => inConfig(c)(configSettings)) ++ - List(fork := true, javaToolchainVersion := "11") + List( + fork := true, + javaToolchainVersion := "11", + javaToolchainJvmIndex := None + ) /** * For Java 8, we need to manually add the Java compiler to the boot @@ -70,14 +80,20 @@ object JavaToolchainPlugin extends AutoPlugin { private val javaHomeCache: util.Map[String, File] = Collections .synchronizedMap(new util.HashMap[String, File]()) - private def getJavaHome(version: String): File = { + private def getJavaHome( + version: String, + jvmIndex: Option[String] = None + ): File = { javaHomeCache.computeIfAbsent( version, (v: String) => { val coursier = Paths.get("bin", "coursier") - new File( - Process(List(coursier.toString, "java-home", "--jvm", v)).!!.trim - ) + val index = jvmIndex + .toList + .flatMap(index => "--jvm-index" :: index :: Nil) + val arguments = + List(coursier.toString, "java-home", "--jvm", v) ++ index + new File(Process(arguments).!!.trim) } ) } diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/GlobalSymbolsCache.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/GlobalSymbolsCache.java index 2af332e6..2e6985e3 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/GlobalSymbolsCache.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/GlobalSymbolsCache.java @@ -3,6 +3,7 @@ import com.sun.tools.javac.code.Symbol; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import java.util.*; @@ -57,7 +58,14 @@ private String uncachedSemanticdbSymbol(Symbol sym, LocalSymbolsCache locals) { } private boolean isLocalVariable(Symbol sym) { - return sym instanceof Symbol.VarSymbol && sym.isLocal(); + switch (sym.getKind()) { + case PARAMETER: + case EXCEPTION_PARAMETER: + case LOCAL_VARIABLE: + return true; + default: + return false; + } } private boolean isAnonymousClass(Symbol sym) {