Skip to content

Commit eb612ff

Browse files
committed
Parse errors from deps now honour the users chosen output
1 parent fbc62d5 commit eb612ff

File tree

5 files changed

+103
-95
lines changed

5 files changed

+103
-95
lines changed

src/main/kotlin/lsifjava/ExternalDocs.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ data class ExternalHoverMeta(val doc: String, val tree: Tree)
2020

2121
private val emptyFileManager = SourceFileManager(emptySet())
2222

23-
class ExternalDocs(private val docPaths: List<Path>) {
23+
class ExternalDocs(private val docPaths: List<Path>, val diagnosticListener: CountingDiagnosticListener) {
2424
private val fileCache = HashMap<String, Pair<JavacTask, CompilationUnitTree>?>()
2525

2626
private val fileManager = let {
@@ -47,7 +47,7 @@ class ExternalDocs(private val docPaths: List<Path>) {
4747
private fun analyzeFileFromJar(containerClass: String, context: Context, javac: JavacTool): Pair<JavacTask, CompilationUnitTree>? {
4848
val file = findFileFromJars(containerClass) ?: return null
4949

50-
val task = javac.getTask(NoopWriter, emptyFileManager, CountingDiagnosticListener.NullWriter, listOf(), listOf(), listOf(file), context)
50+
val task = javac.getTask(NoopWriter, emptyFileManager, diagnosticListener, listOf(), listOf(), listOf(file), context)
5151
val compUnit = task.parse().iterator().apply {
5252
if(!hasNext()) return null
5353
}.next()

src/main/kotlin/lsifjava/FileCollector.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fun buildIndexerMap(
2727
val sourceVersions = buildToolInterface.javaSourceVersions
2828

2929
// TODO(nsc) where to move this
30-
val externalDocManager = ExternalDocs(buildToolInterface.sourcesList)
30+
val externalDocManager = ExternalDocs(buildToolInterface.sourcesList, javacDiagListener)
3131

3232
val fileBuildInfo = Channel<FileBuildInfo>()
3333

src/main/kotlin/lsifjava/JDK8CompatFileManager.kt

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
package lsifjava
33

44
import com.sun.tools.javac.util.Context
5-
import java.io.BufferedReader
6-
import java.io.File
75
import java.io.IOException
8-
import java.io.InputStreamReader
96
import java.nio.charset.Charset
107
import java.nio.file.*
11-
import java.util.concurrent.TimeUnit
12-
import java.util.stream.Collectors
138
import javax.tools.*
149

10+
// hard-coded list for convenience. Sorry, George :)
1511
private val JDK_MODULES = listOf(
1612
"java.activation",
1713
"java.base",
@@ -179,90 +175,4 @@ class JDK8CompatFileManager(manager: StandardJavaFileManager): ForwardingJavaFil
179175

180176
return null
181177
}
182-
183-
// from https://github.com/georgewfraser/java-language-server
184-
// bless you george for all the references. Maybe Ill cut this down/refactor
185-
private object JavaHomeHelper {
186-
fun javaHome(): Path? {
187-
System.getenv("JAVA_HOME")?.let {
188-
return Paths.get(it)
189-
}
190-
val osName = System.getProperty("os.name")
191-
if (isWindows(osName)) return windowsJavaHome()
192-
if (isMac(osName)) return macJavaHome()
193-
if (isLinux(osName)) return linuxJavaHome()
194-
throw RuntimeException("Unrecognized os.name $osName")
195-
}
196-
197-
private fun windowsJavaHome(): Path? {
198-
for (root in File.listRoots()) {
199-
val x64 = root.toPath().resolve("Program Files/Java").toString()
200-
val x86 = root.toPath().resolve("Program Files (x86)/Java").toString()
201-
val found = check(x64, x86)
202-
if (found !== null) return found
203-
}
204-
return null
205-
}
206-
207-
private fun macJavaHome(): Path? {
208-
if (Files.isExecutable(Paths.get("/usr/libexec/java_home"))) {
209-
return execJavaHome()
210-
}
211-
val homes = arrayOf(
212-
"/Library/Java/JavaVirtualMachines/Home",
213-
"/System/Library/Java/JavaVirtualMachines/Home",
214-
"/Library/Java/JavaVirtualMachines/Contents/Home",
215-
"/System/Library/Java/JavaVirtualMachines/Contents/Home")
216-
return check(*homes)
217-
}
218-
219-
private fun linuxJavaHome(): Path? {
220-
val homes = arrayOf("/usr/java", "/opt/java", "/usr/lib/jvm")
221-
return check(*homes)
222-
}
223-
224-
private fun execJavaHome(): Path {
225-
return try {
226-
val process = ProcessBuilder().command("/usr/libexec/java_home").start()
227-
val out = BufferedReader(InputStreamReader(process.inputStream))
228-
val line = out.readLine()
229-
process.waitFor(5, TimeUnit.SECONDS)
230-
Paths.get(line)
231-
} catch (e: IOException) {
232-
throw RuntimeException(e)
233-
} catch (e: InterruptedException) {
234-
throw RuntimeException(e)
235-
}
236-
}
237-
238-
private fun check(vararg roots: String): Path? {
239-
for (root in roots) {
240-
val list: List<Path> = try {
241-
Files.list(Paths.get(root)).collect(Collectors.toList())
242-
} catch (e: NoSuchFileException) {
243-
continue
244-
} catch (e: IOException) {
245-
throw RuntimeException(e)
246-
}
247-
for (jdk in list) {
248-
if (Files.exists(jdk.resolve("bin/javac")) || Files.exists(jdk.resolve("bin/javac.exe"))) {
249-
return jdk
250-
}
251-
}
252-
}
253-
return null
254-
}
255-
256-
private fun isWindows(osName: String): Boolean {
257-
return osName.toLowerCase().startsWith("windows")
258-
}
259-
260-
private fun isMac(osName: String): Boolean {
261-
return osName.toLowerCase().startsWith("mac")
262-
}
263-
264-
private fun isLinux(osName: String): Boolean {
265-
return osName.toLowerCase().startsWith("linux")
266-
}
267-
}
268178
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package lsifjava
2+
3+
import java.io.BufferedReader
4+
import java.io.File
5+
import java.io.IOException
6+
import java.io.InputStreamReader
7+
import java.nio.file.Files
8+
import java.nio.file.NoSuchFileException
9+
import java.nio.file.Path
10+
import java.nio.file.Paths
11+
import java.util.concurrent.TimeUnit
12+
import java.util.stream.Collectors
13+
14+
// from https://github.com/georgewfraser/java-language-server
15+
// bless you george for all the references. Maybe Ill cut this down/refactor
16+
object JavaHomeHelper {
17+
fun javaHome(): Path? {
18+
System.getenv("JAVA_HOME")?.let {
19+
return Paths.get(it)
20+
}
21+
val osName = System.getProperty("os.name")
22+
if (isWindows(osName)) return windowsJavaHome()
23+
if (isMac(osName)) return macJavaHome()
24+
if (isLinux(osName)) return linuxJavaHome()
25+
throw RuntimeException("Unrecognized os.name $osName")
26+
}
27+
28+
private fun windowsJavaHome(): Path? {
29+
for (root in File.listRoots()) {
30+
val x64 = root.toPath().resolve("Program Files/Java").toString()
31+
val x86 = root.toPath().resolve("Program Files (x86)/Java").toString()
32+
val found = check(x64, x86)
33+
if (found !== null) return found
34+
}
35+
return null
36+
}
37+
38+
private fun macJavaHome(): Path? {
39+
if (Files.isExecutable(Paths.get("/usr/libexec/java_home"))) {
40+
return execJavaHome()
41+
}
42+
val homes = arrayOf(
43+
"/Library/Java/JavaVirtualMachines/Home",
44+
"/System/Library/Java/JavaVirtualMachines/Home",
45+
"/Library/Java/JavaVirtualMachines/Contents/Home",
46+
"/System/Library/Java/JavaVirtualMachines/Contents/Home")
47+
return check(*homes)
48+
}
49+
50+
private fun linuxJavaHome(): Path? {
51+
val homes = arrayOf("/usr/java", "/opt/java", "/usr/lib/jvm")
52+
return check(*homes)
53+
}
54+
55+
private fun execJavaHome(): Path {
56+
return try {
57+
val process = ProcessBuilder().command("/usr/libexec/java_home").start()
58+
val out = BufferedReader(InputStreamReader(process.inputStream))
59+
val line = out.readLine()
60+
process.waitFor(5, TimeUnit.SECONDS)
61+
Paths.get(line)
62+
} catch (e: IOException) {
63+
throw RuntimeException(e)
64+
} catch (e: InterruptedException) {
65+
throw RuntimeException(e)
66+
}
67+
}
68+
69+
private fun check(vararg roots: String): Path? {
70+
for (root in roots) {
71+
val list: List<Path> = try {
72+
Files.list(Paths.get(root)).collect(Collectors.toList())
73+
} catch (e: NoSuchFileException) {
74+
continue
75+
} catch (e: IOException) {
76+
throw RuntimeException(e)
77+
}
78+
for (jdk in list) {
79+
if (Files.exists(jdk.resolve("bin/javac")) || Files.exists(jdk.resolve("bin/javac.exe"))) {
80+
return jdk
81+
}
82+
}
83+
}
84+
return null
85+
}
86+
87+
private fun isWindows(osName: String): Boolean {
88+
return osName.toLowerCase().startsWith("windows")
89+
}
90+
91+
private fun isMac(osName: String): Boolean {
92+
return osName.toLowerCase().startsWith("mac")
93+
}
94+
95+
private fun isLinux(osName: String): Boolean {
96+
return osName.toLowerCase().startsWith("linux")
97+
}
98+
}

src/main/kotlin/lsifjava/Main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.io.PrintWriter
66
val javaVersion: Int = getVersion()
77

88
fun main(args: Array<String>) {
9-
println("Running JVM ${System.getProperty("java.version")}")
9+
println("Running JVM ${System.getProperty("java.version")}, JAVA_HOME is set to ${JavaHomeHelper.javaHome() ?: ""}")
1010

1111
val arguments = parse(args)
1212
val writer = createWriter(arguments)

0 commit comments

Comments
 (0)