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