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