Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
duke@1 | 1 | /* |
jjg@893 | 2 | * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.api; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.File; |
duke@1 | 29 | import java.io.InputStream; |
duke@1 | 30 | import java.io.OutputStream; |
jjg@944 | 31 | import java.io.OutputStreamWriter; |
duke@1 | 32 | import java.io.PrintWriter; |
duke@1 | 33 | import java.io.Writer; |
jjg@944 | 34 | import java.nio.charset.Charset; |
duke@1 | 35 | import java.util.ArrayList; |
duke@1 | 36 | import java.util.Collections; |
duke@1 | 37 | import java.util.EnumSet; |
duke@1 | 38 | import java.util.Iterator; |
duke@1 | 39 | import java.util.List; |
duke@1 | 40 | import java.util.Locale; |
duke@1 | 41 | import java.util.Set; |
duke@1 | 42 | import javax.lang.model.SourceVersion; |
duke@1 | 43 | import javax.tools.*; |
duke@1 | 44 | |
duke@1 | 45 | import com.sun.source.util.JavacTask; |
jjg@50 | 46 | import com.sun.tools.javac.file.JavacFileManager; |
duke@1 | 47 | import com.sun.tools.javac.main.JavacOption.OptionKind; |
duke@1 | 48 | import com.sun.tools.javac.main.JavacOption; |
duke@1 | 49 | import com.sun.tools.javac.main.Main; |
duke@1 | 50 | import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper; |
duke@1 | 51 | import com.sun.tools.javac.main.RecognizedOptions; |
jjg@946 | 52 | import com.sun.tools.javac.util.ClientCodeException; |
duke@1 | 53 | import com.sun.tools.javac.util.Context; |
duke@1 | 54 | import com.sun.tools.javac.util.Log; |
duke@1 | 55 | import com.sun.tools.javac.util.Options; |
duke@1 | 56 | import com.sun.tools.javac.util.Pair; |
duke@1 | 57 | |
duke@1 | 58 | /** |
duke@1 | 59 | * TODO: describe com.sun.tools.javac.api.Tool |
duke@1 | 60 | * |
jjg@581 | 61 | * <p><b>This is NOT part of any supported API. |
duke@1 | 62 | * If you write code that depends on this, you do so at your own |
duke@1 | 63 | * risk. This code and its internal interfaces are subject to change |
duke@1 | 64 | * or deletion without notice.</b></p> |
duke@1 | 65 | * |
duke@1 | 66 | * @author Peter von der Ah\u00e9 |
duke@1 | 67 | */ |
duke@1 | 68 | public final class JavacTool implements JavaCompiler { |
duke@1 | 69 | private final List<Pair<String,String>> options |
duke@1 | 70 | = new ArrayList<Pair<String,String>>(); |
duke@1 | 71 | private final Context dummyContext = new Context(); |
duke@1 | 72 | |
duke@1 | 73 | private final PrintWriter silent = new PrintWriter(new OutputStream(){ |
duke@1 | 74 | public void write(int b) {} |
duke@1 | 75 | }); |
duke@1 | 76 | |
duke@1 | 77 | private final Main sharedCompiler = new Main("javac", silent); |
duke@1 | 78 | { |
duke@1 | 79 | sharedCompiler.setOptions(Options.instance(dummyContext)); |
duke@1 | 80 | } |
duke@1 | 81 | |
duke@1 | 82 | /** |
duke@1 | 83 | * Constructor used by service provider mechanism. The correct way to |
duke@1 | 84 | * obtain an instance of this class is using create or the service provider |
duke@1 | 85 | * mechanism. |
duke@1 | 86 | * @see javax.tools.JavaCompilerTool |
duke@1 | 87 | * @see javax.tools.ToolProvider |
duke@1 | 88 | * @see #create |
duke@1 | 89 | */ |
duke@1 | 90 | @Deprecated |
duke@1 | 91 | public JavacTool() {} |
duke@1 | 92 | |
duke@1 | 93 | /** |
duke@1 | 94 | * Static factory method for creating new instances of this tool. |
duke@1 | 95 | * @return new instance of this tool |
duke@1 | 96 | */ |
duke@1 | 97 | public static JavacTool create() { |
duke@1 | 98 | return new JavacTool(); |
duke@1 | 99 | } |
duke@1 | 100 | |
duke@1 | 101 | private String argsToString(Object... args) { |
duke@1 | 102 | String newArgs = null; |
duke@1 | 103 | if (args.length > 0) { |
duke@1 | 104 | StringBuilder sb = new StringBuilder(); |
duke@1 | 105 | String separator = ""; |
duke@1 | 106 | for (Object arg : args) { |
duke@1 | 107 | sb.append(separator).append(arg.toString()); |
duke@1 | 108 | separator = File.pathSeparator; |
duke@1 | 109 | } |
duke@1 | 110 | newArgs = sb.toString(); |
duke@1 | 111 | } |
duke@1 | 112 | return newArgs; |
duke@1 | 113 | } |
duke@1 | 114 | |
duke@1 | 115 | private void setOption1(String name, OptionKind kind, Object... args) { |
duke@1 | 116 | String arg = argsToString(args); |
duke@1 | 117 | JavacOption option = sharedCompiler.getOption(name); |
duke@1 | 118 | if (option == null || !match(kind, option.getKind())) |
duke@1 | 119 | throw new IllegalArgumentException(name); |
duke@1 | 120 | if ((args.length != 0) != option.hasArg()) |
duke@1 | 121 | throw new IllegalArgumentException(name); |
duke@1 | 122 | if (option.hasArg()) { |
duke@1 | 123 | if (option.process(null, name, arg)) // FIXME |
duke@1 | 124 | throw new IllegalArgumentException(name); |
duke@1 | 125 | } else { |
duke@1 | 126 | if (option.process(null, name)) // FIXME |
duke@1 | 127 | throw new IllegalArgumentException(name); |
duke@1 | 128 | } |
duke@1 | 129 | options.add(new Pair<String,String>(name,arg)); |
duke@1 | 130 | } |
duke@1 | 131 | |
duke@1 | 132 | public void setOption(String name, Object... args) { |
duke@1 | 133 | setOption1(name, OptionKind.NORMAL, args); |
duke@1 | 134 | } |
duke@1 | 135 | |
duke@1 | 136 | public void setExtendedOption(String name, Object... args) { |
duke@1 | 137 | setOption1(name, OptionKind.EXTENDED, args); |
duke@1 | 138 | } |
duke@1 | 139 | |
duke@1 | 140 | private static boolean match(OptionKind clientKind, OptionKind optionKind) { |
jjg@507 | 141 | return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind)); |
duke@1 | 142 | } |
duke@1 | 143 | |
duke@1 | 144 | public JavacFileManager getStandardFileManager( |
duke@1 | 145 | DiagnosticListener<? super JavaFileObject> diagnosticListener, |
duke@1 | 146 | Locale locale, |
duke@1 | 147 | Charset charset) { |
duke@1 | 148 | Context context = new Context(); |
jjg@944 | 149 | context.put(Locale.class, locale); |
duke@1 | 150 | if (diagnosticListener != null) |
duke@1 | 151 | context.put(DiagnosticListener.class, diagnosticListener); |
jjg@944 | 152 | PrintWriter pw = (charset == null) |
jjg@944 | 153 | ? new PrintWriter(System.err, true) |
jjg@944 | 154 | : new PrintWriter(new OutputStreamWriter(System.err, charset), true); |
jjg@944 | 155 | context.put(Log.outKey, pw); |
duke@1 | 156 | return new JavacFileManager(context, true, charset); |
duke@1 | 157 | } |
duke@1 | 158 | |
duke@1 | 159 | public JavacTask getTask(Writer out, |
duke@1 | 160 | JavaFileManager fileManager, |
duke@1 | 161 | DiagnosticListener<? super JavaFileObject> diagnosticListener, |
duke@1 | 162 | Iterable<String> options, |
duke@1 | 163 | Iterable<String> classes, |
duke@1 | 164 | Iterable<? extends JavaFileObject> compilationUnits) |
duke@1 | 165 | { |
jjg@946 | 166 | try { |
jjg@946 | 167 | Context context = new Context(); |
jjg@946 | 168 | ClientCodeWrapper ccw = ClientCodeWrapper.instance(context); |
jjg@946 | 169 | |
jjg@946 | 170 | final String kindMsg = "All compilation units must be of SOURCE kind"; |
jjg@946 | 171 | if (options != null) |
jjg@946 | 172 | for (String option : options) |
jjg@946 | 173 | option.getClass(); // null check |
jjg@946 | 174 | if (classes != null) { |
jjg@946 | 175 | for (String cls : classes) |
jjg@946 | 176 | if (!SourceVersion.isName(cls)) // implicit null check |
jjg@946 | 177 | throw new IllegalArgumentException("Not a valid class name: " + cls); |
jjg@946 | 178 | } |
jjg@946 | 179 | if (compilationUnits != null) { |
jjg@946 | 180 | compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check |
jjg@946 | 181 | for (JavaFileObject cu : compilationUnits) { |
jjg@946 | 182 | if (cu.getKind() != JavaFileObject.Kind.SOURCE) |
jjg@946 | 183 | throw new IllegalArgumentException(kindMsg); |
jjg@946 | 184 | } |
jjg@946 | 185 | } |
jjg@946 | 186 | |
jjg@946 | 187 | if (diagnosticListener != null) |
jjg@946 | 188 | context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener)); |
jjg@946 | 189 | |
jjg@946 | 190 | if (out == null) |
jjg@946 | 191 | context.put(Log.outKey, new PrintWriter(System.err, true)); |
jjg@946 | 192 | else |
jjg@946 | 193 | context.put(Log.outKey, new PrintWriter(out, true)); |
jjg@946 | 194 | |
jjg@946 | 195 | if (fileManager == null) |
jjg@946 | 196 | fileManager = getStandardFileManager(diagnosticListener, null, null); |
jjg@946 | 197 | fileManager = ccw.wrap(fileManager); |
jjg@946 | 198 | context.put(JavaFileManager.class, fileManager); |
jjg@946 | 199 | processOptions(context, fileManager, options); |
jjg@946 | 200 | Main compiler = new Main("javacTask", context.get(Log.outKey)); |
jjg@946 | 201 | return new JavacTaskImpl(compiler, options, context, classes, compilationUnits); |
jjg@946 | 202 | } catch (ClientCodeException ex) { |
jjg@946 | 203 | throw new RuntimeException(ex.getCause()); |
duke@1 | 204 | } |
duke@1 | 205 | } |
duke@1 | 206 | |
duke@1 | 207 | private static void processOptions(Context context, |
duke@1 | 208 | JavaFileManager fileManager, |
duke@1 | 209 | Iterable<String> options) |
duke@1 | 210 | { |
duke@1 | 211 | if (options == null) |
duke@1 | 212 | return; |
duke@1 | 213 | |
duke@1 | 214 | Options optionTable = Options.instance(context); |
duke@1 | 215 | |
duke@1 | 216 | JavacOption[] recognizedOptions = |
duke@1 | 217 | RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
duke@1 | 218 | Iterator<String> flags = options.iterator(); |
duke@1 | 219 | while (flags.hasNext()) { |
duke@1 | 220 | String flag = flags.next(); |
duke@1 | 221 | int j; |
duke@1 | 222 | for (j=0; j<recognizedOptions.length; j++) |
duke@1 | 223 | if (recognizedOptions[j].matches(flag)) |
duke@1 | 224 | break; |
duke@1 | 225 | |
duke@1 | 226 | if (j == recognizedOptions.length) { |
duke@1 | 227 | if (fileManager.handleOption(flag, flags)) { |
duke@1 | 228 | continue; |
duke@1 | 229 | } else { |
duke@1 | 230 | String msg = Main.getLocalizedString("err.invalid.flag", flag); |
duke@1 | 231 | throw new IllegalArgumentException(msg); |
duke@1 | 232 | } |
duke@1 | 233 | } |
duke@1 | 234 | |
duke@1 | 235 | JavacOption option = recognizedOptions[j]; |
duke@1 | 236 | if (option.hasArg()) { |
duke@1 | 237 | if (!flags.hasNext()) { |
duke@1 | 238 | String msg = Main.getLocalizedString("err.req.arg", flag); |
duke@1 | 239 | throw new IllegalArgumentException(msg); |
duke@1 | 240 | } |
duke@1 | 241 | String operand = flags.next(); |
duke@1 | 242 | if (option.process(optionTable, flag, operand)) |
duke@1 | 243 | // should not happen as the GrumpyHelper will throw exceptions |
duke@1 | 244 | // in case of errors |
duke@1 | 245 | throw new IllegalArgumentException(flag + " " + operand); |
duke@1 | 246 | } else { |
duke@1 | 247 | if (option.process(optionTable, flag)) |
duke@1 | 248 | // should not happen as the GrumpyHelper will throw exceptions |
duke@1 | 249 | // in case of errors |
duke@1 | 250 | throw new IllegalArgumentException(flag); |
duke@1 | 251 | } |
duke@1 | 252 | } |
duke@1 | 253 | } |
duke@1 | 254 | |
duke@1 | 255 | public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { |
duke@1 | 256 | if (err == null) |
duke@1 | 257 | err = System.err; |
duke@1 | 258 | for (String argument : arguments) |
duke@1 | 259 | argument.getClass(); // null check |
duke@1 | 260 | return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); |
duke@1 | 261 | } |
duke@1 | 262 | |
duke@1 | 263 | public Set<SourceVersion> getSourceVersions() { |
duke@1 | 264 | return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
duke@1 | 265 | SourceVersion.latest())); |
duke@1 | 266 | } |
duke@1 | 267 | |
duke@1 | 268 | public int isSupportedOption(String option) { |
duke@1 | 269 | JavacOption[] recognizedOptions = |
duke@1 | 270 | RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
duke@1 | 271 | for (JavacOption o : recognizedOptions) { |
duke@1 | 272 | if (o.matches(option)) |
duke@1 | 273 | return o.hasArg() ? 1 : 0; |
duke@1 | 274 | } |
duke@1 | 275 | return -1; |
duke@1 | 276 | } |
duke@1 | 277 | |
duke@1 | 278 | } |