Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2005, 2011, 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 */
26 package com.sun.tools.javac.api;
28 import java.io.File;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.io.OutputStreamWriter;
32 import java.io.PrintWriter;
33 import java.io.Writer;
34 import java.nio.charset.Charset;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.EnumSet;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Set;
42 import javax.lang.model.SourceVersion;
43 import javax.tools.*;
45 import com.sun.source.util.JavacTask;
46 import com.sun.tools.javac.file.JavacFileManager;
47 import com.sun.tools.javac.main.JavacOption.OptionKind;
48 import com.sun.tools.javac.main.JavacOption;
49 import com.sun.tools.javac.main.Main;
50 import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
51 import com.sun.tools.javac.main.RecognizedOptions;
52 import com.sun.tools.javac.util.ClientCodeException;
53 import com.sun.tools.javac.util.Context;
54 import com.sun.tools.javac.util.Log;
55 import com.sun.tools.javac.util.Options;
56 import com.sun.tools.javac.util.Pair;
58 /**
59 * TODO: describe com.sun.tools.javac.api.Tool
60 *
61 * <p><b>This is NOT part of any supported API.
62 * If you write code that depends on this, you do so at your own
63 * risk. This code and its internal interfaces are subject to change
64 * or deletion without notice.</b></p>
65 *
66 * @author Peter von der Ah\u00e9
67 */
68 public final class JavacTool implements JavaCompiler {
69 private final List<Pair<String,String>> options
70 = new ArrayList<Pair<String,String>>();
71 private final Context dummyContext = new Context();
73 private final PrintWriter silent = new PrintWriter(new OutputStream(){
74 public void write(int b) {}
75 });
77 private final Main sharedCompiler = new Main("javac", silent);
78 {
79 sharedCompiler.setOptions(Options.instance(dummyContext));
80 }
82 /**
83 * Constructor used by service provider mechanism. The correct way to
84 * obtain an instance of this class is using create or the service provider
85 * mechanism.
86 * @see javax.tools.JavaCompilerTool
87 * @see javax.tools.ToolProvider
88 * @see #create
89 */
90 @Deprecated
91 public JavacTool() {}
93 /**
94 * Static factory method for creating new instances of this tool.
95 * @return new instance of this tool
96 */
97 public static JavacTool create() {
98 return new JavacTool();
99 }
101 private String argsToString(Object... args) {
102 String newArgs = null;
103 if (args.length > 0) {
104 StringBuilder sb = new StringBuilder();
105 String separator = "";
106 for (Object arg : args) {
107 sb.append(separator).append(arg.toString());
108 separator = File.pathSeparator;
109 }
110 newArgs = sb.toString();
111 }
112 return newArgs;
113 }
115 private void setOption1(String name, OptionKind kind, Object... args) {
116 String arg = argsToString(args);
117 JavacOption option = sharedCompiler.getOption(name);
118 if (option == null || !match(kind, option.getKind()))
119 throw new IllegalArgumentException(name);
120 if ((args.length != 0) != option.hasArg())
121 throw new IllegalArgumentException(name);
122 if (option.hasArg()) {
123 if (option.process(null, name, arg)) // FIXME
124 throw new IllegalArgumentException(name);
125 } else {
126 if (option.process(null, name)) // FIXME
127 throw new IllegalArgumentException(name);
128 }
129 options.add(new Pair<String,String>(name,arg));
130 }
132 public void setOption(String name, Object... args) {
133 setOption1(name, OptionKind.NORMAL, args);
134 }
136 public void setExtendedOption(String name, Object... args) {
137 setOption1(name, OptionKind.EXTENDED, args);
138 }
140 private static boolean match(OptionKind clientKind, OptionKind optionKind) {
141 return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind));
142 }
144 public JavacFileManager getStandardFileManager(
145 DiagnosticListener<? super JavaFileObject> diagnosticListener,
146 Locale locale,
147 Charset charset) {
148 Context context = new Context();
149 context.put(Locale.class, locale);
150 if (diagnosticListener != null)
151 context.put(DiagnosticListener.class, diagnosticListener);
152 PrintWriter pw = (charset == null)
153 ? new PrintWriter(System.err, true)
154 : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
155 context.put(Log.outKey, pw);
156 return new JavacFileManager(context, true, charset);
157 }
159 public JavacTask getTask(Writer out,
160 JavaFileManager fileManager,
161 DiagnosticListener<? super JavaFileObject> diagnosticListener,
162 Iterable<String> options,
163 Iterable<String> classes,
164 Iterable<? extends JavaFileObject> compilationUnits)
165 {
166 try {
167 Context context = new Context();
168 ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
170 final String kindMsg = "All compilation units must be of SOURCE kind";
171 if (options != null)
172 for (String option : options)
173 option.getClass(); // null check
174 if (classes != null) {
175 for (String cls : classes)
176 if (!SourceVersion.isName(cls)) // implicit null check
177 throw new IllegalArgumentException("Not a valid class name: " + cls);
178 }
179 if (compilationUnits != null) {
180 compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
181 for (JavaFileObject cu : compilationUnits) {
182 if (cu.getKind() != JavaFileObject.Kind.SOURCE)
183 throw new IllegalArgumentException(kindMsg);
184 }
185 }
187 if (diagnosticListener != null)
188 context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
190 if (out == null)
191 context.put(Log.outKey, new PrintWriter(System.err, true));
192 else
193 context.put(Log.outKey, new PrintWriter(out, true));
195 if (fileManager == null)
196 fileManager = getStandardFileManager(diagnosticListener, null, null);
197 fileManager = ccw.wrap(fileManager);
198 context.put(JavaFileManager.class, fileManager);
199 processOptions(context, fileManager, options);
200 Main compiler = new Main("javacTask", context.get(Log.outKey));
201 return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
202 } catch (ClientCodeException ex) {
203 throw new RuntimeException(ex.getCause());
204 }
205 }
207 private static void processOptions(Context context,
208 JavaFileManager fileManager,
209 Iterable<String> options)
210 {
211 if (options == null)
212 return;
214 Options optionTable = Options.instance(context);
216 JavacOption[] recognizedOptions =
217 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
218 Iterator<String> flags = options.iterator();
219 while (flags.hasNext()) {
220 String flag = flags.next();
221 int j;
222 for (j=0; j<recognizedOptions.length; j++)
223 if (recognizedOptions[j].matches(flag))
224 break;
226 if (j == recognizedOptions.length) {
227 if (fileManager.handleOption(flag, flags)) {
228 continue;
229 } else {
230 String msg = Main.getLocalizedString("err.invalid.flag", flag);
231 throw new IllegalArgumentException(msg);
232 }
233 }
235 JavacOption option = recognizedOptions[j];
236 if (option.hasArg()) {
237 if (!flags.hasNext()) {
238 String msg = Main.getLocalizedString("err.req.arg", flag);
239 throw new IllegalArgumentException(msg);
240 }
241 String operand = flags.next();
242 if (option.process(optionTable, flag, operand))
243 // should not happen as the GrumpyHelper will throw exceptions
244 // in case of errors
245 throw new IllegalArgumentException(flag + " " + operand);
246 } else {
247 if (option.process(optionTable, flag))
248 // should not happen as the GrumpyHelper will throw exceptions
249 // in case of errors
250 throw new IllegalArgumentException(flag);
251 }
252 }
253 }
255 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
256 if (err == null)
257 err = System.err;
258 for (String argument : arguments)
259 argument.getClass(); // null check
260 return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
261 }
263 public Set<SourceVersion> getSourceVersions() {
264 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
265 SourceVersion.latest()));
266 }
268 public int isSupportedOption(String option) {
269 JavacOption[] recognizedOptions =
270 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
271 for (JavacOption o : recognizedOptions) {
272 if (o.matches(option))
273 return o.hasArg() ? 1 : 0;
274 }
275 return -1;
276 }
278 }