Thu, 24 Jul 2008 19:06:57 +0100
6717241: some diagnostic argument is prematurely converted into a String object
Summary: removed early toString() conversions applied to diagnostic arguments
Reviewed-by: jjg
1 /*
2 * Copyright 2005-2008 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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.PrintWriter;
32 import java.io.Writer;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.EnumSet;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Set;
40 import javax.lang.model.SourceVersion;
41 import javax.tools.*;
43 import com.sun.source.util.JavacTask;
44 import com.sun.tools.javac.file.JavacFileManager;
45 import com.sun.tools.javac.main.JavacOption.OptionKind;
46 import com.sun.tools.javac.main.JavacOption;
47 import com.sun.tools.javac.main.Main;
48 import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
49 import com.sun.tools.javac.main.RecognizedOptions;
50 import com.sun.tools.javac.util.Context;
51 import com.sun.tools.javac.util.Log;
52 import com.sun.tools.javac.util.Options;
53 import com.sun.tools.javac.util.Pair;
54 import java.nio.charset.Charset;
56 /**
57 * TODO: describe com.sun.tools.javac.api.Tool
58 *
59 * <p><b>This is NOT part of any API supported by Sun Microsystems.
60 * If you write code that depends on this, you do so at your own
61 * risk. This code and its internal interfaces are subject to change
62 * or deletion without notice.</b></p>
63 *
64 * @author Peter von der Ah\u00e9
65 */
66 public final class JavacTool implements JavaCompiler {
67 private final List<Pair<String,String>> options
68 = new ArrayList<Pair<String,String>>();
69 private final Context dummyContext = new Context();
71 private final PrintWriter silent = new PrintWriter(new OutputStream(){
72 public void write(int b) {}
73 });
75 private final Main sharedCompiler = new Main("javac", silent);
76 {
77 sharedCompiler.setOptions(Options.instance(dummyContext));
78 }
80 /**
81 * Constructor used by service provider mechanism. The correct way to
82 * obtain an instance of this class is using create or the service provider
83 * mechanism.
84 * @see javax.tools.JavaCompilerTool
85 * @see javax.tools.ToolProvider
86 * @see #create
87 */
88 @Deprecated
89 public JavacTool() {}
91 /**
92 * Static factory method for creating new instances of this tool.
93 * @return new instance of this tool
94 */
95 public static JavacTool create() {
96 return new JavacTool();
97 }
99 private String argsToString(Object... args) {
100 String newArgs = null;
101 if (args.length > 0) {
102 StringBuilder sb = new StringBuilder();
103 String separator = "";
104 for (Object arg : args) {
105 sb.append(separator).append(arg.toString());
106 separator = File.pathSeparator;
107 }
108 newArgs = sb.toString();
109 }
110 return newArgs;
111 }
113 private void setOption1(String name, OptionKind kind, Object... args) {
114 String arg = argsToString(args);
115 JavacOption option = sharedCompiler.getOption(name);
116 if (option == null || !match(kind, option.getKind()))
117 throw new IllegalArgumentException(name);
118 if ((args.length != 0) != option.hasArg())
119 throw new IllegalArgumentException(name);
120 if (option.hasArg()) {
121 if (option.process(null, name, arg)) // FIXME
122 throw new IllegalArgumentException(name);
123 } else {
124 if (option.process(null, name)) // FIXME
125 throw new IllegalArgumentException(name);
126 }
127 options.add(new Pair<String,String>(name,arg));
128 }
130 public void setOption(String name, Object... args) {
131 setOption1(name, OptionKind.NORMAL, args);
132 }
134 public void setExtendedOption(String name, Object... args) {
135 setOption1(name, OptionKind.EXTENDED, args);
136 }
138 private static boolean match(OptionKind clientKind, OptionKind optionKind) {
139 return (clientKind == (optionKind == OptionKind.HIDDEN ? optionKind.EXTENDED : optionKind));
140 }
142 public JavacFileManager getStandardFileManager(
143 DiagnosticListener<? super JavaFileObject> diagnosticListener,
144 Locale locale,
145 Charset charset) {
146 Context context = new Context();
147 if (diagnosticListener != null)
148 context.put(DiagnosticListener.class, diagnosticListener);
149 context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME
150 return new JavacFileManager(context, true, charset);
151 }
153 private boolean compilationInProgress = false;
155 /**
156 * Register that a compilation is about to start.
157 */
158 void beginContext(final Context context) {
159 if (compilationInProgress)
160 throw new IllegalStateException("Compilation in progress");
161 compilationInProgress = true;
162 final JavaFileManager givenFileManager = context.get(JavaFileManager.class);
163 context.put(JavaFileManager.class, (JavaFileManager)null);
164 context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
165 public JavaFileManager make() {
166 if (givenFileManager != null) {
167 context.put(JavaFileManager.class, givenFileManager);
168 return givenFileManager;
169 } else {
170 return new JavacFileManager(context, true, null);
171 }
172 }
173 });
174 }
176 /**
177 * Register that a compilation is completed.
178 */
179 void endContext() {
180 compilationInProgress = false;
181 }
183 public JavacTask getTask(Writer out,
184 JavaFileManager fileManager,
185 DiagnosticListener<? super JavaFileObject> diagnosticListener,
186 Iterable<String> options,
187 Iterable<String> classes,
188 Iterable<? extends JavaFileObject> compilationUnits)
189 {
190 final String kindMsg = "All compilation units must be of SOURCE kind";
191 if (options != null)
192 for (String option : options)
193 option.getClass(); // null check
194 if (classes != null) {
195 for (String cls : classes)
196 if (!SourceVersion.isName(cls)) // implicit null check
197 throw new IllegalArgumentException("Not a valid class name: " + cls);
198 }
199 if (compilationUnits != null) {
200 for (JavaFileObject cu : compilationUnits) {
201 if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
202 throw new IllegalArgumentException(kindMsg);
203 }
204 }
206 Context context = new Context();
208 if (diagnosticListener != null)
209 context.put(DiagnosticListener.class, diagnosticListener);
211 if (out == null)
212 context.put(Log.outKey, new PrintWriter(System.err, true));
213 else
214 context.put(Log.outKey, new PrintWriter(out, true));
216 if (fileManager == null)
217 fileManager = getStandardFileManager(diagnosticListener, null, null);
218 context.put(JavaFileManager.class, fileManager);
219 processOptions(context, fileManager, options);
220 Main compiler = new Main("javacTask", context.get(Log.outKey));
221 return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits);
222 }
224 private static void processOptions(Context context,
225 JavaFileManager fileManager,
226 Iterable<String> options)
227 {
228 if (options == null)
229 return;
231 Options optionTable = Options.instance(context);
233 JavacOption[] recognizedOptions =
234 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
235 Iterator<String> flags = options.iterator();
236 while (flags.hasNext()) {
237 String flag = flags.next();
238 int j;
239 for (j=0; j<recognizedOptions.length; j++)
240 if (recognizedOptions[j].matches(flag))
241 break;
243 if (j == recognizedOptions.length) {
244 if (fileManager.handleOption(flag, flags)) {
245 continue;
246 } else {
247 String msg = Main.getLocalizedString("err.invalid.flag", flag);
248 throw new IllegalArgumentException(msg);
249 }
250 }
252 JavacOption option = recognizedOptions[j];
253 if (option.hasArg()) {
254 if (!flags.hasNext()) {
255 String msg = Main.getLocalizedString("err.req.arg", flag);
256 throw new IllegalArgumentException(msg);
257 }
258 String operand = flags.next();
259 if (option.process(optionTable, flag, operand))
260 // should not happen as the GrumpyHelper will throw exceptions
261 // in case of errors
262 throw new IllegalArgumentException(flag + " " + operand);
263 } else {
264 if (option.process(optionTable, flag))
265 // should not happen as the GrumpyHelper will throw exceptions
266 // in case of errors
267 throw new IllegalArgumentException(flag);
268 }
269 }
270 }
272 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
273 if (err == null)
274 err = System.err;
275 for (String argument : arguments)
276 argument.getClass(); // null check
277 return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
278 }
280 public Set<SourceVersion> getSourceVersions() {
281 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
282 SourceVersion.latest()));
283 }
285 public int isSupportedOption(String option) {
286 JavacOption[] recognizedOptions =
287 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
288 for (JavacOption o : recognizedOptions) {
289 if (o.matches(option))
290 return o.hasArg() ? 1 : 0;
291 }
292 return -1;
293 }
295 }