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