1 /*
   2  * Copyright (c) 2005, 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 com.sun.tools.javac.api;
  27 
  28 import java.util.Collection;
  29 import java.util.LinkedHashSet;
  30 import java.util.Locale;
  31 import java.util.Objects;
  32 import java.util.ServiceLoader;
  33 import java.util.Set;
  34 import java.util.stream.Collectors;
  35 
  36 import javax.annotation.processing.Processor;
  37 import javax.lang.model.element.Element;
  38 import javax.lang.model.type.TypeMirror;
  39 import javax.lang.model.util.Elements;
  40 import javax.lang.model.util.Types;
  41 import javax.tools.JavaFileObject;
  42 
  43 import com.sun.source.tree.CompilationUnitTree;
  44 import com.sun.source.tree.Tree;
  45 import com.sun.source.util.JavacTask;
  46 import com.sun.source.util.ParameterNameProvider;
  47 import com.sun.source.util.Plugin;
  48 import com.sun.source.util.TaskListener;
  49 import com.sun.tools.doclint.DocLint;
  50 import com.sun.tools.javac.code.MissingInfoHandler;
  51 import com.sun.tools.javac.main.JavaCompiler;
  52 import com.sun.tools.javac.model.JavacElements;
  53 import com.sun.tools.javac.model.JavacTypes;
  54 import com.sun.tools.javac.platform.PlatformDescription;
  55 import com.sun.tools.javac.platform.PlatformDescription.PluginInfo;
  56 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
  57 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  58 import com.sun.tools.javac.tree.JCTree;
  59 import com.sun.tools.javac.util.Context;
  60 import com.sun.tools.javac.util.DefinedBy;
  61 import com.sun.tools.javac.util.DefinedBy.Api;
  62 import com.sun.tools.javac.util.List;
  63 import com.sun.tools.javac.util.Log;
  64 import com.sun.tools.javac.util.PropagatedException;
  65 
  66 /**
  67  * Provides basic functionality for implementations of JavacTask.
  68  *
  69  * <p><b>This is NOT part of any supported API.
  70  * If you write code that depends on this, you do so at your own
  71  * risk.  This code and its internal interfaces are subject to change
  72  * or deletion without notice.</b></p>
  73  */
  74 public class BasicJavacTask extends JavacTask {
  75     protected Context context;
  76     private TaskListener taskListener;
  77 
  78     public static JavacTask instance(Context context) {
  79         JavacTask instance = context.get(JavacTask.class);
  80         if (instance == null)
  81             instance = new BasicJavacTask(context, true);
  82         return instance;
  83     }
  84 
  85     public BasicJavacTask(Context c, boolean register) {
  86         context = c;
  87         if (register)
  88             context.put(JavacTask.class, this);
  89     }
  90 
  91     @Override @DefinedBy(Api.COMPILER_TREE)
  92     public Iterable<? extends CompilationUnitTree> parse() {
  93         throw new IllegalStateException();
  94     }
  95 
  96     @Override @DefinedBy(Api.COMPILER_TREE)
  97     public Iterable<? extends Element> analyze() {
  98         throw new IllegalStateException();
  99     }
 100 
 101     @Override @DefinedBy(Api.COMPILER_TREE)
 102     public Iterable<? extends JavaFileObject> generate() {
 103         throw new IllegalStateException();
 104     }
 105 
 106     @Override @DefinedBy(Api.COMPILER_TREE)
 107     public void setTaskListener(TaskListener tl) {
 108         MultiTaskListener mtl = MultiTaskListener.instance(context);
 109         if (taskListener != null)
 110             mtl.remove(taskListener);
 111         if (tl != null)
 112             mtl.add(tl);
 113         taskListener = tl;
 114     }
 115 
 116     @Override @DefinedBy(Api.COMPILER_TREE)
 117     public void addTaskListener(TaskListener taskListener) {
 118         MultiTaskListener mtl = MultiTaskListener.instance(context);
 119         mtl.add(taskListener);
 120     }
 121 
 122     @Override @DefinedBy(Api.COMPILER_TREE)
 123     public void removeTaskListener(TaskListener taskListener) {
 124         MultiTaskListener mtl = MultiTaskListener.instance(context);
 125         mtl.remove(taskListener);
 126     }
 127 
 128     @Override
 129     public void setParameterNameProvider(ParameterNameProvider handler) {
 130         MissingInfoHandler.instance(context).setDelegate(handler);
 131     }
 132 
 133     public Collection<TaskListener> getTaskListeners() {
 134         MultiTaskListener mtl = MultiTaskListener.instance(context);
 135         return mtl.getTaskListeners();
 136     }
 137 
 138     @Override @DefinedBy(Api.COMPILER_TREE)
 139     public TypeMirror getTypeMirror(Iterable<? extends Tree> path) {
 140         // TODO: Should complete attribution if necessary
 141         Tree last = null;
 142         for (Tree node : path) {
 143             last = Objects.requireNonNull(node);
 144         }
 145         if (last == null) {
 146             throw new IllegalArgumentException("empty path");
 147         }
 148         return ((JCTree) last).type;
 149     }
 150 
 151     @Override @DefinedBy(Api.COMPILER_TREE)
 152     public Elements getElements() {
 153         if (context == null)
 154             throw new IllegalStateException();
 155         return JavacElements.instance(context);
 156     }
 157 
 158     @Override @DefinedBy(Api.COMPILER_TREE)
 159     public Types getTypes() {
 160         if (context == null)
 161             throw new IllegalStateException();
 162         return JavacTypes.instance(context);
 163     }
 164 
 165     @Override @DefinedBy(Api.COMPILER)
 166     public void addModules(Iterable<String> moduleNames) {
 167         throw new IllegalStateException();
 168     }
 169 
 170     @Override @DefinedBy(Api.COMPILER)
 171     public void setProcessors(Iterable<? extends Processor> processors) {
 172         throw new IllegalStateException();
 173     }
 174 
 175     @Override @DefinedBy(Api.COMPILER)
 176     public void setLocale(Locale locale) {
 177         throw new IllegalStateException();
 178     }
 179 
 180     @Override @DefinedBy(Api.COMPILER)
 181     public Boolean call() {
 182         throw new IllegalStateException();
 183     }
 184 
 185     /**
 186      * For internal use only.
 187      * This method will be removed without warning.
 188      * @return the context
 189      */
 190     public Context getContext() {
 191         return context;
 192     }
 193 
 194     public void initPlugins(Set<List<String>> pluginOpts) {
 195         PlatformDescription platformProvider = context.get(PlatformDescription.class);
 196 
 197         if (platformProvider != null) {
 198             for (PluginInfo<Plugin> pluginDesc : platformProvider.getPlugins()) {
 199                 java.util.List<String> options =
 200                         pluginDesc.getOptions().entrySet().stream()
 201                                                           .map(e -> e.getKey() + "=" + e.getValue())
 202                                                           .collect(Collectors.toList());
 203                 try {
 204                     pluginDesc.getPlugin().init(this, options.toArray(new String[options.size()]));
 205                 } catch (RuntimeException ex) {
 206                     throw new PropagatedException(ex);
 207                 }
 208             }
 209         }
 210 
 211         if (pluginOpts.isEmpty())
 212             return;
 213 
 214         Set<List<String>> pluginsToCall = new LinkedHashSet<>(pluginOpts);
 215         JavacProcessingEnvironment pEnv = JavacProcessingEnvironment.instance(context);
 216         ServiceLoader<Plugin> sl = pEnv.getServiceLoader(Plugin.class);
 217         for (Plugin plugin : sl) {
 218             for (List<String> p : pluginsToCall) {
 219                 if (plugin.getName().equals(p.head)) {
 220                     pluginsToCall.remove(p);
 221                     try {
 222                         plugin.init(this, p.tail.toArray(new String[p.tail.size()]));
 223                     } catch (RuntimeException ex) {
 224                         throw new PropagatedException(ex);
 225                     }
 226                     break;
 227                 }
 228             }
 229         }
 230         for (List<String> p: pluginsToCall) {
 231             Log.instance(context).error(Errors.PluginNotFound(p.head));
 232         }
 233     }
 234 
 235     public void initDocLint(List<String> docLintOpts) {
 236         if (docLintOpts.isEmpty())
 237             return;
 238 
 239         new DocLint().init(this, docLintOpts.toArray(new String[docLintOpts.size()]));
 240         JavaCompiler.instance(context).keepComments = true;
 241     }
 242 }