Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/ToolEnvironment.java
41161 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.javadoc.internal.tool;262728import java.util.*;2930import javax.lang.model.element.Element;31import javax.lang.model.element.TypeElement;32import javax.lang.model.util.Elements;33import javax.tools.JavaFileManager;34import javax.tools.JavaFileObject;35import javax.tools.JavaFileObject.Kind;3637import com.sun.source.util.DocTrees;38import com.sun.source.util.TreePath;39import com.sun.tools.javac.api.JavacTrees;40import com.sun.tools.javac.code.ClassFinder;41import com.sun.tools.javac.code.Flags;42import com.sun.tools.javac.code.Source;43import com.sun.tools.javac.code.Symbol;44import com.sun.tools.javac.code.Symbol.ClassSymbol;45import com.sun.tools.javac.code.Symbol.CompletionFailure;46import com.sun.tools.javac.code.Symbol.ModuleSymbol;47import com.sun.tools.javac.code.Symtab;48import com.sun.tools.javac.comp.AttrContext;49import com.sun.tools.javac.comp.Check;50import com.sun.tools.javac.comp.Enter;51import com.sun.tools.javac.comp.Env;52import com.sun.tools.javac.file.JavacFileManager;53import com.sun.tools.javac.model.JavacElements;54import com.sun.tools.javac.model.JavacTypes;55import com.sun.tools.javac.tree.JCTree;56import com.sun.tools.javac.tree.JCTree.JCClassDecl;57import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;58import com.sun.tools.javac.tree.JCTree.JCPackageDecl;59import com.sun.tools.javac.util.Context;60import com.sun.tools.javac.util.Convert;61import com.sun.tools.javac.util.Name;62import com.sun.tools.javac.util.Names;6364/**65* Holds the environment for a run of javadoc.66* Holds only the information needed throughout the67* run and not the compiler info that could be GC'ed68* or ported.69*70* <p><b>This is NOT part of any supported API.71* If you write code that depends on this, you do so at your own risk.72* This code and its internal interfaces are subject to change or73* deletion without notice.</b>74*/75public class ToolEnvironment {76protected static final Context.Key<ToolEnvironment> ToolEnvKey = new Context.Key<>();7778public static ToolEnvironment instance(Context context) {79ToolEnvironment instance = context.get(ToolEnvKey);80if (instance == null)81instance = new ToolEnvironment(context);82return instance;83}8485final Messager messager;8687/** Predefined symbols known to the compiler. */88public final Symtab syms;8990/** JavaDoc's subtype of the compiler's class finder */91private final ClassFinder finder;9293/** Javadoc's subtype of the compiler's enter phase. */94final Enter enter;9596/** The name table. */97private Names names;9899/** If true, prevent printing of any notifications. */100boolean quiet = false;101102/** If true, ignore all errors encountered during Enter. */103boolean ignoreSourceErrors = false;104105Check chk;106com.sun.tools.javac.code.Types types;107JavaFileManager fileManager;108public final Context context;109110WeakHashMap<JCTree, TreePath> treePaths = new WeakHashMap<>();111112/** Allow documenting from class files? */113boolean docClasses = false;114115/**116* The source language version.117*/118public final Source source;119120public final Elements elements;121122public final JavacTypes typeutils;123124protected DocEnvImpl docEnv;125126public final DocTrees docTrees;127128public final Map<Element, TreePath> elementToTreePath;129130/**131* Constructor132*133* @param context Context for this javadoc instance.134*/135protected ToolEnvironment(Context context) {136context.put(ToolEnvKey, this);137this.context = context;138139messager = Messager.instance0(context);140syms = Symtab.instance(context);141finder = JavadocClassFinder.instance(context);142enter = JavadocEnter.instance(context);143names = Names.instance(context);144chk = Check.instance(context);145types = com.sun.tools.javac.code.Types.instance(context);146fileManager = context.get(JavaFileManager.class);147if (fileManager instanceof JavacFileManager jfm) {148jfm.setSymbolFileEnabled(false);149}150docTrees = JavacTrees.instance(context);151source = Source.instance(context);152elements = JavacElements.instance(context);153typeutils = JavacTypes.instance(context);154elementToTreePath = new HashMap<>();155}156157public void initialize(ToolOptions options) {158this.quiet = options.quiet();159this.ignoreSourceErrors = options.ignoreSourceErrors();160}161162/**163* Load a class by qualified name.164*/165public TypeElement loadClass(String name) {166try {167Name nameImpl = names.fromString(name);168ModuleSymbol mod = syms.inferModule(Convert.packagePart(nameImpl));169ClassSymbol c = finder.loadClass(mod != null ? mod : syms.errModule, nameImpl);170return c;171} catch (CompletionFailure ex) {172chk.completionError(null, ex);173return null;174}175}176177boolean isSynthetic(Symbol sym) {178return (sym.flags() & Flags.SYNTHETIC) != 0;179}180181void setElementToTreePath(Element e, TreePath tree) {182if (e == null || tree == null)183return;184elementToTreePath.put(e, tree);185}186187public Kind getFileKind(TypeElement te) {188JavaFileObject jfo = ((ClassSymbol)te).outermostClass().classfile;189return jfo == null ? Kind.SOURCE : jfo.getKind();190}191192/**193* Print a notice, iff <em>quiet</em> is not specified.194*195* @param key selects message from resource196*/197public void notice(String key) {198if (quiet) {199return;200}201messager.noticeUsingKey(key);202}203204/**205* Print a notice, iff <em>quiet</em> is not specified.206*207* @param key selects message from resource208* @param a1 first argument209*/210public void notice(String key, String a1) {211if (quiet) {212return;213}214messager.noticeUsingKey(key, a1);215}216217TreePath getTreePath(JCCompilationUnit tree) {218TreePath p = treePaths.get(tree);219if (p == null)220treePaths.put(tree, p = new TreePath(tree));221return p;222}223224TreePath getTreePath(JCCompilationUnit toplevel, JCPackageDecl tree) {225TreePath p = treePaths.get(tree);226if (p == null)227treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));228return p;229}230231TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl tree) {232TreePath p = treePaths.get(tree);233if (p == null)234treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));235return p;236}237238TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl cdecl, JCTree tree) {239return new TreePath(getTreePath(toplevel, cdecl), tree);240}241242public com.sun.tools.javac.code.Types getTypes() {243return types;244}245246public Env<AttrContext> getEnv(ClassSymbol tsym) {247return enter.getEnv(tsym);248}249250public boolean isQuiet() {251return quiet;252}253}254255256