package com.sun.tools.javadoc.main;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import com.sun.tools.javac.code.ClassFinder;
import com.sun.tools.javac.code.Symbol.Completer;
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.Abort;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
DocEnv docenv;
final Messager messager;
final ClassFinder javadocFinder;
final Enter javadocEnter;
final Set<JavaFileObject> uniquefiles;
protected JavadocTool(Context context) {
super(context);
messager = Messager.instance0(context);
javadocFinder = JavadocClassFinder.instance(context);
javadocEnter = JavadocEnter.instance(context);
uniquefiles = new HashSet<>();
}
@Override
protected boolean keepComments() {
return true;
}
public static JavadocTool make0(Context context) {
JavadocClassFinder.preRegister(context);
JavadocEnter.preRegister(context);
JavadocMemberEnter.preRegister(context);
JavadocTodo.preRegister(context);
Messager.instance0(context);
return new JavadocTool(context);
}
public RootDocImpl getRootDocImpl(String doclocale,
String encoding,
ModifierFilter filter,
List<String> args,
List<String[]> options,
Iterable<? extends JavaFileObject> fileObjects,
boolean breakiterator,
List<String> subPackages,
List<String> excludedPackages,
boolean docClasses,
boolean legacyDoclet,
boolean quiet) throws IOException {
docenv = DocEnv.instance(context);
docenv.showAccess = filter;
docenv.quiet = quiet;
docenv.breakiterator = breakiterator;
docenv.setLocale(doclocale);
docenv.setEncoding(encoding);
docenv.docClasses = docClasses;
docenv.legacyDoclet = legacyDoclet;
javadocFinder.sourceCompleter = docClasses ? Completer.NULL_COMPLETER : sourceCompleter;
if (docClasses) {
for (String arg: args) {
if (!isValidPackageName(arg))
docenv.error(null, "main.illegal_class_name", arg);
}
if (messager.nerrors() != 0) {
return null;
}
return new RootDocImpl(docenv, args, options);
}
ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<>();
Set<String> includedPackages = new LinkedHashSet<>();
try {
StandardJavaFileManager fm = docenv.fileManager instanceof StandardJavaFileManager
? (StandardJavaFileManager) docenv.fileManager : null;
Set<String> packageNames = new LinkedHashSet<>();
for (String arg: args) {
if (fm != null && arg.endsWith(".java") && new File(arg).exists()) {
if (new File(arg).getName().equals("module-info.java")) {
docenv.warning(null, "main.file_ignored", arg);
} else {
parse(fm.getJavaFileObjects(arg), classTrees, true);
}
} else if (isValidPackageName(arg)) {
packageNames.add(arg);
} else if (arg.endsWith(".java")) {
if (fm == null)
throw new IllegalArgumentException();
else
docenv.error(null, "main.file_not_found", arg);
} else {
docenv.error(null, "main.illegal_package_name", arg);
}
}
parse(fileObjects, classTrees, true);
modules.initModules(classTrees.toList());
Location location = modules.multiModuleMode ? StandardLocation.MODULE_SOURCE_PATH
: docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH) ? StandardLocation.SOURCE_PATH
: StandardLocation.CLASS_PATH;
PackageTable t = new PackageTable(docenv.fileManager, location)
.packages(packageNames)
.subpackages(subPackages, excludedPackages);
includedPackages = t.getIncludedPackages();
ListBuffer<JCCompilationUnit> packageTrees = new ListBuffer<>();
for (String packageName: includedPackages) {
List<JavaFileObject> files = t.getFiles(packageName);
docenv.notice("main.Loading_source_files_for_package", packageName);
if (files.isEmpty())
messager.warning(Messager.NOPOS, "main.no_source_files_for_package", packageName);
parse(files, packageTrees, false);
}
modules.enter(packageTrees.toList(), null);
if (messager.nerrors() != 0) {
return null;
}
docenv.notice("main.Building_tree");
javadocEnter.main(classTrees.toList().appendList(packageTrees.toList()));
} catch (Abort ex) {}
if (messager.nerrors() != 0)
return null;
return new RootDocImpl(docenv, listClasses(classTrees.toList()), List.from(includedPackages), options);
}
boolean isValidPackageName(String s) {
int index;
while ((index = s.indexOf('.')) != -1) {
if (!isValidClassName(s.substring(0, index))) return false;
s = s.substring(index+1);
}
return isValidClassName(s);
}
private void parse(Iterable<? extends JavaFileObject> files, ListBuffer<JCCompilationUnit> trees,
boolean trace) {
for (JavaFileObject fo: files) {
if (uniquefiles.add(fo)) {
if (trace)
docenv.notice("main.Loading_source_file", fo.getName());
trees.append(parse(fo));
}
}
}
final static boolean surrogatesSupported = surrogatesSupported();
private static boolean surrogatesSupported() {
try {
boolean b = Character.isHighSurrogate('a');
return true;
} catch (NoSuchMethodError ex) {
return false;
}
}
public static boolean isValidClassName(String s) {
if (s.length() < 1) return false;
if (s.equals("package-info")) return true;
if (surrogatesSupported) {
int cp = s.codePointAt(0);
if (!Character.isJavaIdentifierStart(cp))
return false;
for (int j=Character.charCount(cp); j<s.length(); j+=Character.charCount(cp)) {
cp = s.codePointAt(j);
if (!Character.isJavaIdentifierPart(cp))
return false;
}
} else {
if (!Character.isJavaIdentifierStart(s.charAt(0)))
return false;
for (int j=1; j<s.length(); j++)
if (!Character.isJavaIdentifierPart(s.charAt(j)))
return false;
}
return true;
}
List<JCClassDecl> listClasses(List<JCCompilationUnit> trees) {
ListBuffer<JCClassDecl> result = new ListBuffer<>();
for (JCCompilationUnit t : trees) {
for (JCTree def : t.defs) {
if (def.hasTag(JCTree.Tag.CLASSDEF))
result.append((JCClassDecl)def);
}
}
return result.toList();
}
class PackageTable {
private final Map<String, Entry> entries = new LinkedHashMap<>();
private final Set<String> includedPackages = new LinkedHashSet<>();
private final JavaFileManager fm;
private final Location location;
private final Set<JavaFileObject.Kind> sourceKinds = EnumSet.of(JavaFileObject.Kind.SOURCE);
PackageTable(JavaFileManager fm, Location locn) {
this.fm = fm;
this.location = locn;
getEntry("").excluded = false;
}
PackageTable packages(Collection<String> packageNames) {
includedPackages.addAll(packageNames);
return this;
}
PackageTable subpackages(Collection<String> packageNames, Collection<String> excludePackageNames)
throws IOException {
for (String p: excludePackageNames) {
getEntry(p).excluded = true;
}
for (String packageName: packageNames) {
Location packageLocn = getLocation(packageName);
for (JavaFileObject fo: fm.list(packageLocn, packageName, sourceKinds, true)) {
String binaryName = fm.inferBinaryName(packageLocn, fo);
String pn = getPackageName(binaryName);
String simpleName = getSimpleName(binaryName);
Entry e = getEntry(pn);
if (!e.isExcluded() && isValidClassName(simpleName)) {
includedPackages.add(pn);
e.files = (e.files == null ? List.of(fo) : e.files.prepend(fo));
}
}
}
return this;
}
Set<String> getIncludedPackages() {
return includedPackages;
}
List<JavaFileObject> getFiles(String packageName) throws IOException {
Entry e = getEntry(packageName);
if (e.files != null)
return e.files;
ListBuffer<JavaFileObject> lb = new ListBuffer<>();
Location packageLocn = getLocation(packageName);
for (JavaFileObject fo: fm.list(packageLocn, packageName, sourceKinds, false)) {
String binaryName = fm.inferBinaryName(packageLocn, fo);
String simpleName = getSimpleName(binaryName);
if (isValidClassName(simpleName)) {
lb.append(fo);
}
}
return lb.toList();
}
private Location getLocation(String packageName) throws IOException {
if (location == StandardLocation.MODULE_SOURCE_PATH) {
Name pack = names.fromString(packageName);
for (ModuleSymbol msym : modules.allModules()) {
PackageSymbol p = syms.getPackage(msym, pack);
if (p != null && !p.members().isEmpty()) {
return fm.getLocationForModule(location, msym.name.toString());
}
}
return null;
} else {
return location;
}
}
private Entry getEntry(String name) {
Entry e = entries.get(name);
if (e == null)
entries.put(name, e = new Entry(name));
return e;
}
private String getPackageName(String name) {
int lastDot = name.lastIndexOf(".");
return (lastDot == -1 ? "" : name.substring(0, lastDot));
}
private String getSimpleName(String name) {
int lastDot = name.lastIndexOf(".");
return (lastDot == -1 ? name : name.substring(lastDot + 1));
}
class Entry {
final String name;
Boolean excluded;
List<JavaFileObject> files;
Entry(String name) {
this.name = name;
}
boolean isExcluded() {
if (excluded == null)
excluded = getEntry(getPackageName(name)).isExcluded();
return excluded;
}
}
}
}