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