26 package com.sun.tools.javac.api; |
26 package com.sun.tools.javac.api; |
27 |
27 |
28 import java.io.FileNotFoundException; |
28 import java.io.FileNotFoundException; |
29 import java.io.IOException; |
29 import java.io.IOException; |
30 import java.text.BreakIterator; |
30 import java.text.BreakIterator; |
|
31 import java.util.HashMap; |
31 import java.util.HashSet; |
32 import java.util.HashSet; |
|
33 import java.util.Map; |
32 import java.util.Set; |
34 import java.util.Set; |
33 import java.util.regex.Matcher; |
35 import java.util.regex.Matcher; |
34 import java.util.regex.Pattern; |
36 import java.util.regex.Pattern; |
35 |
37 |
36 import javax.annotation.processing.ProcessingEnvironment; |
38 import javax.annotation.processing.ProcessingEnvironment; |
66 import com.sun.source.util.DocTreeScanner; |
68 import com.sun.source.util.DocTreeScanner; |
67 import com.sun.source.util.DocTrees; |
69 import com.sun.source.util.DocTrees; |
68 import com.sun.source.util.JavacTask; |
70 import com.sun.source.util.JavacTask; |
69 import com.sun.source.util.TreePath; |
71 import com.sun.source.util.TreePath; |
70 import com.sun.tools.javac.code.Flags; |
72 import com.sun.tools.javac.code.Flags; |
|
73 import com.sun.tools.javac.code.Scope.NamedImportScope; |
|
74 import com.sun.tools.javac.code.Scope.StarImportScope; |
|
75 import com.sun.tools.javac.code.Scope.WriteableScope; |
71 import com.sun.tools.javac.code.Symbol; |
76 import com.sun.tools.javac.code.Symbol; |
72 import com.sun.tools.javac.code.Symbol.ClassSymbol; |
77 import com.sun.tools.javac.code.Symbol.ClassSymbol; |
73 import com.sun.tools.javac.code.Symbol.MethodSymbol; |
78 import com.sun.tools.javac.code.Symbol.MethodSymbol; |
74 import com.sun.tools.javac.code.Symbol.PackageSymbol; |
79 import com.sun.tools.javac.code.Symbol.PackageSymbol; |
75 import com.sun.tools.javac.code.Symbol.TypeSymbol; |
80 import com.sun.tools.javac.code.Symbol.TypeSymbol; |
76 import com.sun.tools.javac.code.Symbol.VarSymbol; |
81 import com.sun.tools.javac.code.Symbol.VarSymbol; |
|
82 import com.sun.tools.javac.code.Symtab; |
77 import com.sun.tools.javac.code.Type; |
83 import com.sun.tools.javac.code.Type; |
78 import com.sun.tools.javac.code.Type.ArrayType; |
84 import com.sun.tools.javac.code.Type.ArrayType; |
79 import com.sun.tools.javac.code.Type.ClassType; |
85 import com.sun.tools.javac.code.Type.ClassType; |
80 import com.sun.tools.javac.code.Type.ErrorType; |
86 import com.sun.tools.javac.code.Type.ErrorType; |
81 import com.sun.tools.javac.code.Type.UnionClassType; |
87 import com.sun.tools.javac.code.Type.UnionClassType; |
101 import com.sun.tools.javac.tree.DCTree.DCErroneous; |
107 import com.sun.tools.javac.tree.DCTree.DCErroneous; |
102 import com.sun.tools.javac.tree.DCTree.DCIdentifier; |
108 import com.sun.tools.javac.tree.DCTree.DCIdentifier; |
103 import com.sun.tools.javac.tree.DCTree.DCParam; |
109 import com.sun.tools.javac.tree.DCTree.DCParam; |
104 import com.sun.tools.javac.tree.DCTree.DCReference; |
110 import com.sun.tools.javac.tree.DCTree.DCReference; |
105 import com.sun.tools.javac.tree.DCTree.DCText; |
111 import com.sun.tools.javac.tree.DCTree.DCText; |
|
112 import com.sun.tools.javac.tree.DocCommentTable; |
106 import com.sun.tools.javac.tree.DocTreeMaker; |
113 import com.sun.tools.javac.tree.DocTreeMaker; |
107 import com.sun.tools.javac.tree.EndPosTable; |
114 import com.sun.tools.javac.tree.EndPosTable; |
108 import com.sun.tools.javac.tree.JCTree; |
115 import com.sun.tools.javac.tree.JCTree; |
109 import com.sun.tools.javac.tree.JCTree.JCBlock; |
116 import com.sun.tools.javac.tree.JCTree.JCBlock; |
110 import com.sun.tools.javac.tree.JCTree.JCCatch; |
117 import com.sun.tools.javac.tree.JCTree.JCCatch; |
161 private Types types; |
168 private Types types; |
162 private DocTreeMaker docTreeMaker; |
169 private DocTreeMaker docTreeMaker; |
163 private BreakIterator breakIterator; |
170 private BreakIterator breakIterator; |
164 private JavaFileManager fileManager; |
171 private JavaFileManager fileManager; |
165 private ParserFactory parser; |
172 private ParserFactory parser; |
|
173 private Symtab syms; |
|
174 private Map<JavaFileObject, PackageSymbol> javaFileObjectToPackageMap; |
166 |
175 |
167 // called reflectively from Trees.instance(CompilationTask task) |
176 // called reflectively from Trees.instance(CompilationTask task) |
168 public static JavacTrees instance(JavaCompiler.CompilationTask task) { |
177 public static JavacTrees instance(JavaCompiler.CompilationTask task) { |
169 if (!(task instanceof BasicJavacTask)) |
178 if (!(task instanceof BasicJavacTask)) |
170 throw new IllegalArgumentException(); |
179 throw new IllegalArgumentException(); |
184 instance = new JavacTrees(context); |
193 instance = new JavacTrees(context); |
185 return instance; |
194 return instance; |
186 } |
195 } |
187 |
196 |
188 protected JavacTrees(Context context) { |
197 protected JavacTrees(Context context) { |
|
198 javaFileObjectToPackageMap = new HashMap<>(); |
189 this.breakIterator = null; |
199 this.breakIterator = null; |
190 context.put(JavacTrees.class, this); |
200 context.put(JavacTrees.class, this); |
191 init(context); |
201 init(context); |
192 } |
202 } |
193 |
203 |
205 memberEnter = MemberEnter.instance(context); |
215 memberEnter = MemberEnter.instance(context); |
206 names = Names.instance(context); |
216 names = Names.instance(context); |
207 types = Types.instance(context); |
217 types = Types.instance(context); |
208 docTreeMaker = DocTreeMaker.instance(context); |
218 docTreeMaker = DocTreeMaker.instance(context); |
209 parser = ParserFactory.instance(context); |
219 parser = ParserFactory.instance(context); |
|
220 syms = Symtab.instance(context); |
210 fileManager = context.get(JavaFileManager.class); |
221 fileManager = context.get(JavaFileManager.class); |
211 JavacTask t = context.get(JavacTask.class); |
222 JavacTask t = context.get(JavacTask.class); |
212 if (t instanceof JavacTaskImpl) |
223 if (t instanceof JavacTaskImpl) |
213 javacTaskImpl = (JavacTaskImpl) t; |
224 javacTaskImpl = (JavacTaskImpl) t; |
214 } |
225 } |
1016 } |
1027 } |
1017 |
1028 |
1018 @Override @DefinedBy(Api.COMPILER_TREE) |
1029 @Override @DefinedBy(Api.COMPILER_TREE) |
1019 public DocTreePath getDocTreePath(FileObject fileObject) { |
1030 public DocTreePath getDocTreePath(FileObject fileObject) { |
1020 JavaFileObject jfo = asJavaFileObject(fileObject); |
1031 JavaFileObject jfo = asJavaFileObject(fileObject); |
1021 return new DocTreePath(makeTreePath(jfo), getDocCommentTree(jfo)); |
1032 DocCommentTree docCommentTree = getDocCommentTree(jfo); |
|
1033 return new DocTreePath(makeTreePath(jfo, docCommentTree), docCommentTree); |
1022 } |
1034 } |
1023 |
1035 |
1024 @Override @DefinedBy(Api.COMPILER_TREE) |
1036 @Override @DefinedBy(Api.COMPILER_TREE) |
1025 public void setBreakIterator(BreakIterator breakiterator) { |
1037 public void setBreakIterator(BreakIterator breakiterator) { |
1026 this.breakIterator = breakiterator; |
1038 this.breakIterator = breakiterator; |
1134 } else { |
1146 } else { |
1135 return v.type; |
1147 return v.type; |
1136 } |
1148 } |
1137 } |
1149 } |
1138 |
1150 |
1139 private TreePath makeTreePath(final JavaFileObject jfo) { |
1151 /** |
|
1152 * Register a file object, such as for a package.html, that provides |
|
1153 * doc comments for a package. |
|
1154 * @param psym the PackageSymbol representing the package. |
|
1155 * @param jfo the JavaFileObject for the given package. |
|
1156 */ |
|
1157 public void putJavaFileObject(PackageSymbol psym, JavaFileObject jfo) { |
|
1158 javaFileObjectToPackageMap.putIfAbsent(jfo, psym); |
|
1159 } |
|
1160 |
|
1161 private TreePath makeTreePath(final JavaFileObject jfo, DocCommentTree dcTree) { |
1140 JCCompilationUnit jcCompilationUnit = new JCCompilationUnit(List.nil()) { |
1162 JCCompilationUnit jcCompilationUnit = new JCCompilationUnit(List.nil()) { |
1141 public int getPos() { |
1163 public int getPos() { |
1142 return Position.FIRSTPOS; |
1164 return Position.FIRSTPOS; |
1143 } |
1165 } |
1144 |
1166 |
1154 return Position.makeLineMap(s.toCharArray(), s.length(), true); |
1176 return Position.makeLineMap(s.toCharArray(), s.length(), true); |
1155 } catch (IOException ignore) {} |
1177 } catch (IOException ignore) {} |
1156 return null; |
1178 return null; |
1157 } |
1179 } |
1158 }; |
1180 }; |
|
1181 |
|
1182 PackageSymbol psym = javaFileObjectToPackageMap.getOrDefault(jfo, syms.unnamedPackage); |
|
1183 |
|
1184 jcCompilationUnit.docComments = new DocCommentTable() { |
|
1185 @Override |
|
1186 public boolean hasComment(JCTree tree) { |
|
1187 return false; |
|
1188 } |
|
1189 |
|
1190 @Override |
|
1191 public Comment getComment(JCTree tree) { |
|
1192 throw new UnsupportedOperationException(); |
|
1193 } |
|
1194 |
|
1195 @Override |
|
1196 public String getCommentText(JCTree tree) { |
|
1197 throw new UnsupportedOperationException(); |
|
1198 } |
|
1199 |
|
1200 @Override |
|
1201 public DCDocComment getCommentTree(JCTree tree) { |
|
1202 return (DCDocComment)dcTree; |
|
1203 } |
|
1204 |
|
1205 @Override |
|
1206 public void putComment(JCTree tree, Comment c) { |
|
1207 throw new UnsupportedOperationException(); |
|
1208 } |
|
1209 |
|
1210 }; |
|
1211 jcCompilationUnit.lineMap = jcCompilationUnit.getLineMap(); |
|
1212 jcCompilationUnit.namedImportScope = new NamedImportScope(psym, jcCompilationUnit.toplevelScope); |
|
1213 jcCompilationUnit.packge = psym; |
|
1214 jcCompilationUnit.starImportScope = null; |
1159 jcCompilationUnit.sourcefile = jfo; |
1215 jcCompilationUnit.sourcefile = jfo; |
1160 enter.main(List.of(jcCompilationUnit)); |
1216 jcCompilationUnit.starImportScope = new StarImportScope(psym); |
|
1217 jcCompilationUnit.toplevelScope = WriteableScope.create(psym); |
|
1218 |
1161 return new TreePath(jcCompilationUnit); |
1219 return new TreePath(jcCompilationUnit); |
1162 } |
1220 } |
1163 } |
1221 } |