Tue, 25 Sep 2012 13:11:05 -0700
7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
Reviewed-by: mcimadamore
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.InputStream; |
duke@1 | 29 | import java.io.OutputStream; |
jjg@944 | 30 | import java.io.OutputStreamWriter; |
duke@1 | 31 | import java.io.PrintWriter; |
duke@1 | 32 | import java.io.Writer; |
jjg@944 | 33 | import java.nio.charset.Charset; |
duke@1 | 34 | import java.util.Collections; |
duke@1 | 35 | import java.util.EnumSet; |
duke@1 | 36 | import java.util.Iterator; |
duke@1 | 37 | import java.util.Locale; |
duke@1 | 38 | import java.util.Set; |
duke@1 | 39 | import javax.lang.model.SourceVersion; |
duke@1 | 40 | import javax.tools.*; |
duke@1 | 41 | |
duke@1 | 42 | import com.sun.source.util.JavacTask; |
jjg@50 | 43 | import com.sun.tools.javac.file.JavacFileManager; |
duke@1 | 44 | import com.sun.tools.javac.main.Main; |
jjg@1157 | 45 | import com.sun.tools.javac.main.Option; |
jjg@1157 | 46 | import com.sun.tools.javac.main.OptionHelper; |
jjg@1157 | 47 | import com.sun.tools.javac.main.OptionHelper.GrumpyHelper; |
jjg@946 | 48 | import com.sun.tools.javac.util.ClientCodeException; |
duke@1 | 49 | import com.sun.tools.javac.util.Context; |
duke@1 | 50 | import com.sun.tools.javac.util.Log; |
jjg@1136 | 51 | import com.sun.tools.javac.util.Log.PrefixKind; |
duke@1 | 52 | import com.sun.tools.javac.util.Options; |
duke@1 | 53 | |
duke@1 | 54 | /** |
duke@1 | 55 | * TODO: describe com.sun.tools.javac.api.Tool |
duke@1 | 56 | * |
jjg@581 | 57 | * <p><b>This is NOT part of any supported API. |
duke@1 | 58 | * If you write code that depends on this, you do so at your own |
duke@1 | 59 | * risk. This code and its internal interfaces are subject to change |
duke@1 | 60 | * or deletion without notice.</b></p> |
duke@1 | 61 | * |
duke@1 | 62 | * @author Peter von der Ah\u00e9 |
duke@1 | 63 | */ |
duke@1 | 64 | public final class JavacTool implements JavaCompiler { |
duke@1 | 65 | /** |
jjg@1157 | 66 | * Constructor used by service provider mechanism. The recommended way to |
jjg@1157 | 67 | * obtain an instance of this class is by using {@link #create} or the |
jjg@1157 | 68 | * service provider mechanism. |
duke@1 | 69 | * @see javax.tools.JavaCompilerTool |
duke@1 | 70 | * @see javax.tools.ToolProvider |
duke@1 | 71 | * @see #create |
duke@1 | 72 | */ |
duke@1 | 73 | @Deprecated |
duke@1 | 74 | public JavacTool() {} |
duke@1 | 75 | |
duke@1 | 76 | /** |
duke@1 | 77 | * Static factory method for creating new instances of this tool. |
duke@1 | 78 | * @return new instance of this tool |
duke@1 | 79 | */ |
duke@1 | 80 | public static JavacTool create() { |
duke@1 | 81 | return new JavacTool(); |
duke@1 | 82 | } |
duke@1 | 83 | |
duke@1 | 84 | public JavacFileManager getStandardFileManager( |
duke@1 | 85 | DiagnosticListener<? super JavaFileObject> diagnosticListener, |
duke@1 | 86 | Locale locale, |
duke@1 | 87 | Charset charset) { |
duke@1 | 88 | Context context = new Context(); |
jjg@944 | 89 | context.put(Locale.class, locale); |
duke@1 | 90 | if (diagnosticListener != null) |
duke@1 | 91 | context.put(DiagnosticListener.class, diagnosticListener); |
jjg@944 | 92 | PrintWriter pw = (charset == null) |
jjg@944 | 93 | ? new PrintWriter(System.err, true) |
jjg@944 | 94 | : new PrintWriter(new OutputStreamWriter(System.err, charset), true); |
jjg@944 | 95 | context.put(Log.outKey, pw); |
duke@1 | 96 | return new JavacFileManager(context, true, charset); |
duke@1 | 97 | } |
duke@1 | 98 | |
jjg@1136 | 99 | @Override |
duke@1 | 100 | public JavacTask getTask(Writer out, |
duke@1 | 101 | JavaFileManager fileManager, |
duke@1 | 102 | DiagnosticListener<? super JavaFileObject> diagnosticListener, |
duke@1 | 103 | Iterable<String> options, |
duke@1 | 104 | Iterable<String> classes, |
jjg@1136 | 105 | Iterable<? extends JavaFileObject> compilationUnits) { |
jjg@1136 | 106 | Context context = new Context(); |
jjg@1136 | 107 | return getTask(out, fileManager, diagnosticListener, |
jjg@1136 | 108 | options, classes, compilationUnits, |
jjg@1136 | 109 | context); |
jjg@1136 | 110 | } |
jjg@1136 | 111 | |
jjg@1136 | 112 | public JavacTask getTask(Writer out, |
jjg@1136 | 113 | JavaFileManager fileManager, |
jjg@1136 | 114 | DiagnosticListener<? super JavaFileObject> diagnosticListener, |
jjg@1136 | 115 | Iterable<String> options, |
jjg@1136 | 116 | Iterable<String> classes, |
jjg@1136 | 117 | Iterable<? extends JavaFileObject> compilationUnits, |
jjg@1136 | 118 | Context context) |
duke@1 | 119 | { |
jjg@946 | 120 | try { |
jjg@946 | 121 | ClientCodeWrapper ccw = ClientCodeWrapper.instance(context); |
jjg@946 | 122 | |
jjg@946 | 123 | final String kindMsg = "All compilation units must be of SOURCE kind"; |
jjg@946 | 124 | if (options != null) |
jjg@946 | 125 | for (String option : options) |
jjg@946 | 126 | option.getClass(); // null check |
jjg@946 | 127 | if (classes != null) { |
jjg@946 | 128 | for (String cls : classes) |
jjg@946 | 129 | if (!SourceVersion.isName(cls)) // implicit null check |
jjg@946 | 130 | throw new IllegalArgumentException("Not a valid class name: " + cls); |
jjg@946 | 131 | } |
jjg@946 | 132 | if (compilationUnits != null) { |
jjg@946 | 133 | compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check |
jjg@946 | 134 | for (JavaFileObject cu : compilationUnits) { |
jjg@946 | 135 | if (cu.getKind() != JavaFileObject.Kind.SOURCE) |
jjg@946 | 136 | throw new IllegalArgumentException(kindMsg); |
jjg@946 | 137 | } |
jjg@946 | 138 | } |
jjg@946 | 139 | |
jjg@946 | 140 | if (diagnosticListener != null) |
jjg@946 | 141 | context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener)); |
jjg@946 | 142 | |
jjg@946 | 143 | if (out == null) |
jjg@946 | 144 | context.put(Log.outKey, new PrintWriter(System.err, true)); |
jjg@946 | 145 | else |
jjg@946 | 146 | context.put(Log.outKey, new PrintWriter(out, true)); |
jjg@946 | 147 | |
jjg@946 | 148 | if (fileManager == null) |
jjg@946 | 149 | fileManager = getStandardFileManager(diagnosticListener, null, null); |
jjg@946 | 150 | fileManager = ccw.wrap(fileManager); |
jjg@1157 | 151 | |
jjg@946 | 152 | context.put(JavaFileManager.class, fileManager); |
jjg@1157 | 153 | |
jjg@946 | 154 | processOptions(context, fileManager, options); |
jjg@946 | 155 | Main compiler = new Main("javacTask", context.get(Log.outKey)); |
jjg@946 | 156 | return new JavacTaskImpl(compiler, options, context, classes, compilationUnits); |
jjg@946 | 157 | } catch (ClientCodeException ex) { |
jjg@946 | 158 | throw new RuntimeException(ex.getCause()); |
duke@1 | 159 | } |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | private static void processOptions(Context context, |
duke@1 | 163 | JavaFileManager fileManager, |
duke@1 | 164 | Iterable<String> options) |
duke@1 | 165 | { |
duke@1 | 166 | if (options == null) |
duke@1 | 167 | return; |
duke@1 | 168 | |
jjg@1157 | 169 | final Options optionTable = Options.instance(context); |
jjg@1136 | 170 | Log log = Log.instance(context); |
duke@1 | 171 | |
jjg@1157 | 172 | Option[] recognizedOptions = |
jjg@1157 | 173 | Option.getJavacToolOptions().toArray(new Option[0]); |
jjg@1157 | 174 | OptionHelper optionHelper = new GrumpyHelper(log) { |
jjg@1157 | 175 | @Override |
jjg@1157 | 176 | public String get(Option option) { |
jjg@1157 | 177 | return optionTable.get(option.getText()); |
jjg@1157 | 178 | } |
jjg@1157 | 179 | |
jjg@1157 | 180 | @Override |
jjg@1157 | 181 | public void put(String name, String value) { |
jjg@1157 | 182 | optionTable.put(name, value); |
jjg@1157 | 183 | } |
jjg@1157 | 184 | |
jjg@1157 | 185 | @Override |
jjg@1157 | 186 | public void remove(String name) { |
jjg@1157 | 187 | optionTable.remove(name); |
jjg@1157 | 188 | } |
jjg@1157 | 189 | }; |
jjg@1157 | 190 | |
duke@1 | 191 | Iterator<String> flags = options.iterator(); |
duke@1 | 192 | while (flags.hasNext()) { |
duke@1 | 193 | String flag = flags.next(); |
duke@1 | 194 | int j; |
duke@1 | 195 | for (j=0; j<recognizedOptions.length; j++) |
duke@1 | 196 | if (recognizedOptions[j].matches(flag)) |
duke@1 | 197 | break; |
duke@1 | 198 | |
duke@1 | 199 | if (j == recognizedOptions.length) { |
duke@1 | 200 | if (fileManager.handleOption(flag, flags)) { |
duke@1 | 201 | continue; |
duke@1 | 202 | } else { |
jjg@1136 | 203 | String msg = log.localize(PrefixKind.JAVAC, "err.invalid.flag", flag); |
duke@1 | 204 | throw new IllegalArgumentException(msg); |
duke@1 | 205 | } |
duke@1 | 206 | } |
duke@1 | 207 | |
jjg@1157 | 208 | Option option = recognizedOptions[j]; |
duke@1 | 209 | if (option.hasArg()) { |
duke@1 | 210 | if (!flags.hasNext()) { |
jjg@1136 | 211 | String msg = log.localize(PrefixKind.JAVAC, "err.req.arg", flag); |
duke@1 | 212 | throw new IllegalArgumentException(msg); |
duke@1 | 213 | } |
duke@1 | 214 | String operand = flags.next(); |
jjg@1157 | 215 | if (option.process(optionHelper, flag, operand)) |
duke@1 | 216 | // should not happen as the GrumpyHelper will throw exceptions |
duke@1 | 217 | // in case of errors |
duke@1 | 218 | throw new IllegalArgumentException(flag + " " + operand); |
duke@1 | 219 | } else { |
jjg@1157 | 220 | if (option.process(optionHelper, flag)) |
duke@1 | 221 | // should not happen as the GrumpyHelper will throw exceptions |
duke@1 | 222 | // in case of errors |
duke@1 | 223 | throw new IllegalArgumentException(flag); |
duke@1 | 224 | } |
duke@1 | 225 | } |
jjg@1135 | 226 | |
jjg@1135 | 227 | optionTable.notifyListeners(); |
duke@1 | 228 | } |
duke@1 | 229 | |
duke@1 | 230 | public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { |
duke@1 | 231 | if (err == null) |
duke@1 | 232 | err = System.err; |
duke@1 | 233 | for (String argument : arguments) |
duke@1 | 234 | argument.getClass(); // null check |
duke@1 | 235 | return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); |
duke@1 | 236 | } |
duke@1 | 237 | |
duke@1 | 238 | public Set<SourceVersion> getSourceVersions() { |
duke@1 | 239 | return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
duke@1 | 240 | SourceVersion.latest())); |
duke@1 | 241 | } |
duke@1 | 242 | |
duke@1 | 243 | public int isSupportedOption(String option) { |
jjg@1157 | 244 | Set<Option> recognizedOptions = Option.getJavacToolOptions(); |
jjg@1157 | 245 | for (Option o : recognizedOptions) { |
duke@1 | 246 | if (o.matches(option)) |
duke@1 | 247 | return o.hasArg() ? 1 : 0; |
duke@1 | 248 | } |
duke@1 | 249 | return -1; |
duke@1 | 250 | } |
duke@1 | 251 | |
duke@1 | 252 | } |