|
2 | 2 | package lsifjava |
3 | 3 |
|
4 | 4 | import com.sun.tools.javac.util.Context |
5 | | -import java.io.BufferedReader |
6 | | -import java.io.File |
7 | 5 | import java.io.IOException |
8 | | -import java.io.InputStreamReader |
9 | 6 | import java.nio.charset.Charset |
10 | 7 | import java.nio.file.* |
11 | | -import java.util.concurrent.TimeUnit |
12 | | -import java.util.stream.Collectors |
13 | 8 | import javax.tools.* |
14 | 9 |
|
| 10 | +// hard-coded list for convenience. Sorry, George :) |
15 | 11 | private val JDK_MODULES = listOf( |
16 | 12 | "java.activation", |
17 | 13 | "java.base", |
@@ -179,90 +175,4 @@ class JDK8CompatFileManager(manager: StandardJavaFileManager): ForwardingJavaFil |
179 | 175 |
|
180 | 176 | return null |
181 | 177 | } |
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 | | - } |
268 | 178 | } |
0 commit comments