-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathDocs.java
More file actions
77 lines (67 loc) · 2.35 KB
/
Docs.java
File metadata and controls
77 lines (67 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.javacs;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Logger;
import javax.tools.*;
public class Docs {
/** File manager with source-path + platform sources, which we will use to look up individual source files */
final SourceFileManager fileManager = new SourceFileManager();
Docs(Set<Path> docPath, Path javaHome) {
var srcZipPath = srcZip(javaHome);
// Path to source .jars + src.zip
var sourcePath = new ArrayList<Path>(docPath);
if (srcZipPath != NOT_FOUND) {
sourcePath.add(srcZipPath);
}
try {
fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcePath);
if (srcZipPath != NOT_FOUND) {
fileManager.setLocationFromPaths(StandardLocation.MODULE_SOURCE_PATH, Set.of(srcZipPath));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static final Path NOT_FOUND = Paths.get("");
private static Path cacheSrcZip;
private static Path srcZip(Path javaHome) {
if (cacheSrcZip == null) {
cacheSrcZip = findSrcZip(javaHome);
}
if (cacheSrcZip == NOT_FOUND) {
return NOT_FOUND;
}
try {
var fs = FileSystems.newFileSystem(cacheSrcZip, Docs.class.getClassLoader());
return fs.getPath("/");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static Path findSrcZip(Path javaHome) {
if (javaHome == NOT_FOUND) {
javaHome = JavaHomeHelper.javaHome();
}
if (javaHome == NOT_FOUND) {
LOG.warning("Couldn't find Java home.");
return NOT_FOUND;
}
String[] locations = {
"lib/src.zip", "src.zip", "libexec/openjdk.jdk/Contents/Home/lib/src.zip"
};
for (var rel : locations) {
var abs = javaHome.resolve(rel);
if (Files.exists(abs)) {
LOG.info("Found src.zip " + abs);
return abs;
}
}
LOG.warning("Couldn't find src.zip in " + javaHome);
return NOT_FOUND;
}
private static final Logger LOG = Logger.getLogger("main");
}