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