|
1 | 1 | @file:Suppress("JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE") |
2 | 2 | package lsifjava |
3 | 3 |
|
| 4 | +import com.sun.tools.javac.code.* |
4 | 5 | import com.sun.source.tree.* |
5 | 6 | import com.sun.source.util.DocTrees |
6 | 7 | import com.sun.source.util.JavacTask |
7 | 8 | import com.sun.source.util.TreePathScanner |
8 | | -import com.sun.tools.javac.util.Context |
9 | 9 | import com.sun.tools.javac.api.JavacTool |
10 | 10 | import com.sun.tools.javac.tree.JCTree.* |
| 11 | +import com.sun.tools.javac.util.Context |
11 | 12 | import java.nio.charset.Charset |
12 | | -import java.nio.file.Path |
| 13 | +import java.nio.file.* |
13 | 14 | import javax.lang.model.element.Element |
14 | | -import javax.tools.* |
| 15 | +import javax.tools.JavaFileObject |
| 16 | +import com.sun.tools.javac.file.JavacFileManager |
| 17 | +import javax.tools.StandardLocation |
15 | 18 |
|
16 | 19 | data class ExternalHoverMeta(val doc: String, val tree: Tree) |
17 | 20 |
|
18 | | -class ExternalDocs(private val docPaths: List<Path>) { |
| 21 | +private val emptyFileManager = SourceFileManager(emptySet()) |
19 | 22 |
|
| 23 | +class ExternalDocs(private val docPaths: List<Path>, private val diagnosticListener: CountingDiagnosticListener) { |
| 24 | + // we cache compilation unit trees here to reduce the number of times we have to invoke the parser |
20 | 25 | private val fileCache = HashMap<String, Pair<JavacTask, CompilationUnitTree>?>() |
21 | 26 |
|
22 | | - private val fileManager: StandardJavaFileManager by lazy { |
23 | | - val manager = ToolProvider.getSystemJavaCompiler() |
24 | | - .getStandardFileManager(null, null, Charset.defaultCharset()) |
| 27 | + private val fileManager = let { |
| 28 | + val manager = JavacFileManager(Context(), false, Charset.defaultCharset()) |
25 | 29 | manager.setLocation(StandardLocation.SOURCE_PATH, docPaths.map { it.toFile() }) |
26 | 30 | manager |
27 | 31 | } |
28 | 32 |
|
| 33 | + private val jdkFileManager = JDK8CompatFileManager(fileManager) |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns hover metadata for the tree section that matches the given element in the class file |
| 37 | + * associated with <code>containerClass</code>, which is expected to be the fully qualified name of the class file |
| 38 | + */ |
29 | 39 | fun findDocForElement(containerClass: String, javac: JavacTool, element: Element): ExternalHoverMeta? { |
30 | 40 | val context = DocumentIndexer.SimpleContext() |
31 | 41 |
|
32 | | - val (task, compUnit) = fileCache.getOrPut(containerClass) { analyzeFileFromJar(containerClass, context, javac) } |
33 | | - ?: return null |
| 42 | + val (task, compUnit) = fileCache.getOrPut(containerClass) { |
| 43 | + val fileObject = findFileFromJars(containerClass) ?: return@getOrPut null |
| 44 | + parseFileObject(fileObject, context, javac) |
| 45 | + } ?: return null |
34 | 46 |
|
35 | 47 | return DocExtractionVisitor(task, element).scan(compUnit, null) |
36 | 48 | } |
37 | 49 |
|
38 | | - private fun analyzeFileFromJar(containerClass: String, context: Context, javac: JavacTool): Pair<JavacTask, CompilationUnitTree>? { |
39 | | - val file = fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, containerClass, JavaFileObject.Kind.SOURCE) |
40 | | - ?: return null |
41 | | - val task = javac.getTask(NoopWriter, fileManager, CountingDiagnosticListener.NullWriter, listOf(), listOf(), listOf(file), context) |
42 | | - val compUnit = task.parse().iterator().next() |
43 | | - val analyzeResult = runCatching { task.analyze() } |
44 | | - analyzeResult.getOrNull() ?: run { |
45 | | - //println("${file.name} threw exception") |
46 | | - return null |
47 | | - } |
| 50 | + private fun findFileFromJars(containerClass: String): JavaFileObject? = |
| 51 | + fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, containerClass, JavaFileObject.Kind.SOURCE) |
| 52 | + ?: jdkFileManager.getJavaFileForInput(containerClass) |
| 53 | + |
| 54 | + /** |
| 55 | + * Performs basic parsing of the external dependency JavaFileObject and returns a (javac task, compilation unit) pair. |
| 56 | + * For reasons beyond my knowing (and many a staring at the debugger), an empty file manager |
| 57 | + */ |
| 58 | + private fun parseFileObject(file: JavaFileObject, context: Context, javac: JavacTool): Pair<JavacTask, CompilationUnitTree>? { |
| 59 | + val task = javac.getTask(NoopWriter, emptyFileManager, diagnosticListener, listOf(), listOf(), listOf(file), context) |
| 60 | + val compUnit = task.parse().iterator().apply { |
| 61 | + // bail out of the enclosing function |
| 62 | + if(!hasNext()) return null |
| 63 | + }.next() |
48 | 64 | return Pair(task, compUnit) |
49 | 65 | } |
50 | | - |
51 | | - private class DocExtractionVisitor(task: JavacTask, private val element: Element): TreePathScanner<ExternalHoverMeta?, Unit?>() { |
52 | | - private val docs: DocTrees = DocTrees.instance(task) |
53 | | - |
54 | | - override fun visitMethod(node: MethodTree?, p: Unit?): ExternalHoverMeta? { |
55 | | - (node as JCMethodDecl).sym ?: return null |
56 | 66 |
|
57 | | - if(node.sym.toString() == element.toString()) { |
| 67 | + private class DocExtractionVisitor( |
| 68 | + val task: JavacTask, |
| 69 | + private val element: Element, |
| 70 | + private val docs: DocTrees = DocTrees.instance(task) |
| 71 | + ): TreePathScanner<ExternalHoverMeta?, Unit?>() { |
| 72 | + // Basic flag to indicate if this DocExtractionVisitor has visited its assigned class decl yet. |
| 73 | + // If set to false, we want to create a new DocExtractionVisitor for the class decl we are visiting. |
| 74 | + // This way we can keep track of the owning class decl for methods/variables that are otherwise only |
| 75 | + // available with fully resolved symbols, which we don't get with non-full-fat parsing+analyzing |
| 76 | + private var new: Boolean = true |
| 77 | + private lateinit var classDecl: ClassTree |
| 78 | + |
| 79 | + override fun visitMethod(node: MethodTree, p: Unit?): ExternalHoverMeta? { |
| 80 | + if(element !is Symbol.MethodSymbol) return null |
| 81 | + |
| 82 | + if(element.owner.simpleName.toString() != classDecl.simpleName.toString()) |
| 83 | + return null |
| 84 | + |
| 85 | + if(element.name.toString() != node.name.toString()) return null |
| 86 | + |
| 87 | + if(element.name.toString() != "<init>" && |
| 88 | + element.returnType.toString() != node.returnType.toString()) return null |
| 89 | + |
| 90 | + val paramsEqual = element.params.size == node.parameters.size && |
| 91 | + element.params.foldIndexed(true) { i, acc, sym -> |
| 92 | + val paramType = (node.parameters[i] as JCVariableDecl).vartype |
| 93 | + val defTypeName = when(paramType) { |
| 94 | + is JCPrimitiveTypeTree -> paramType.toString() |
| 95 | + is JCTypeApply -> paramType.clazz.toString() |
| 96 | + is JCIdent -> paramType.toString() |
| 97 | + else -> { |
| 98 | + println("param type wasn't JCPrimitiveTypeTree|JCTypeApply|JCIdent, but ${paramType::class.java}") |
| 99 | + return null |
| 100 | + } |
| 101 | + } |
| 102 | + acc && sym.type.tsym.simpleName.toString() == defTypeName |
| 103 | + } |
| 104 | + |
| 105 | + if(paramsEqual) { |
58 | 106 | val doc = docs.getDocComment(currentPath) ?: return null |
59 | | - return ExternalHoverMeta(doc, node) |
| 107 | + return ExternalHoverMeta(doc.trim(), node) |
60 | 108 | } |
61 | 109 |
|
62 | 110 | return null |
63 | 111 | } |
64 | 112 |
|
65 | | - override fun visitVariable(node: VariableTree?, p: Unit?): ExternalHoverMeta? { |
66 | | - (node as JCVariableDecl).sym ?: return null |
| 113 | + override fun visitVariable(node: VariableTree, p: Unit?): ExternalHoverMeta? { |
| 114 | + if(element !is Symbol.VarSymbol) return null |
67 | 115 |
|
68 | | - if(node.sym.toString() == element.toString()) { |
| 116 | + if(element.owner.simpleName.toString() != classDecl.simpleName.toString()) |
| 117 | + return null |
69 | 118 |
|
| 119 | + if(element.name.toString() == node.name.toString()) { |
| 120 | + val doc = docs.getDocComment(currentPath) ?: return null |
| 121 | + return ExternalHoverMeta(doc.trim(), node) |
70 | 122 | } |
| 123 | + |
71 | 124 | // filter to instance variables |
72 | 125 | return null |
73 | 126 | } |
74 | 127 |
|
75 | | - override fun visitClass(node: ClassTree?, p: Unit?): ExternalHoverMeta? { |
76 | | - (node as JCClassDecl).sym ?: return null |
77 | | - |
78 | | - if(node.sym.toString() == element.toString()) { |
| 128 | + override fun visitClass(node: ClassTree, p: Unit?): ExternalHoverMeta? { |
| 129 | + // we need this logic here to stop calling scan on the same ClassTree infinitely, but we also |
| 130 | + // want to start a new visitor for each nested class decl |
| 131 | + if(!new) { |
| 132 | + return DocExtractionVisitor(task, element, docs).scan(node, null) |
| 133 | + } |
| 134 | + new = false |
| 135 | + classDecl = node |
| 136 | + |
| 137 | + if(element !is Symbol.ClassSymbol) return super.visitClass(node, p) |
| 138 | + |
| 139 | + // Assumption: no class-like name conflicts within a single class file |
| 140 | + if((node as JCClassDecl).name.toString() == element.simpleName.toString()) { |
79 | 141 | val doc = docs.getDocComment(currentPath) ?: return super.visitClass(node, p) |
80 | | - return ExternalHoverMeta(doc, node) |
| 142 | + return ExternalHoverMeta(doc.trim(), node) |
81 | 143 | } |
82 | 144 |
|
83 | 145 | return super.visitClass(node, p) |
|
0 commit comments