author | ksrini |
Wed, 16 Apr 2014 18:15:48 -0700 | |
changeset 23973 | 4b5f3a297142 |
parent 23116 | 4c134ccd6b8d |
child 24604 | 7f68545b5128 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
23116
4c134ccd6b8d
8029145: javadoc fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
22163
diff
changeset
|
2 |
* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javadoc; |
|
27 |
||
1869 | 28 |
import java.io.File; |
29 |
import java.io.IOException; |
|
10 | 30 |
import java.util.Collection; |
1869 | 31 |
import java.util.EnumSet; |
32 |
import java.util.HashMap; |
|
23116
4c134ccd6b8d
8029145: javadoc fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
22163
diff
changeset
|
33 |
import java.util.HashSet; |
1869 | 34 |
import java.util.Map; |
35 |
import java.util.Set; |
|
36 |
import javax.tools.JavaFileManager.Location; |
|
37 |
import javax.tools.JavaFileObject; |
|
38 |
import javax.tools.StandardJavaFileManager; |
|
39 |
import javax.tools.StandardLocation; |
|
10 | 40 |
|
1869 | 41 |
import com.sun.tools.javac.code.Symbol.CompletionFailure; |
42 |
import com.sun.tools.javac.tree.JCTree; |
|
43 |
import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
|
44 |
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
|
45 |
import com.sun.tools.javac.util.Abort; |
|
46 |
import com.sun.tools.javac.util.Context; |
|
47 |
import com.sun.tools.javac.util.List; |
|
48 |
import com.sun.tools.javac.util.ListBuffer; |
|
49 |
import com.sun.tools.javac.util.Position; |
|
10 | 50 |
|
51 |
||
52 |
/** |
|
53 |
* This class could be the main entry point for Javadoc when Javadoc is used as a |
|
54 |
* component in a larger software system. It provides operations to |
|
55 |
* construct a new javadoc processor, and to run it on a set of source |
|
56 |
* files. |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
57 |
* |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
58 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
59 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
60 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
61 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
62 |
* |
10 | 63 |
* @author Neal Gafter |
64 |
*/ |
|
65 |
public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { |
|
66 |
DocEnv docenv; |
|
67 |
||
68 |
final Messager messager; |
|
14543 | 69 |
final JavadocClassReader javadocReader; |
70 |
final JavadocEnter javadocEnter; |
|
23973
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
71 |
final Set<JavaFileObject> uniquefiles; |
10 | 72 |
|
73 |
/** |
|
74 |
* Construct a new JavaCompiler processor, using appropriately |
|
75 |
* extended phases of the underlying compiler. |
|
76 |
*/ |
|
77 |
protected JavadocTool(Context context) { |
|
78 |
super(context); |
|
79 |
messager = Messager.instance0(context); |
|
14543 | 80 |
javadocReader = JavadocClassReader.instance0(context); |
81 |
javadocEnter = JavadocEnter.instance0(context); |
|
23973
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
82 |
uniquefiles = new HashSet<>(); |
10 | 83 |
} |
84 |
||
85 |
/** |
|
86 |
* For javadoc, the parser needs to keep comments. Overrides method from JavaCompiler. |
|
87 |
*/ |
|
88 |
protected boolean keepComments() { |
|
89 |
return true; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Construct a new javadoc tool. |
|
94 |
*/ |
|
95 |
public static JavadocTool make0(Context context) { |
|
96 |
Messager messager = null; |
|
97 |
try { |
|
98 |
// force the use of Javadoc's class reader |
|
99 |
JavadocClassReader.preRegister(context); |
|
100 |
||
101 |
// force the use of Javadoc's own enter phase |
|
102 |
JavadocEnter.preRegister(context); |
|
103 |
||
104 |
// force the use of Javadoc's own member enter phase |
|
105 |
JavadocMemberEnter.preRegister(context); |
|
106 |
||
107 |
// force the use of Javadoc's own todo phase |
|
108 |
JavadocTodo.preRegister(context); |
|
109 |
||
110 |
// force the use of Messager as a Log |
|
111 |
messager = Messager.instance0(context); |
|
112 |
||
113 |
return new JavadocTool(context); |
|
114 |
} catch (CompletionFailure ex) { |
|
115 |
messager.error(Position.NOPOS, ex.getMessage()); |
|
116 |
return null; |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
public RootDocImpl getRootDocImpl(String doclocale, |
|
121 |
String encoding, |
|
122 |
ModifierFilter filter, |
|
123 |
List<String> javaNames, |
|
124 |
List<String[]> options, |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
125 |
Iterable<? extends JavaFileObject> fileObjects, |
10 | 126 |
boolean breakiterator, |
127 |
List<String> subPackages, |
|
128 |
List<String> excludedPackages, |
|
129 |
boolean docClasses, |
|
130 |
boolean legacyDoclet, |
|
131 |
boolean quiet) throws IOException { |
|
132 |
docenv = DocEnv.instance(context); |
|
133 |
docenv.showAccess = filter; |
|
1869 | 134 |
docenv.quiet = quiet; |
10 | 135 |
docenv.breakiterator = breakiterator; |
136 |
docenv.setLocale(doclocale); |
|
137 |
docenv.setEncoding(encoding); |
|
138 |
docenv.docClasses = docClasses; |
|
139 |
docenv.legacyDoclet = legacyDoclet; |
|
19921
58bcbe156057
8023974: Drop 'implements Completer' and 'implements SourceCompleter' from ClassReader resp. JavaCompiler.
jfranck
parents:
14545
diff
changeset
|
140 |
javadocReader.sourceCompleter = docClasses ? null : thisCompleter; |
10 | 141 |
|
22163 | 142 |
ListBuffer<String> names = new ListBuffer<>(); |
143 |
ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<>(); |
|
144 |
ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<>(); |
|
10 | 145 |
|
146 |
try { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
147 |
StandardJavaFileManager fm = docenv.fileManager instanceof StandardJavaFileManager |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
148 |
? (StandardJavaFileManager) docenv.fileManager : null; |
10 | 149 |
for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) { |
150 |
String name = it.head; |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
151 |
if (!docClasses && fm != null && name.endsWith(".java") && new File(name).exists()) { |
1869 | 152 |
JavaFileObject fo = fm.getJavaFileObjects(name).iterator().next(); |
23973
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
153 |
parse(fo, classTrees, true); |
10 | 154 |
} else if (isValidPackageName(name)) { |
155 |
names = names.append(name); |
|
156 |
} else if (name.endsWith(".java")) { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
157 |
if (fm == null) |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
158 |
throw new IllegalArgumentException(); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
159 |
else |
14543 | 160 |
docenv.error(null, "main.file_not_found", name); |
10 | 161 |
} else { |
162 |
docenv.error(null, "main.illegal_package_name", name); |
|
163 |
} |
|
164 |
} |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
165 |
for (JavaFileObject fo: fileObjects) { |
23973
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
166 |
parse(fo, classTrees, true); |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
167 |
} |
10 | 168 |
|
169 |
if (!docClasses) { |
|
170 |
// Recursively search given subpackages. If any packages |
|
171 |
//are found, add them to the list. |
|
1869 | 172 |
Map<String,List<JavaFileObject>> packageFiles = |
173 |
searchSubPackages(subPackages, names, excludedPackages); |
|
10 | 174 |
|
175 |
// Parse the packages |
|
176 |
for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) { |
|
177 |
// Parse sources ostensibly belonging to package. |
|
1869 | 178 |
String packageName = packs.head; |
179 |
parsePackageClasses(packageName, packageFiles.get(packageName), packTrees, excludedPackages); |
|
10 | 180 |
} |
181 |
||
182 |
if (messager.nerrors() != 0) return null; |
|
183 |
||
184 |
// Enter symbols for all files |
|
185 |
docenv.notice("main.Building_tree"); |
|
14543 | 186 |
javadocEnter.main(classTrees.toList().appendList(packTrees.toList())); |
10 | 187 |
} |
188 |
} catch (Abort ex) {} |
|
189 |
||
1869 | 190 |
if (messager.nerrors() != 0) |
191 |
return null; |
|
10 | 192 |
|
193 |
if (docClasses) |
|
194 |
return new RootDocImpl(docenv, javaNames, options); |
|
195 |
else |
|
196 |
return new RootDocImpl(docenv, listClasses(classTrees.toList()), names.toList(), options); |
|
197 |
} |
|
198 |
||
199 |
/** Is the given string a valid package name? */ |
|
200 |
boolean isValidPackageName(String s) { |
|
201 |
int index; |
|
202 |
while ((index = s.indexOf('.')) != -1) { |
|
203 |
if (!isValidClassName(s.substring(0, index))) return false; |
|
204 |
s = s.substring(index+1); |
|
205 |
} |
|
206 |
return isValidClassName(s); |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* search all directories in path for subdirectory name. Add all |
|
211 |
* .java files found in such a directory to args. |
|
212 |
*/ |
|
213 |
private void parsePackageClasses(String name, |
|
23973
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
214 |
List<JavaFileObject> files, |
1869 | 215 |
ListBuffer<JCCompilationUnit> trees, |
216 |
List<String> excludedPackages) |
|
217 |
throws IOException { |
|
10 | 218 |
if (excludedPackages.contains(name)) { |
219 |
return; |
|
220 |
} |
|
1869 | 221 |
|
10 | 222 |
docenv.notice("main.Loading_source_files_for_package", name); |
1869 | 223 |
|
224 |
if (files == null) { |
|
225 |
Location location = docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH) |
|
226 |
? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; |
|
22163 | 227 |
ListBuffer<JavaFileObject> lb = new ListBuffer<>(); |
1869 | 228 |
for (JavaFileObject fo: docenv.fileManager.list( |
229 |
location, name, EnumSet.of(JavaFileObject.Kind.SOURCE), false)) { |
|
230 |
String binaryName = docenv.fileManager.inferBinaryName(location, fo); |
|
231 |
String simpleName = getSimpleName(binaryName); |
|
232 |
if (isValidClassName(simpleName)) { |
|
233 |
lb.append(fo); |
|
10 | 234 |
} |
235 |
} |
|
1869 | 236 |
files = lb.toList(); |
10 | 237 |
} |
23973
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
238 |
if (files.nonEmpty()) { |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
239 |
for (JavaFileObject fo : files) { |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
240 |
parse(fo, trees, false); |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
241 |
} |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
242 |
} else { |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
243 |
messager.warning(Messager.NOPOS, "main.no_source_files_for_package", |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
244 |
name.replace(File.separatorChar, '.')); |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
245 |
} |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
246 |
} |
1869 | 247 |
|
23973
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
248 |
private void parse(JavaFileObject fo, ListBuffer<JCCompilationUnit> trees, |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
249 |
boolean trace) { |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
250 |
if (uniquefiles.add(fo)) { // ignore duplicates |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
251 |
if (trace) |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
252 |
docenv.notice("main.Loading_source_file", fo.getName()); |
4b5f3a297142
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents:
23116
diff
changeset
|
253 |
trees.append(parse(fo)); |
1869 | 254 |
} |
10 | 255 |
} |
256 |
||
257 |
/** |
|
258 |
* Recursively search all directories in path for subdirectory name. |
|
259 |
* Add all packages found in such a directory to packages list. |
|
260 |
*/ |
|
1869 | 261 |
private Map<String,List<JavaFileObject>> searchSubPackages( |
262 |
List<String> subPackages, |
|
263 |
ListBuffer<String> packages, |
|
264 |
List<String> excludedPackages) |
|
265 |
throws IOException { |
|
22163 | 266 |
Map<String,List<JavaFileObject>> packageFiles = new HashMap<>(); |
1869 | 267 |
|
22163 | 268 |
Map<String,Boolean> includedPackages = new HashMap<>(); |
1869 | 269 |
includedPackages.put("", true); |
270 |
for (String p: excludedPackages) |
|
271 |
includedPackages.put(p, false); |
|
272 |
||
10635 | 273 |
StandardLocation path = docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH) |
274 |
? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; |
|
275 |
||
276 |
searchSubPackages(subPackages, |
|
277 |
includedPackages, |
|
278 |
packages, packageFiles, |
|
279 |
path, |
|
280 |
EnumSet.of(JavaFileObject.Kind.SOURCE)); |
|
281 |
||
1869 | 282 |
return packageFiles; |
283 |
} |
|
284 |
||
10 | 285 |
private void searchSubPackages(List<String> subPackages, |
1869 | 286 |
Map<String,Boolean> includedPackages, |
287 |
ListBuffer<String> packages, |
|
288 |
Map<String, List<JavaFileObject>> packageFiles, |
|
289 |
StandardLocation location, Set<JavaFileObject.Kind> kinds) |
|
290 |
throws IOException { |
|
291 |
for (String subPackage: subPackages) { |
|
292 |
if (!isIncluded(subPackage, includedPackages)) |
|
293 |
continue; |
|
10 | 294 |
|
1869 | 295 |
for (JavaFileObject fo: docenv.fileManager.list(location, subPackage, kinds, true)) { |
296 |
String binaryName = docenv.fileManager.inferBinaryName(location, fo); |
|
297 |
String packageName = getPackageName(binaryName); |
|
298 |
String simpleName = getSimpleName(binaryName); |
|
299 |
if (isIncluded(packageName, includedPackages) && isValidClassName(simpleName)) { |
|
300 |
List<JavaFileObject> list = packageFiles.get(packageName); |
|
301 |
list = (list == null ? List.of(fo) : list.prepend(fo)); |
|
302 |
packageFiles.put(packageName, list); |
|
303 |
if (!packages.contains(packageName)) |
|
304 |
packages.add(packageName); |
|
305 |
} |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
private String getPackageName(String name) { |
|
311 |
int lastDot = name.lastIndexOf("."); |
|
312 |
return (lastDot == -1 ? "" : name.substring(0, lastDot)); |
|
313 |
} |
|
314 |
||
315 |
private String getSimpleName(String name) { |
|
316 |
int lastDot = name.lastIndexOf("."); |
|
317 |
return (lastDot == -1 ? name : name.substring(lastDot + 1)); |
|
318 |
} |
|
319 |
||
320 |
private boolean isIncluded(String packageName, Map<String,Boolean> includedPackages) { |
|
321 |
Boolean b = includedPackages.get(packageName); |
|
322 |
if (b == null) { |
|
323 |
b = isIncluded(getPackageName(packageName), includedPackages); |
|
324 |
includedPackages.put(packageName, b); |
|
325 |
} |
|
326 |
return b; |
|
10 | 327 |
} |
328 |
||
329 |
/** |
|
330 |
* Recursively search all directories in path for subdirectory name. |
|
331 |
* Add all packages found in such a directory to packages list. |
|
332 |
*/ |
|
333 |
private void searchSubPackage(String packageName, |
|
334 |
ListBuffer<String> packages, |
|
335 |
List<String> excludedPackages, |
|
336 |
Collection<File> pathnames) { |
|
337 |
if (excludedPackages.contains(packageName)) |
|
338 |
return; |
|
339 |
||
340 |
String packageFilename = packageName.replace('.', File.separatorChar); |
|
341 |
boolean addedPackage = false; |
|
342 |
for (File pathname : pathnames) { |
|
343 |
File f = new File(pathname, packageFilename); |
|
344 |
String filenames[] = f.list(); |
|
345 |
// if filenames not null, then found directory |
|
346 |
if (filenames != null) { |
|
347 |
for (String filename : filenames) { |
|
348 |
if (!addedPackage |
|
349 |
&& (isValidJavaSourceFile(filename) || |
|
350 |
isValidJavaClassFile(filename)) |
|
351 |
&& !packages.contains(packageName)) { |
|
352 |
packages.append(packageName); |
|
353 |
addedPackage = true; |
|
354 |
} else if (isValidClassName(filename) && |
|
355 |
(new File(f, filename)).isDirectory()) { |
|
356 |
searchSubPackage(packageName + "." + filename, |
|
357 |
packages, excludedPackages, pathnames); |
|
358 |
} |
|
359 |
} |
|
360 |
} |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Return true if given file name is a valid class file name. |
|
366 |
* @param file the name of the file to check. |
|
367 |
* @return true if given file name is a valid class file name |
|
368 |
* and false otherwise. |
|
369 |
*/ |
|
370 |
private static boolean isValidJavaClassFile(String file) { |
|
371 |
if (!file.endsWith(".class")) return false; |
|
372 |
String clazzName = file.substring(0, file.length() - ".class".length()); |
|
373 |
return isValidClassName(clazzName); |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Return true if given file name is a valid Java source file name. |
|
378 |
* @param file the name of the file to check. |
|
379 |
* @return true if given file name is a valid Java source file name |
|
380 |
* and false otherwise. |
|
381 |
*/ |
|
382 |
private static boolean isValidJavaSourceFile(String file) { |
|
383 |
if (!file.endsWith(".java")) return false; |
|
384 |
String clazzName = file.substring(0, file.length() - ".java".length()); |
|
385 |
return isValidClassName(clazzName); |
|
386 |
} |
|
387 |
||
388 |
/** Are surrogates supported? |
|
389 |
*/ |
|
390 |
final static boolean surrogatesSupported = surrogatesSupported(); |
|
391 |
private static boolean surrogatesSupported() { |
|
392 |
try { |
|
393 |
boolean b = Character.isHighSurrogate('a'); |
|
394 |
return true; |
|
395 |
} catch (NoSuchMethodError ex) { |
|
396 |
return false; |
|
397 |
} |
|
398 |
} |
|
399 |
||
400 |
/** |
|
401 |
* Return true if given file name is a valid class name |
|
402 |
* (including "package-info"). |
|
14259 | 403 |
* @param s the name of the class to check. |
10 | 404 |
* @return true if given class name is a valid class name |
405 |
* and false otherwise. |
|
406 |
*/ |
|
407 |
public static boolean isValidClassName(String s) { |
|
408 |
if (s.length() < 1) return false; |
|
409 |
if (s.equals("package-info")) return true; |
|
410 |
if (surrogatesSupported) { |
|
411 |
int cp = s.codePointAt(0); |
|
412 |
if (!Character.isJavaIdentifierStart(cp)) |
|
413 |
return false; |
|
414 |
for (int j=Character.charCount(cp); j<s.length(); j+=Character.charCount(cp)) { |
|
415 |
cp = s.codePointAt(j); |
|
416 |
if (!Character.isJavaIdentifierPart(cp)) |
|
417 |
return false; |
|
418 |
} |
|
419 |
} else { |
|
420 |
if (!Character.isJavaIdentifierStart(s.charAt(0))) |
|
421 |
return false; |
|
422 |
for (int j=1; j<s.length(); j++) |
|
423 |
if (!Character.isJavaIdentifierPart(s.charAt(j))) |
|
424 |
return false; |
|
425 |
} |
|
426 |
return true; |
|
427 |
} |
|
428 |
||
429 |
/** |
|
430 |
* From a list of top level trees, return the list of contained class definitions |
|
431 |
*/ |
|
432 |
List<JCClassDecl> listClasses(List<JCCompilationUnit> trees) { |
|
22163 | 433 |
ListBuffer<JCClassDecl> result = new ListBuffer<>(); |
10 | 434 |
for (JCCompilationUnit t : trees) { |
435 |
for (JCTree def : t.defs) { |
|
10950 | 436 |
if (def.hasTag(JCTree.Tag.CLASSDEF)) |
10 | 437 |
result.append((JCClassDecl)def); |
438 |
} |
|
439 |
} |
|
440 |
return result.toList(); |
|
441 |
} |
|
442 |
||
443 |
} |