package jdk.javadoc.internal.tool;
import java.util.*;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import com.sun.source.util.DocTrees;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.ClassFinder;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Check;
import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.model.JavacTypes;
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.tree.JCTree.JCPackageDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Convert;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
public class ToolEnvironment {
protected static final Context.Key<ToolEnvironment> ToolEnvKey = new Context.Key<>();
public static ToolEnvironment instance(Context context) {
ToolEnvironment instance = context.get(ToolEnvKey);
if (instance == null)
instance = new ToolEnvironment(context);
return instance;
}
final Messager messager;
public final Symtab syms;
private final ClassFinder finder;
final Enter enter;
private Names names;
final Symbol externalizableSym;
boolean quiet = false;
boolean ignoreSourceErrors = false;
Check chk;
com.sun.tools.javac.code.Types types;
JavaFileManager fileManager;
public final Context context;
WeakHashMap<JCTree, TreePath> treePaths = new WeakHashMap<>();
boolean docClasses = false;
public final Source source;
public final Elements elements;
public final JavacTypes typeutils;
protected DocEnvImpl docEnv;
public final DocTrees docTrees;
public final Map<Element, TreePath> elementToTreePath;
protected ToolEnvironment(Context context) {
context.put(ToolEnvKey, this);
this.context = context;
messager = Messager.instance0(context);
syms = Symtab.instance(context);
finder = JavadocClassFinder.instance(context);
enter = JavadocEnter.instance(context);
names = Names.instance(context);
externalizableSym = syms.enterClass(syms.java_base, names.fromString("java.io.Externalizable"));
chk = Check.instance(context);
types = com.sun.tools.javac.code.Types.instance(context);
fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof JavacFileManager) {
((JavacFileManager)fileManager).setSymbolFileEnabled(false);
}
docTrees = JavacTrees.instance(context);
source = Source.instance(context);
elements = JavacElements.instance(context);
typeutils = JavacTypes.instance(context);
elementToTreePath = new HashMap<>();
}
public void initialize(ToolOptions options) {
this.quiet = options.quiet();
this.ignoreSourceErrors = options.ignoreSourceErrors();
}
public TypeElement loadClass(String name) {
try {
Name nameImpl = names.fromString(name);
ModuleSymbol mod = syms.inferModule(Convert.packagePart(nameImpl));
ClassSymbol c = finder.loadClass(mod != null ? mod : syms.errModule, nameImpl);
return c;
} catch (CompletionFailure ex) {
chk.completionError(null, ex);
return null;
}
}
boolean isSynthetic(Symbol sym) {
return (sym.flags() & Flags.SYNTHETIC) != 0;
}
void setElementToTreePath(Element e, TreePath tree) {
if (e == null || tree == null)
return;
elementToTreePath.put(e, tree);
}
public Kind getFileKind(TypeElement te) {
JavaFileObject jfo = ((ClassSymbol)te).outermostClass().classfile;
return jfo == null ? Kind.SOURCE : jfo.getKind();
}
public void notice(String key) {
if (quiet) {
return;
}
messager.notice(key);
}
public void notice(String key, String a1) {
if (quiet) {
return;
}
messager.notice(key, a1);
}
TreePath getTreePath(JCCompilationUnit tree) {
TreePath p = treePaths.get(tree);
if (p == null)
treePaths.put(tree, p = new TreePath(tree));
return p;
}
TreePath getTreePath(JCCompilationUnit toplevel, JCPackageDecl tree) {
TreePath p = treePaths.get(tree);
if (p == null)
treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));
return p;
}
TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl tree) {
TreePath p = treePaths.get(tree);
if (p == null)
treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));
return p;
}
TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl cdecl, JCTree tree) {
return new TreePath(getTreePath(toplevel, cdecl), tree);
}
public com.sun.tools.javac.code.Types getTypes() {
return types;
}
public Env<AttrContext> getEnv(ClassSymbol tsym) {
return enter.getEnv(tsym);
}
public boolean isQuiet() {
return quiet;
}
}