Mon, 24 Oct 2011 13:00:20 +0100
7096014: Javac tokens should retain state
Summary: Refactor javac tokens from enum constants to stateful instances (to keep track of position, comments, etc.)
Reviewed-by: jjg
duke@1 | 1 | /* |
jjg@909 | 2 | * Copyright (c) 2004, 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.apt.main; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.*; |
duke@1 | 29 | import java.util.Map; |
duke@1 | 30 | |
duke@1 | 31 | import javax.tools.JavaFileManager; |
duke@1 | 32 | import javax.tools.JavaFileObject; |
duke@1 | 33 | |
jjg@50 | 34 | import com.sun.tools.javac.file.JavacFileManager; |
duke@1 | 35 | import com.sun.tools.javac.util.*; |
duke@1 | 36 | import com.sun.tools.javac.code.*; |
duke@1 | 37 | import com.sun.tools.javac.jvm.*; |
duke@1 | 38 | |
duke@1 | 39 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 40 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 41 | |
duke@1 | 42 | import com.sun.tools.apt.comp.*; |
duke@1 | 43 | import com.sun.tools.apt.util.Bark; |
duke@1 | 44 | import com.sun.mirror.apt.AnnotationProcessorFactory; |
duke@1 | 45 | |
duke@1 | 46 | /** |
jjg@581 | 47 | * <p><b>This is NOT part of any supported API. |
duke@1 | 48 | * If you write code that depends on this, you do so at your own |
duke@1 | 49 | * risk. This code and its internal interfaces are subject to change |
duke@1 | 50 | * or deletion without notice.</b> |
duke@1 | 51 | */ |
darcy@331 | 52 | @SuppressWarnings("deprecation") |
jjg@789 | 53 | public class AptJavaCompiler extends com.sun.tools.javac.main.JavaCompiler { |
duke@1 | 54 | /** The context key for the compiler. */ |
jjg@789 | 55 | protected static final Context.Key<AptJavaCompiler> compilerKey = |
jjg@789 | 56 | new Context.Key<AptJavaCompiler>(); |
duke@1 | 57 | |
duke@1 | 58 | /** Get the JavaCompiler instance for this context. */ |
jjg@789 | 59 | public static AptJavaCompiler instance(Context context) { |
jjg@789 | 60 | AptJavaCompiler instance = context.get(compilerKey); |
duke@1 | 61 | if (instance == null) |
jjg@789 | 62 | instance = new AptJavaCompiler(context); |
duke@1 | 63 | return instance; |
duke@1 | 64 | } |
duke@1 | 65 | |
duke@1 | 66 | |
duke@1 | 67 | java.util.Set<String> genSourceFileNames; |
duke@1 | 68 | java.util.Set<String> genClassFileNames; |
duke@1 | 69 | |
duke@1 | 70 | public java.util.Set<String> getSourceFileNames() { |
duke@1 | 71 | return genSourceFileNames; |
duke@1 | 72 | } |
duke@1 | 73 | |
duke@1 | 74 | /** List of names of generated class files. |
duke@1 | 75 | */ |
duke@1 | 76 | public java.util.Set<String> getClassFileNames() { |
duke@1 | 77 | return genClassFileNames; |
duke@1 | 78 | } |
duke@1 | 79 | |
duke@1 | 80 | java.util.Set<java.io.File> aggregateGenFiles = java.util.Collections.emptySet(); |
duke@1 | 81 | |
duke@1 | 82 | public java.util.Set<java.io.File> getAggregateGenFiles() { |
duke@1 | 83 | return aggregateGenFiles; |
duke@1 | 84 | } |
duke@1 | 85 | |
duke@1 | 86 | /** The bark to be used for error reporting. |
duke@1 | 87 | */ |
duke@1 | 88 | Bark bark; |
duke@1 | 89 | |
duke@1 | 90 | /** The log to be used for error reporting. |
duke@1 | 91 | */ |
duke@1 | 92 | Log log; |
duke@1 | 93 | |
duke@1 | 94 | /** The annotation framework |
duke@1 | 95 | */ |
duke@1 | 96 | Apt apt; |
duke@1 | 97 | |
duke@1 | 98 | private static Context preRegister(Context context) { |
duke@1 | 99 | Bark.preRegister(context); |
duke@1 | 100 | |
duke@1 | 101 | if (context.get(JavaFileManager.class) == null) |
duke@1 | 102 | JavacFileManager.preRegister(context); |
duke@1 | 103 | |
duke@1 | 104 | return context; |
duke@1 | 105 | } |
duke@1 | 106 | |
duke@1 | 107 | /** Construct a new compiler from a shared context. |
duke@1 | 108 | */ |
jjg@789 | 109 | public AptJavaCompiler(Context context) { |
duke@1 | 110 | super(preRegister(context)); |
duke@1 | 111 | |
duke@1 | 112 | context.put(compilerKey, this); |
duke@1 | 113 | apt = Apt.instance(context); |
duke@1 | 114 | |
duke@1 | 115 | ClassReader classReader = ClassReader.instance(context); |
duke@1 | 116 | classReader.preferSource = true; |
duke@1 | 117 | |
duke@1 | 118 | // TEMPORARY NOTE: bark==log, but while refactoring, we maintain their |
duke@1 | 119 | // original identities, to remember the original intent. |
duke@1 | 120 | log = Log.instance(context); |
duke@1 | 121 | bark = Bark.instance(context); |
duke@1 | 122 | |
duke@1 | 123 | Options options = Options.instance(context); |
duke@1 | 124 | classOutput = options.get("-retrofit") == null; |
duke@1 | 125 | nocompile = options.get("-nocompile") != null; |
duke@1 | 126 | print = options.get("-print") != null; |
duke@1 | 127 | classesAsDecls= options.get("-XclassesAsDecls") != null; |
duke@1 | 128 | |
duke@1 | 129 | genSourceFileNames = new java.util.LinkedHashSet<String>(); |
duke@1 | 130 | genClassFileNames = new java.util.LinkedHashSet<String>(); |
duke@1 | 131 | |
duke@1 | 132 | // this forces a copy of the line map to be kept in the tree, |
duke@1 | 133 | // for use by com.sun.mirror.util.SourcePosition. |
duke@1 | 134 | lineDebugInfo = true; |
duke@1 | 135 | } |
duke@1 | 136 | |
duke@1 | 137 | /* Switches: |
duke@1 | 138 | */ |
duke@1 | 139 | |
duke@1 | 140 | /** Emit class files. This switch is always set, except for the first |
duke@1 | 141 | * phase of retrofitting, where signatures are parsed. |
duke@1 | 142 | */ |
duke@1 | 143 | public boolean classOutput; |
duke@1 | 144 | |
duke@1 | 145 | /** The internal printing annotation processor should be used. |
duke@1 | 146 | */ |
duke@1 | 147 | public boolean print; |
duke@1 | 148 | |
duke@1 | 149 | /** Compilation should not be done after annotation processing. |
duke@1 | 150 | */ |
duke@1 | 151 | public boolean nocompile; |
duke@1 | 152 | |
duke@1 | 153 | /** Are class files being treated as declarations |
duke@1 | 154 | */ |
duke@1 | 155 | public boolean classesAsDecls; |
duke@1 | 156 | |
duke@1 | 157 | /** Try to open input stream with given name. |
duke@1 | 158 | * Report an error if this fails. |
duke@1 | 159 | * @param filename The file name of the input stream to be opened. |
duke@1 | 160 | */ |
duke@1 | 161 | // PROVIDED FOR EXTREME BACKWARDS COMPATIBILITY |
duke@1 | 162 | // There are some very obscure errors that can arise while translating |
duke@1 | 163 | // the contents of a file from bytes to characters. In Tiger, these |
duke@1 | 164 | // diagnostics were ignored. This method provides compatibility with |
duke@1 | 165 | // that behavior. It would be better to honor those diagnostics, in which |
duke@1 | 166 | // case, this method can be deleted. |
duke@1 | 167 | @Override |
duke@1 | 168 | public CharSequence readSource(JavaFileObject filename) { |
duke@1 | 169 | try { |
duke@1 | 170 | inputFiles.add(filename); |
duke@1 | 171 | boolean prev = bark.setDiagnosticsIgnored(true); |
duke@1 | 172 | try { |
duke@1 | 173 | return filename.getCharContent(false); |
duke@1 | 174 | } |
duke@1 | 175 | finally { |
duke@1 | 176 | bark.setDiagnosticsIgnored(prev); |
duke@1 | 177 | } |
duke@1 | 178 | } catch (IOException e) { |
duke@1 | 179 | bark.error(Position.NOPOS, "cant.read.file", filename); |
duke@1 | 180 | return null; |
duke@1 | 181 | } |
duke@1 | 182 | } |
duke@1 | 183 | |
duke@1 | 184 | /** Parse contents of input stream. |
duke@1 | 185 | * @param filename The name of the file from which input stream comes. |
duke@1 | 186 | * @param input The input stream to be parsed. |
duke@1 | 187 | */ |
duke@1 | 188 | // PROVIDED FOR BACKWARDS COMPATIBILITY |
duke@1 | 189 | // In Tiger, diagnostics from the scanner and parser were ignored. |
duke@1 | 190 | // This method provides compatibility with that behavior. |
duke@1 | 191 | // It would be better to honor those diagnostics, in which |
duke@1 | 192 | // case, this method can be deleted. |
duke@1 | 193 | @Override |
duke@1 | 194 | protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) { |
duke@1 | 195 | boolean prev = bark.setDiagnosticsIgnored(true); |
duke@1 | 196 | try { |
duke@1 | 197 | return super.parse(filename, content); |
duke@1 | 198 | } |
duke@1 | 199 | finally { |
duke@1 | 200 | bark.setDiagnosticsIgnored(prev); |
duke@1 | 201 | } |
duke@1 | 202 | } |
duke@1 | 203 | |
duke@1 | 204 | @Override |
duke@1 | 205 | protected boolean keepComments() { |
duke@1 | 206 | return true; // make doc comments available to mirror API impl. |
duke@1 | 207 | } |
duke@1 | 208 | |
duke@1 | 209 | /** Track when the JavaCompiler has been used to compile something. */ |
duke@1 | 210 | private boolean hasBeenUsed = false; |
duke@1 | 211 | |
duke@1 | 212 | /** Main method: compile a list of files, return all compiled classes |
duke@1 | 213 | * @param filenames The names of all files to be compiled. |
duke@1 | 214 | */ |
duke@1 | 215 | public List<ClassSymbol> compile(List<String> filenames, |
duke@1 | 216 | Map<String, String> origOptions, |
duke@1 | 217 | ClassLoader aptCL, |
duke@1 | 218 | AnnotationProcessorFactory providedFactory, |
duke@1 | 219 | java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories, |
duke@1 | 220 | java.util.Set<java.io.File> aggregateGenFiles) |
duke@1 | 221 | throws Throwable { |
duke@1 | 222 | // as a JavaCompiler can only be used once, throw an exception if |
duke@1 | 223 | // it has been used before. |
duke@1 | 224 | assert !hasBeenUsed : "attempt to reuse JavaCompiler"; |
duke@1 | 225 | hasBeenUsed = true; |
duke@1 | 226 | |
duke@1 | 227 | this.aggregateGenFiles = aggregateGenFiles; |
duke@1 | 228 | |
duke@1 | 229 | long msec = System.currentTimeMillis(); |
duke@1 | 230 | |
duke@1 | 231 | ListBuffer<ClassSymbol> classes = new ListBuffer<ClassSymbol>(); |
duke@1 | 232 | try { |
jjg@198 | 233 | JavacFileManager fm = (JavacFileManager)fileManager; |
duke@1 | 234 | //parse all files |
duke@1 | 235 | ListBuffer<JCCompilationUnit> trees = new ListBuffer<JCCompilationUnit>(); |
duke@1 | 236 | for (List<String> l = filenames; l.nonEmpty(); l = l.tail) { |
duke@1 | 237 | if (classesAsDecls) { |
duke@1 | 238 | if (! l.head.endsWith(".java") ) { // process as class file |
duke@1 | 239 | ClassSymbol cs = reader.enterClass(names.fromString(l.head)); |
duke@1 | 240 | try { |
duke@1 | 241 | cs.complete(); |
duke@1 | 242 | } catch(Symbol.CompletionFailure cf) { |
duke@1 | 243 | bark.aptError("CantFindClass", l); |
duke@1 | 244 | continue; |
duke@1 | 245 | } |
duke@1 | 246 | |
duke@1 | 247 | classes.append(cs); // add to list of classes |
duke@1 | 248 | continue; |
duke@1 | 249 | } |
duke@1 | 250 | } |
jjg@198 | 251 | JavaFileObject fo = fm.getJavaFileObjectsFromStrings(List.of(l.head)).iterator().next(); |
jjg@198 | 252 | trees.append(parse(fo)); |
duke@1 | 253 | } |
duke@1 | 254 | |
duke@1 | 255 | //enter symbols for all files |
duke@1 | 256 | List<JCCompilationUnit> roots = trees.toList(); |
duke@1 | 257 | |
duke@1 | 258 | if (errorCount() == 0) { |
duke@1 | 259 | boolean prev = bark.setDiagnosticsIgnored(true); |
duke@1 | 260 | try { |
duke@1 | 261 | enter.main(roots); |
duke@1 | 262 | } |
duke@1 | 263 | finally { |
duke@1 | 264 | bark.setDiagnosticsIgnored(prev); |
duke@1 | 265 | } |
duke@1 | 266 | } |
duke@1 | 267 | |
duke@1 | 268 | if (errorCount() == 0) { |
duke@1 | 269 | apt.main(roots, |
duke@1 | 270 | classes, |
duke@1 | 271 | origOptions, aptCL, |
duke@1 | 272 | providedFactory, |
duke@1 | 273 | productiveFactories); |
duke@1 | 274 | genSourceFileNames.addAll(apt.getSourceFileNames()); |
duke@1 | 275 | genClassFileNames.addAll(apt.getClassFileNames()); |
duke@1 | 276 | } |
duke@1 | 277 | |
duke@1 | 278 | } catch (Abort ex) { |
duke@1 | 279 | } |
duke@1 | 280 | |
duke@1 | 281 | if (verbose) |
jjg@909 | 282 | log.printVerbose("total", Long.toString(System.currentTimeMillis() - msec)); |
duke@1 | 283 | |
duke@1 | 284 | chk.reportDeferredDiagnostics(); |
duke@1 | 285 | |
duke@1 | 286 | printCount("error", errorCount()); |
duke@1 | 287 | printCount("warn", warningCount()); |
duke@1 | 288 | |
duke@1 | 289 | return classes.toList(); |
duke@1 | 290 | } |
duke@1 | 291 | } |