ToolEnvironment.java revision 3900:02e61db8289d
1272343Sngie/*
2272343Sngie * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
3272343Sngie * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4272343Sngie *
5272343Sngie * 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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26package jdk.javadoc.internal.tool;
27
28
29import java.util.*;
30
31import javax.lang.model.element.Element;
32import javax.lang.model.element.TypeElement;
33import javax.lang.model.util.Elements;
34import javax.tools.JavaFileManager;
35import javax.tools.JavaFileObject;
36import javax.tools.JavaFileObject.Kind;
37
38import com.sun.source.util.DocTrees;
39import com.sun.source.util.TreePath;
40import com.sun.tools.javac.api.JavacTrees;
41import com.sun.tools.javac.code.ClassFinder;
42import com.sun.tools.javac.code.Flags;
43import com.sun.tools.javac.code.Source;
44import com.sun.tools.javac.code.Symbol;
45import com.sun.tools.javac.code.Symbol.ClassSymbol;
46import com.sun.tools.javac.code.Symbol.CompletionFailure;
47import com.sun.tools.javac.code.Symbol.ModuleSymbol;
48import com.sun.tools.javac.code.Symtab;
49import com.sun.tools.javac.comp.AttrContext;
50import com.sun.tools.javac.comp.Check;
51import com.sun.tools.javac.comp.Enter;
52import com.sun.tools.javac.comp.Env;
53import com.sun.tools.javac.file.JavacFileManager;
54import com.sun.tools.javac.model.JavacElements;
55import com.sun.tools.javac.model.JavacTypes;
56import com.sun.tools.javac.tree.JCTree;
57import com.sun.tools.javac.tree.JCTree.JCClassDecl;
58import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
59import com.sun.tools.javac.tree.JCTree.JCPackageDecl;
60import com.sun.tools.javac.util.Context;
61import com.sun.tools.javac.util.Convert;
62import com.sun.tools.javac.util.Name;
63import com.sun.tools.javac.util.Names;
64
65/**
66 * Holds the environment for a run of javadoc.
67 * Holds only the information needed throughout the
68 * run and not the compiler info that could be GC'ed
69 * or ported.
70 *
71 *  <p><b>This is NOT part of any supported API.
72 *  If you write code that depends on this, you do so at your own risk.
73 *  This code and its internal interfaces are subject to change or
74 *  deletion without notice.</b>
75 *
76 * @author Robert Field
77 * @author Neal Gafter (rewrite)
78 * @author Scott Seligman (generics)
79 */
80public class ToolEnvironment {
81    protected static final Context.Key<ToolEnvironment> ToolEnvKey = new Context.Key<>();
82
83    public static ToolEnvironment instance(Context context) {
84        ToolEnvironment instance = context.get(ToolEnvKey);
85        if (instance == null)
86            instance = new ToolEnvironment(context);
87        return instance;
88    }
89
90    final Messager messager;
91
92    /** Predefined symbols known to the compiler. */
93    public final Symtab syms;
94
95    /** Referenced directly in RootDocImpl. */
96    private final ClassFinder finder;
97
98    /** Javadoc's own version of the compiler's enter phase. */
99    final Enter enter;
100
101    /** The name table. */
102    private Names names;
103
104    final Symbol externalizableSym;
105
106    /**
107     * True if we do not want to print any notifications at all.
108     */
109    boolean quiet = false;
110
111    Check chk;
112    com.sun.tools.javac.code.Types types;
113    JavaFileManager fileManager;
114    public final Context context;
115
116    WeakHashMap<JCTree, TreePath> treePaths = new WeakHashMap<>();
117
118    /** Allow documenting from class files? */
119    boolean docClasses = false;
120
121    /**
122     * The source language version.
123     */
124    public final Source source;
125
126    public final Elements elements;
127
128    public final JavacTypes typeutils;
129
130    protected DocEnvImpl docEnv;
131
132    public final DocTrees docTrees;
133
134    public final Map<Element, TreePath> elementToTreePath;
135
136    /**
137     * Constructor
138     *
139     * @param context      Context for this javadoc instance.
140     */
141    protected ToolEnvironment(Context context) {
142        context.put(ToolEnvKey, this);
143        this.context = context;
144
145        messager = Messager.instance0(context);
146        syms = Symtab.instance(context);
147        finder = JavadocClassFinder.instance(context);
148        enter = JavadocEnter.instance(context);
149        names = Names.instance(context);
150        externalizableSym = syms.enterClass(syms.java_base, names.fromString("java.io.Externalizable"));
151        chk = Check.instance(context);
152        types = com.sun.tools.javac.code.Types.instance(context);
153        fileManager = context.get(JavaFileManager.class);
154        if (fileManager instanceof JavacFileManager) {
155            ((JavacFileManager)fileManager).setSymbolFileEnabled(false);
156        }
157        docTrees = JavacTrees.instance(context);
158        source = Source.instance(context);
159        elements =  JavacElements.instance(context);
160        typeutils = JavacTypes.instance(context);
161        elementToTreePath = new HashMap<>();
162    }
163
164    public void initialize(Map<ToolOption, Object> toolOpts) {
165        this.quiet = (boolean)toolOpts.getOrDefault(ToolOption.QUIET, false);
166    }
167
168    /**
169     * Load a class by qualified name.
170     */
171    public TypeElement loadClass(String name) {
172        try {
173            Name nameImpl = names.fromString(name);
174            ModuleSymbol mod = syms.inferModule(Convert.packagePart(nameImpl));
175            ClassSymbol c = finder.loadClass(mod != null ? mod : syms.errModule, nameImpl);
176            return c;
177        } catch (CompletionFailure ex) {
178            chk.completionError(null, ex);
179            return null;
180        }
181    }
182
183    boolean isSynthetic(Symbol sym) {
184        return (sym.flags() & Flags.SYNTHETIC) != 0;
185    }
186
187    void setElementToTreePath(Element e, TreePath tree) {
188        if (e == null || tree == null)
189            return;
190        elementToTreePath.put(e, tree);
191    }
192
193    public Kind getFileKind(TypeElement te) {
194        JavaFileObject jfo = ((ClassSymbol)te).outermostClass().classfile;
195        return jfo == null ? Kind.SOURCE : jfo.getKind();
196    }
197
198    /**
199     * Print a notice, iff <em>quiet</em> is not specified.
200     *
201     * @param key selects message from resource
202     */
203    public void notice(String key) {
204        if (quiet) {
205            return;
206        }
207        messager.notice(key);
208    }
209
210    /**
211     * Print a notice, iff <em>quiet</em> is not specified.
212     *
213     * @param key selects message from resource
214     * @param a1 first argument
215     */
216    public void notice(String key, String a1) {
217        if (quiet) {
218            return;
219        }
220        messager.notice(key, a1);
221    }
222
223    TreePath getTreePath(JCCompilationUnit tree) {
224        TreePath p = treePaths.get(tree);
225        if (p == null)
226            treePaths.put(tree, p = new TreePath(tree));
227        return p;
228    }
229
230    TreePath getTreePath(JCCompilationUnit toplevel, JCPackageDecl tree) {
231        TreePath p = treePaths.get(tree);
232        if (p == null)
233            treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));
234        return p;
235    }
236
237    TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl tree) {
238        TreePath p = treePaths.get(tree);
239        if (p == null)
240            treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));
241        return p;
242    }
243
244    TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl cdecl, JCTree tree) {
245        return new TreePath(getTreePath(toplevel, cdecl), tree);
246    }
247
248    public com.sun.tools.javac.code.Types getTypes() {
249        return types;
250    }
251
252    public Env<AttrContext> getEnv(ClassSymbol tsym) {
253        return enter.getEnv(tsym);
254    }
255
256    public boolean isQuiet() {
257        return quiet;
258    }
259}
260