Tue, 11 Aug 2009 01:13:14 +0100
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
Summary: javac should warn/complain about identifiers with the same name as synthetic symbol
Reviewed-by: jjg
duke@1 | 1 | /* |
xdono@323 | 2 | * Copyright 2005-2009 Sun Microsystems, Inc. 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 |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun 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 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.processing; |
duke@1 | 27 | |
jjg@50 | 28 | |
jjg@50 | 29 | import java.lang.reflect.*; |
jjg@50 | 30 | import java.util.*; |
jjg@50 | 31 | import java.util.regex.*; |
jjg@50 | 32 | |
jjg@50 | 33 | import java.net.URL; |
jjg@50 | 34 | import java.io.Closeable; |
jjg@50 | 35 | import java.io.File; |
jjg@50 | 36 | import java.io.PrintWriter; |
jjg@50 | 37 | import java.io.IOException; |
jjg@50 | 38 | import java.net.MalformedURLException; |
duke@1 | 39 | import java.io.StringWriter; |
duke@1 | 40 | |
duke@1 | 41 | import javax.annotation.processing.*; |
duke@1 | 42 | import javax.lang.model.SourceVersion; |
duke@1 | 43 | import javax.lang.model.element.AnnotationMirror; |
duke@1 | 44 | import javax.lang.model.element.Element; |
duke@1 | 45 | import javax.lang.model.element.TypeElement; |
duke@1 | 46 | import javax.lang.model.element.PackageElement; |
duke@1 | 47 | import javax.lang.model.util.*; |
duke@1 | 48 | import javax.tools.JavaFileManager; |
duke@1 | 49 | import javax.tools.StandardJavaFileManager; |
duke@1 | 50 | import javax.tools.JavaFileObject; |
duke@1 | 51 | import javax.tools.DiagnosticListener; |
jjg@50 | 52 | |
jjg@310 | 53 | import com.sun.source.util.AbstractTypeProcessor; |
jjg@50 | 54 | import com.sun.source.util.TaskEvent; |
jjg@50 | 55 | import com.sun.source.util.TaskListener; |
jjg@50 | 56 | import com.sun.tools.javac.api.JavacTaskImpl; |
jjg@50 | 57 | import com.sun.tools.javac.code.*; |
jjg@50 | 58 | import com.sun.tools.javac.code.Symbol.*; |
jjg@50 | 59 | import com.sun.tools.javac.file.JavacFileManager; |
jjg@50 | 60 | import com.sun.tools.javac.jvm.*; |
jjg@50 | 61 | import com.sun.tools.javac.main.JavaCompiler; |
jjg@310 | 62 | import com.sun.tools.javac.main.JavaCompiler.CompileState; |
jjg@50 | 63 | import com.sun.tools.javac.model.JavacElements; |
jjg@50 | 64 | import com.sun.tools.javac.model.JavacTypes; |
jjg@50 | 65 | import com.sun.tools.javac.parser.*; |
jjg@50 | 66 | import com.sun.tools.javac.tree.*; |
jjg@50 | 67 | import com.sun.tools.javac.tree.JCTree.*; |
jjg@50 | 68 | import com.sun.tools.javac.util.Abort; |
jjg@50 | 69 | import com.sun.tools.javac.util.Context; |
jjg@50 | 70 | import com.sun.tools.javac.util.List; |
jjg@50 | 71 | import com.sun.tools.javac.util.ListBuffer; |
jjg@50 | 72 | import com.sun.tools.javac.util.Log; |
mcimadamore@136 | 73 | import com.sun.tools.javac.util.JavacMessages; |
jjg@50 | 74 | import com.sun.tools.javac.util.Name; |
jjg@113 | 75 | import com.sun.tools.javac.util.Names; |
jjg@50 | 76 | import com.sun.tools.javac.util.Options; |
jjg@50 | 77 | |
duke@1 | 78 | import static javax.tools.StandardLocation.*; |
duke@1 | 79 | |
duke@1 | 80 | /** |
duke@1 | 81 | * Objects of this class hold and manage the state needed to support |
duke@1 | 82 | * annotation processing. |
duke@1 | 83 | * |
duke@1 | 84 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
duke@1 | 85 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 86 | * This code and its internal interfaces are subject to change or |
duke@1 | 87 | * deletion without notice.</b> |
duke@1 | 88 | */ |
duke@1 | 89 | public class JavacProcessingEnvironment implements ProcessingEnvironment, Closeable { |
duke@1 | 90 | Options options; |
duke@1 | 91 | |
duke@1 | 92 | private final boolean printProcessorInfo; |
duke@1 | 93 | private final boolean printRounds; |
duke@1 | 94 | private final boolean verbose; |
duke@1 | 95 | private final boolean lint; |
duke@1 | 96 | private final boolean procOnly; |
duke@1 | 97 | private final boolean fatalErrors; |
jjg@310 | 98 | private boolean foundTypeProcessors; |
duke@1 | 99 | |
duke@1 | 100 | private final JavacFiler filer; |
duke@1 | 101 | private final JavacMessager messager; |
duke@1 | 102 | private final JavacElements elementUtils; |
duke@1 | 103 | private final JavacTypes typeUtils; |
duke@1 | 104 | |
duke@1 | 105 | /** |
duke@1 | 106 | * Holds relevant state history of which processors have been |
duke@1 | 107 | * used. |
duke@1 | 108 | */ |
duke@1 | 109 | private DiscoveredProcessors discoveredProcs; |
duke@1 | 110 | |
duke@1 | 111 | /** |
duke@1 | 112 | * Map of processor-specific options. |
duke@1 | 113 | */ |
duke@1 | 114 | private final Map<String, String> processorOptions; |
duke@1 | 115 | |
duke@1 | 116 | /** |
duke@1 | 117 | */ |
duke@1 | 118 | private final Set<String> unmatchedProcessorOptions; |
duke@1 | 119 | |
duke@1 | 120 | /** |
duke@1 | 121 | * Annotations implicitly processed and claimed by javac. |
duke@1 | 122 | */ |
duke@1 | 123 | private final Set<String> platformAnnotations; |
duke@1 | 124 | |
duke@1 | 125 | /** |
duke@1 | 126 | * Set of packages given on command line. |
duke@1 | 127 | */ |
duke@1 | 128 | private Set<PackageSymbol> specifiedPackages = Collections.emptySet(); |
duke@1 | 129 | |
duke@1 | 130 | /** The log to be used for error reporting. |
duke@1 | 131 | */ |
duke@1 | 132 | Log log; |
duke@1 | 133 | |
duke@1 | 134 | /** |
duke@1 | 135 | * Source level of the compile. |
duke@1 | 136 | */ |
duke@1 | 137 | Source source; |
duke@1 | 138 | |
mcimadamore@136 | 139 | /** |
mcimadamore@136 | 140 | * JavacMessages object used for localization |
mcimadamore@136 | 141 | */ |
mcimadamore@136 | 142 | private JavacMessages messages; |
mcimadamore@136 | 143 | |
duke@1 | 144 | private Context context; |
duke@1 | 145 | |
mcimadamore@136 | 146 | public JavacProcessingEnvironment(Context context, Iterable<? extends Processor> processors) { |
duke@1 | 147 | options = Options.instance(context); |
duke@1 | 148 | this.context = context; |
duke@1 | 149 | log = Log.instance(context); |
duke@1 | 150 | source = Source.instance(context); |
duke@1 | 151 | printProcessorInfo = options.get("-XprintProcessorInfo") != null; |
duke@1 | 152 | printRounds = options.get("-XprintRounds") != null; |
duke@1 | 153 | verbose = options.get("-verbose") != null; |
duke@1 | 154 | lint = options.lint("processing"); |
duke@1 | 155 | procOnly = options.get("-proc:only") != null || |
duke@1 | 156 | options.get("-Xprint") != null; |
duke@1 | 157 | fatalErrors = options.get("fatalEnterError") != null; |
duke@1 | 158 | platformAnnotations = initPlatformAnnotations(); |
jjg@310 | 159 | foundTypeProcessors = false; |
duke@1 | 160 | |
duke@1 | 161 | // Initialize services before any processors are initialzied |
duke@1 | 162 | // in case processors use them. |
duke@1 | 163 | filer = new JavacFiler(context); |
duke@1 | 164 | messager = new JavacMessager(context, this); |
duke@1 | 165 | elementUtils = new JavacElements(context); |
duke@1 | 166 | typeUtils = new JavacTypes(context); |
duke@1 | 167 | processorOptions = initProcessorOptions(context); |
duke@1 | 168 | unmatchedProcessorOptions = initUnmatchedProcessorOptions(); |
mcimadamore@136 | 169 | messages = JavacMessages.instance(context); |
duke@1 | 170 | initProcessorIterator(context, processors); |
duke@1 | 171 | } |
duke@1 | 172 | |
duke@1 | 173 | private Set<String> initPlatformAnnotations() { |
duke@1 | 174 | Set<String> platformAnnotations = new HashSet<String>(); |
duke@1 | 175 | platformAnnotations.add("java.lang.Deprecated"); |
duke@1 | 176 | platformAnnotations.add("java.lang.Override"); |
duke@1 | 177 | platformAnnotations.add("java.lang.SuppressWarnings"); |
duke@1 | 178 | platformAnnotations.add("java.lang.annotation.Documented"); |
duke@1 | 179 | platformAnnotations.add("java.lang.annotation.Inherited"); |
duke@1 | 180 | platformAnnotations.add("java.lang.annotation.Retention"); |
duke@1 | 181 | platformAnnotations.add("java.lang.annotation.Target"); |
duke@1 | 182 | return Collections.unmodifiableSet(platformAnnotations); |
duke@1 | 183 | } |
duke@1 | 184 | |
duke@1 | 185 | private void initProcessorIterator(Context context, Iterable<? extends Processor> processors) { |
duke@1 | 186 | Log log = Log.instance(context); |
duke@1 | 187 | Iterator<? extends Processor> processorIterator; |
duke@1 | 188 | |
duke@1 | 189 | if (options.get("-Xprint") != null) { |
duke@1 | 190 | try { |
duke@1 | 191 | Processor processor = PrintingProcessor.class.newInstance(); |
duke@1 | 192 | processorIterator = List.of(processor).iterator(); |
duke@1 | 193 | } catch (Throwable t) { |
duke@1 | 194 | AssertionError assertError = |
duke@1 | 195 | new AssertionError("Problem instantiating PrintingProcessor."); |
duke@1 | 196 | assertError.initCause(t); |
duke@1 | 197 | throw assertError; |
duke@1 | 198 | } |
duke@1 | 199 | } else if (processors != null) { |
duke@1 | 200 | processorIterator = processors.iterator(); |
duke@1 | 201 | } else { |
duke@1 | 202 | String processorNames = options.get("-processor"); |
duke@1 | 203 | JavaFileManager fileManager = context.get(JavaFileManager.class); |
duke@1 | 204 | try { |
duke@1 | 205 | // If processorpath is not explicitly set, use the classpath. |
duke@1 | 206 | ClassLoader processorCL = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) |
duke@1 | 207 | ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH) |
duke@1 | 208 | : fileManager.getClassLoader(CLASS_PATH); |
duke@1 | 209 | |
duke@1 | 210 | /* |
duke@1 | 211 | * If the "-processor" option is used, search the appropriate |
duke@1 | 212 | * path for the named class. Otherwise, use a service |
duke@1 | 213 | * provider mechanism to create the processor iterator. |
duke@1 | 214 | */ |
duke@1 | 215 | if (processorNames != null) { |
duke@1 | 216 | processorIterator = new NameProcessIterator(processorNames, processorCL, log); |
duke@1 | 217 | } else { |
duke@1 | 218 | processorIterator = new ServiceIterator(processorCL, log); |
duke@1 | 219 | } |
duke@1 | 220 | } catch (SecurityException e) { |
duke@1 | 221 | /* |
duke@1 | 222 | * A security exception will occur if we can't create a classloader. |
duke@1 | 223 | * Ignore the exception if, with hindsight, we didn't need it anyway |
duke@1 | 224 | * (i.e. no processor was specified either explicitly, or implicitly, |
duke@1 | 225 | * in service configuration file.) Otherwise, we cannot continue. |
duke@1 | 226 | */ |
duke@1 | 227 | processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader", e); |
duke@1 | 228 | } |
duke@1 | 229 | } |
duke@1 | 230 | discoveredProcs = new DiscoveredProcessors(processorIterator); |
duke@1 | 231 | } |
duke@1 | 232 | |
duke@1 | 233 | /** |
duke@1 | 234 | * Returns an empty processor iterator if no processors are on the |
duke@1 | 235 | * relevant path, otherwise if processors are present, logs an |
duke@1 | 236 | * error. Called when a service loader is unavailable for some |
duke@1 | 237 | * reason, either because a service loader class cannot be found |
duke@1 | 238 | * or because a security policy prevents class loaders from being |
duke@1 | 239 | * created. |
duke@1 | 240 | * |
duke@1 | 241 | * @param key The resource key to use to log an error message |
duke@1 | 242 | * @param e If non-null, pass this exception to Abort |
duke@1 | 243 | */ |
duke@1 | 244 | private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) { |
duke@1 | 245 | JavaFileManager fileManager = context.get(JavaFileManager.class); |
duke@1 | 246 | |
duke@1 | 247 | if (fileManager instanceof JavacFileManager) { |
duke@1 | 248 | StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager; |
duke@1 | 249 | Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) |
duke@1 | 250 | ? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH) |
duke@1 | 251 | : standardFileManager.getLocation(CLASS_PATH); |
duke@1 | 252 | |
duke@1 | 253 | if (needClassLoader(options.get("-processor"), workingPath) ) |
duke@1 | 254 | handleException(key, e); |
duke@1 | 255 | |
duke@1 | 256 | } else { |
duke@1 | 257 | handleException(key, e); |
duke@1 | 258 | } |
duke@1 | 259 | |
duke@1 | 260 | java.util.List<Processor> pl = Collections.emptyList(); |
duke@1 | 261 | return pl.iterator(); |
duke@1 | 262 | } |
duke@1 | 263 | |
duke@1 | 264 | /** |
duke@1 | 265 | * Handle a security exception thrown during initializing the |
duke@1 | 266 | * Processor iterator. |
duke@1 | 267 | */ |
duke@1 | 268 | private void handleException(String key, Exception e) { |
duke@1 | 269 | if (e != null) { |
duke@1 | 270 | log.error(key, e.getLocalizedMessage()); |
duke@1 | 271 | throw new Abort(e); |
duke@1 | 272 | } else { |
duke@1 | 273 | log.error(key); |
duke@1 | 274 | throw new Abort(); |
duke@1 | 275 | } |
duke@1 | 276 | } |
duke@1 | 277 | |
duke@1 | 278 | /** |
duke@1 | 279 | * Use a service loader appropriate for the platform to provide an |
duke@1 | 280 | * iterator over annotations processors. If |
duke@1 | 281 | * java.util.ServiceLoader is present use it, otherwise, use |
duke@1 | 282 | * sun.misc.Service, otherwise fail if a loader is needed. |
duke@1 | 283 | */ |
duke@1 | 284 | private class ServiceIterator implements Iterator<Processor> { |
duke@1 | 285 | // The to-be-wrapped iterator. |
duke@1 | 286 | private Iterator<?> iterator; |
duke@1 | 287 | private Log log; |
duke@1 | 288 | |
duke@1 | 289 | ServiceIterator(ClassLoader classLoader, Log log) { |
duke@1 | 290 | Class<?> loaderClass; |
duke@1 | 291 | String loadMethodName; |
duke@1 | 292 | boolean jusl; |
duke@1 | 293 | |
duke@1 | 294 | this.log = log; |
duke@1 | 295 | try { |
duke@1 | 296 | try { |
duke@1 | 297 | loaderClass = Class.forName("java.util.ServiceLoader"); |
duke@1 | 298 | loadMethodName = "load"; |
duke@1 | 299 | jusl = true; |
duke@1 | 300 | } catch (ClassNotFoundException cnfe) { |
duke@1 | 301 | try { |
duke@1 | 302 | loaderClass = Class.forName("sun.misc.Service"); |
duke@1 | 303 | loadMethodName = "providers"; |
duke@1 | 304 | jusl = false; |
duke@1 | 305 | } catch (ClassNotFoundException cnfe2) { |
duke@1 | 306 | // Fail softly if a loader is not actually needed. |
duke@1 | 307 | this.iterator = handleServiceLoaderUnavailability("proc.no.service", |
duke@1 | 308 | null); |
duke@1 | 309 | return; |
duke@1 | 310 | } |
duke@1 | 311 | } |
duke@1 | 312 | |
duke@1 | 313 | // java.util.ServiceLoader.load or sun.misc.Service.providers |
duke@1 | 314 | Method loadMethod = loaderClass.getMethod(loadMethodName, |
duke@1 | 315 | Class.class, |
duke@1 | 316 | ClassLoader.class); |
duke@1 | 317 | |
duke@1 | 318 | Object result = loadMethod.invoke(null, |
duke@1 | 319 | Processor.class, |
duke@1 | 320 | classLoader); |
duke@1 | 321 | |
duke@1 | 322 | // For java.util.ServiceLoader, we have to call another |
duke@1 | 323 | // method to get the iterator. |
duke@1 | 324 | if (jusl) { |
duke@1 | 325 | Method m = loaderClass.getMethod("iterator"); |
duke@1 | 326 | result = m.invoke(result); // serviceLoader.iterator(); |
duke@1 | 327 | } |
duke@1 | 328 | |
duke@1 | 329 | // The result should now be an iterator. |
duke@1 | 330 | this.iterator = (Iterator<?>) result; |
duke@1 | 331 | } catch (Throwable t) { |
duke@1 | 332 | log.error("proc.service.problem"); |
duke@1 | 333 | throw new Abort(t); |
duke@1 | 334 | } |
duke@1 | 335 | } |
duke@1 | 336 | |
duke@1 | 337 | public boolean hasNext() { |
duke@1 | 338 | try { |
duke@1 | 339 | return iterator.hasNext(); |
duke@1 | 340 | } catch (Throwable t) { |
duke@1 | 341 | if ("ServiceConfigurationError". |
duke@1 | 342 | equals(t.getClass().getSimpleName())) { |
duke@1 | 343 | log.error("proc.bad.config.file", t.getLocalizedMessage()); |
duke@1 | 344 | } |
duke@1 | 345 | throw new Abort(t); |
duke@1 | 346 | } |
duke@1 | 347 | } |
duke@1 | 348 | |
duke@1 | 349 | public Processor next() { |
duke@1 | 350 | try { |
duke@1 | 351 | return (Processor)(iterator.next()); |
duke@1 | 352 | } catch (Throwable t) { |
duke@1 | 353 | if ("ServiceConfigurationError". |
duke@1 | 354 | equals(t.getClass().getSimpleName())) { |
duke@1 | 355 | log.error("proc.bad.config.file", t.getLocalizedMessage()); |
duke@1 | 356 | } else { |
duke@1 | 357 | log.error("proc.processor.constructor.error", t.getLocalizedMessage()); |
duke@1 | 358 | } |
duke@1 | 359 | throw new Abort(t); |
duke@1 | 360 | } |
duke@1 | 361 | } |
duke@1 | 362 | |
duke@1 | 363 | public void remove() { |
duke@1 | 364 | throw new UnsupportedOperationException(); |
duke@1 | 365 | } |
duke@1 | 366 | } |
duke@1 | 367 | |
duke@1 | 368 | |
duke@1 | 369 | private static class NameProcessIterator implements Iterator<Processor> { |
duke@1 | 370 | Processor nextProc = null; |
duke@1 | 371 | Iterator<String> names; |
duke@1 | 372 | ClassLoader processorCL; |
duke@1 | 373 | Log log; |
duke@1 | 374 | |
duke@1 | 375 | NameProcessIterator(String names, ClassLoader processorCL, Log log) { |
duke@1 | 376 | this.names = Arrays.asList(names.split(",")).iterator(); |
duke@1 | 377 | this.processorCL = processorCL; |
duke@1 | 378 | this.log = log; |
duke@1 | 379 | } |
duke@1 | 380 | |
duke@1 | 381 | public boolean hasNext() { |
duke@1 | 382 | if (nextProc != null) |
duke@1 | 383 | return true; |
duke@1 | 384 | else { |
duke@1 | 385 | if (!names.hasNext()) |
duke@1 | 386 | return false; |
duke@1 | 387 | else { |
duke@1 | 388 | String processorName = names.next(); |
duke@1 | 389 | |
duke@1 | 390 | Processor processor; |
duke@1 | 391 | try { |
duke@1 | 392 | try { |
duke@1 | 393 | processor = |
duke@1 | 394 | (Processor) (processorCL.loadClass(processorName).newInstance()); |
duke@1 | 395 | } catch (ClassNotFoundException cnfe) { |
duke@1 | 396 | log.error("proc.processor.not.found", processorName); |
duke@1 | 397 | return false; |
duke@1 | 398 | } catch (ClassCastException cce) { |
duke@1 | 399 | log.error("proc.processor.wrong.type", processorName); |
duke@1 | 400 | return false; |
duke@1 | 401 | } catch (Exception e ) { |
duke@1 | 402 | log.error("proc.processor.cant.instantiate", processorName); |
duke@1 | 403 | return false; |
duke@1 | 404 | } |
duke@1 | 405 | } catch(Throwable t) { |
duke@1 | 406 | throw new AnnotationProcessingError(t); |
duke@1 | 407 | } |
duke@1 | 408 | nextProc = processor; |
duke@1 | 409 | return true; |
duke@1 | 410 | } |
duke@1 | 411 | |
duke@1 | 412 | } |
duke@1 | 413 | } |
duke@1 | 414 | |
duke@1 | 415 | public Processor next() { |
duke@1 | 416 | if (hasNext()) { |
duke@1 | 417 | Processor p = nextProc; |
duke@1 | 418 | nextProc = null; |
duke@1 | 419 | return p; |
duke@1 | 420 | } else |
duke@1 | 421 | throw new NoSuchElementException(); |
duke@1 | 422 | } |
duke@1 | 423 | |
duke@1 | 424 | public void remove () { |
duke@1 | 425 | throw new UnsupportedOperationException(); |
duke@1 | 426 | } |
duke@1 | 427 | } |
duke@1 | 428 | |
duke@1 | 429 | public boolean atLeastOneProcessor() { |
duke@1 | 430 | return discoveredProcs.iterator().hasNext(); |
duke@1 | 431 | } |
duke@1 | 432 | |
duke@1 | 433 | private Map<String, String> initProcessorOptions(Context context) { |
duke@1 | 434 | Options options = Options.instance(context); |
duke@1 | 435 | Set<String> keySet = options.keySet(); |
duke@1 | 436 | Map<String, String> tempOptions = new LinkedHashMap<String, String>(); |
duke@1 | 437 | |
duke@1 | 438 | for(String key : keySet) { |
duke@1 | 439 | if (key.startsWith("-A") && key.length() > 2) { |
duke@1 | 440 | int sepIndex = key.indexOf('='); |
duke@1 | 441 | String candidateKey = null; |
duke@1 | 442 | String candidateValue = null; |
duke@1 | 443 | |
duke@1 | 444 | if (sepIndex == -1) |
duke@1 | 445 | candidateKey = key.substring(2); |
duke@1 | 446 | else if (sepIndex >= 3) { |
duke@1 | 447 | candidateKey = key.substring(2, sepIndex); |
duke@1 | 448 | candidateValue = (sepIndex < key.length()-1)? |
duke@1 | 449 | key.substring(sepIndex+1) : null; |
duke@1 | 450 | } |
duke@1 | 451 | tempOptions.put(candidateKey, candidateValue); |
duke@1 | 452 | } |
duke@1 | 453 | } |
duke@1 | 454 | |
duke@1 | 455 | return Collections.unmodifiableMap(tempOptions); |
duke@1 | 456 | } |
duke@1 | 457 | |
duke@1 | 458 | private Set<String> initUnmatchedProcessorOptions() { |
duke@1 | 459 | Set<String> unmatchedProcessorOptions = new HashSet<String>(); |
duke@1 | 460 | unmatchedProcessorOptions.addAll(processorOptions.keySet()); |
duke@1 | 461 | return unmatchedProcessorOptions; |
duke@1 | 462 | } |
duke@1 | 463 | |
duke@1 | 464 | /** |
duke@1 | 465 | * State about how a processor has been used by the tool. If a |
duke@1 | 466 | * processor has been used on a prior round, its process method is |
duke@1 | 467 | * called on all subsequent rounds, perhaps with an empty set of |
duke@1 | 468 | * annotations to process. The {@code annotatedSupported} method |
duke@1 | 469 | * caches the supported annotation information from the first (and |
duke@1 | 470 | * only) getSupportedAnnotationTypes call to the processor. |
duke@1 | 471 | */ |
duke@1 | 472 | static class ProcessorState { |
duke@1 | 473 | public Processor processor; |
duke@1 | 474 | public boolean contributed; |
duke@1 | 475 | private ArrayList<Pattern> supportedAnnotationPatterns; |
duke@1 | 476 | private ArrayList<String> supportedOptionNames; |
duke@1 | 477 | |
duke@1 | 478 | ProcessorState(Processor p, Log log, Source source, ProcessingEnvironment env) { |
duke@1 | 479 | processor = p; |
duke@1 | 480 | contributed = false; |
duke@1 | 481 | |
duke@1 | 482 | try { |
duke@1 | 483 | processor.init(env); |
duke@1 | 484 | |
duke@1 | 485 | checkSourceVersionCompatibility(source, log); |
duke@1 | 486 | |
duke@1 | 487 | supportedAnnotationPatterns = new ArrayList<Pattern>(); |
duke@1 | 488 | for (String importString : processor.getSupportedAnnotationTypes()) { |
duke@1 | 489 | supportedAnnotationPatterns.add(importStringToPattern(importString, |
duke@1 | 490 | processor, |
duke@1 | 491 | log)); |
duke@1 | 492 | } |
duke@1 | 493 | |
duke@1 | 494 | supportedOptionNames = new ArrayList<String>(); |
duke@1 | 495 | for (String optionName : processor.getSupportedOptions() ) { |
duke@1 | 496 | if (checkOptionName(optionName, log)) |
duke@1 | 497 | supportedOptionNames.add(optionName); |
duke@1 | 498 | } |
duke@1 | 499 | |
duke@1 | 500 | } catch (Throwable t) { |
duke@1 | 501 | throw new AnnotationProcessingError(t); |
duke@1 | 502 | } |
duke@1 | 503 | } |
duke@1 | 504 | |
duke@1 | 505 | /** |
duke@1 | 506 | * Checks whether or not a processor's source version is |
duke@1 | 507 | * compatible with the compilation source version. The |
duke@1 | 508 | * processor's source version needs to be greater than or |
duke@1 | 509 | * equal to the source version of the compile. |
duke@1 | 510 | */ |
duke@1 | 511 | private void checkSourceVersionCompatibility(Source source, Log log) { |
duke@1 | 512 | SourceVersion procSourceVersion = processor.getSupportedSourceVersion(); |
duke@1 | 513 | |
duke@1 | 514 | if (procSourceVersion.compareTo(Source.toSourceVersion(source)) < 0 ) { |
duke@1 | 515 | log.warning("proc.processor.incompatible.source.version", |
duke@1 | 516 | procSourceVersion, |
duke@1 | 517 | processor.getClass().getName(), |
duke@1 | 518 | source.name); |
duke@1 | 519 | } |
duke@1 | 520 | } |
duke@1 | 521 | |
duke@1 | 522 | private boolean checkOptionName(String optionName, Log log) { |
duke@1 | 523 | boolean valid = isValidOptionName(optionName); |
duke@1 | 524 | if (!valid) |
duke@1 | 525 | log.error("proc.processor.bad.option.name", |
duke@1 | 526 | optionName, |
duke@1 | 527 | processor.getClass().getName()); |
duke@1 | 528 | return valid; |
duke@1 | 529 | } |
duke@1 | 530 | |
duke@1 | 531 | public boolean annotationSupported(String annotationName) { |
duke@1 | 532 | for(Pattern p: supportedAnnotationPatterns) { |
duke@1 | 533 | if (p.matcher(annotationName).matches()) |
duke@1 | 534 | return true; |
duke@1 | 535 | } |
duke@1 | 536 | return false; |
duke@1 | 537 | } |
duke@1 | 538 | |
duke@1 | 539 | /** |
duke@1 | 540 | * Remove options that are matched by this processor. |
duke@1 | 541 | */ |
duke@1 | 542 | public void removeSupportedOptions(Set<String> unmatchedProcessorOptions) { |
duke@1 | 543 | unmatchedProcessorOptions.removeAll(supportedOptionNames); |
duke@1 | 544 | } |
duke@1 | 545 | } |
duke@1 | 546 | |
duke@1 | 547 | // TODO: These two classes can probably be rewritten better... |
duke@1 | 548 | /** |
duke@1 | 549 | * This class holds information about the processors that have |
duke@1 | 550 | * been discoverd so far as well as the means to discover more, if |
duke@1 | 551 | * necessary. A single iterator should be used per round of |
duke@1 | 552 | * annotation processing. The iterator first visits already |
duke@1 | 553 | * discovered processors then fails over to the service provided |
duke@1 | 554 | * mechanism if additional queries are made. |
duke@1 | 555 | */ |
duke@1 | 556 | class DiscoveredProcessors implements Iterable<ProcessorState> { |
duke@1 | 557 | |
duke@1 | 558 | class ProcessorStateIterator implements Iterator<ProcessorState> { |
duke@1 | 559 | DiscoveredProcessors psi; |
duke@1 | 560 | Iterator<ProcessorState> innerIter; |
duke@1 | 561 | boolean onProcInterator; |
duke@1 | 562 | |
duke@1 | 563 | ProcessorStateIterator(DiscoveredProcessors psi) { |
duke@1 | 564 | this.psi = psi; |
duke@1 | 565 | this.innerIter = psi.procStateList.iterator(); |
duke@1 | 566 | this.onProcInterator = false; |
duke@1 | 567 | } |
duke@1 | 568 | |
duke@1 | 569 | public ProcessorState next() { |
duke@1 | 570 | if (!onProcInterator) { |
duke@1 | 571 | if (innerIter.hasNext()) |
duke@1 | 572 | return innerIter.next(); |
duke@1 | 573 | else |
duke@1 | 574 | onProcInterator = true; |
duke@1 | 575 | } |
duke@1 | 576 | |
duke@1 | 577 | if (psi.processorIterator.hasNext()) { |
duke@1 | 578 | ProcessorState ps = new ProcessorState(psi.processorIterator.next(), |
duke@1 | 579 | log, source, JavacProcessingEnvironment.this); |
duke@1 | 580 | psi.procStateList.add(ps); |
duke@1 | 581 | return ps; |
duke@1 | 582 | } else |
duke@1 | 583 | throw new NoSuchElementException(); |
duke@1 | 584 | } |
duke@1 | 585 | |
duke@1 | 586 | public boolean hasNext() { |
duke@1 | 587 | if (onProcInterator) |
duke@1 | 588 | return psi.processorIterator.hasNext(); |
duke@1 | 589 | else |
duke@1 | 590 | return innerIter.hasNext() || psi.processorIterator.hasNext(); |
duke@1 | 591 | } |
duke@1 | 592 | |
duke@1 | 593 | public void remove () { |
duke@1 | 594 | throw new UnsupportedOperationException(); |
duke@1 | 595 | } |
duke@1 | 596 | |
duke@1 | 597 | /** |
duke@1 | 598 | * Run all remaining processors on the procStateList that |
duke@1 | 599 | * have not already run this round with an empty set of |
duke@1 | 600 | * annotations. |
duke@1 | 601 | */ |
duke@1 | 602 | public void runContributingProcs(RoundEnvironment re) { |
duke@1 | 603 | if (!onProcInterator) { |
duke@1 | 604 | Set<TypeElement> emptyTypeElements = Collections.emptySet(); |
duke@1 | 605 | while(innerIter.hasNext()) { |
duke@1 | 606 | ProcessorState ps = innerIter.next(); |
duke@1 | 607 | if (ps.contributed) |
duke@1 | 608 | callProcessor(ps.processor, emptyTypeElements, re); |
duke@1 | 609 | } |
duke@1 | 610 | } |
duke@1 | 611 | } |
duke@1 | 612 | } |
duke@1 | 613 | |
duke@1 | 614 | Iterator<? extends Processor> processorIterator; |
duke@1 | 615 | ArrayList<ProcessorState> procStateList; |
duke@1 | 616 | |
duke@1 | 617 | public ProcessorStateIterator iterator() { |
duke@1 | 618 | return new ProcessorStateIterator(this); |
duke@1 | 619 | } |
duke@1 | 620 | |
duke@1 | 621 | DiscoveredProcessors(Iterator<? extends Processor> processorIterator) { |
duke@1 | 622 | this.processorIterator = processorIterator; |
duke@1 | 623 | this.procStateList = new ArrayList<ProcessorState>(); |
duke@1 | 624 | } |
duke@1 | 625 | } |
duke@1 | 626 | |
duke@1 | 627 | private void discoverAndRunProcs(Context context, |
duke@1 | 628 | Set<TypeElement> annotationsPresent, |
duke@1 | 629 | List<ClassSymbol> topLevelClasses, |
duke@1 | 630 | List<PackageSymbol> packageInfoFiles) { |
duke@1 | 631 | // Writer for -XprintRounds and -XprintProcessorInfo data |
duke@1 | 632 | PrintWriter xout = context.get(Log.outKey); |
duke@1 | 633 | |
duke@1 | 634 | Map<String, TypeElement> unmatchedAnnotations = |
duke@1 | 635 | new HashMap<String, TypeElement>(annotationsPresent.size()); |
duke@1 | 636 | |
duke@1 | 637 | for(TypeElement a : annotationsPresent) { |
duke@1 | 638 | unmatchedAnnotations.put(a.getQualifiedName().toString(), |
duke@1 | 639 | a); |
duke@1 | 640 | } |
duke@1 | 641 | |
duke@1 | 642 | // Give "*" processors a chance to match |
duke@1 | 643 | if (unmatchedAnnotations.size() == 0) |
duke@1 | 644 | unmatchedAnnotations.put("", null); |
duke@1 | 645 | |
duke@1 | 646 | DiscoveredProcessors.ProcessorStateIterator psi = discoveredProcs.iterator(); |
duke@1 | 647 | // TODO: Create proper argument values; need past round |
duke@1 | 648 | // information to fill in this constructor. Note that the 1 |
duke@1 | 649 | // st round of processing could be the last round if there |
duke@1 | 650 | // were parse errors on the initial source files; however, we |
duke@1 | 651 | // are not doing processing in that case. |
duke@1 | 652 | |
duke@1 | 653 | Set<Element> rootElements = new LinkedHashSet<Element>(); |
duke@1 | 654 | rootElements.addAll(topLevelClasses); |
duke@1 | 655 | rootElements.addAll(packageInfoFiles); |
duke@1 | 656 | rootElements = Collections.unmodifiableSet(rootElements); |
duke@1 | 657 | |
duke@1 | 658 | RoundEnvironment renv = new JavacRoundEnvironment(false, |
duke@1 | 659 | false, |
duke@1 | 660 | rootElements, |
duke@1 | 661 | JavacProcessingEnvironment.this); |
duke@1 | 662 | |
duke@1 | 663 | while(unmatchedAnnotations.size() > 0 && psi.hasNext() ) { |
duke@1 | 664 | ProcessorState ps = psi.next(); |
duke@1 | 665 | Set<String> matchedNames = new HashSet<String>(); |
duke@1 | 666 | Set<TypeElement> typeElements = new LinkedHashSet<TypeElement>(); |
duke@1 | 667 | for (String unmatchedAnnotationName : unmatchedAnnotations.keySet()) { |
duke@1 | 668 | if (ps.annotationSupported(unmatchedAnnotationName) ) { |
duke@1 | 669 | matchedNames.add(unmatchedAnnotationName); |
duke@1 | 670 | TypeElement te = unmatchedAnnotations.get(unmatchedAnnotationName); |
duke@1 | 671 | if (te != null) |
duke@1 | 672 | typeElements.add(te); |
duke@1 | 673 | } |
duke@1 | 674 | } |
duke@1 | 675 | |
duke@1 | 676 | if (matchedNames.size() > 0 || ps.contributed) { |
jjg@310 | 677 | foundTypeProcessors = foundTypeProcessors || (ps.processor instanceof AbstractTypeProcessor); |
duke@1 | 678 | boolean processingResult = callProcessor(ps.processor, typeElements, renv); |
duke@1 | 679 | ps.contributed = true; |
duke@1 | 680 | ps.removeSupportedOptions(unmatchedProcessorOptions); |
duke@1 | 681 | |
duke@1 | 682 | if (printProcessorInfo || verbose) { |
duke@1 | 683 | xout.println(Log.getLocalizedString("x.print.processor.info", |
duke@1 | 684 | ps.processor.getClass().getName(), |
duke@1 | 685 | matchedNames.toString(), |
duke@1 | 686 | processingResult)); |
duke@1 | 687 | } |
duke@1 | 688 | |
duke@1 | 689 | if (processingResult) { |
duke@1 | 690 | unmatchedAnnotations.keySet().removeAll(matchedNames); |
duke@1 | 691 | } |
duke@1 | 692 | |
duke@1 | 693 | } |
duke@1 | 694 | } |
duke@1 | 695 | unmatchedAnnotations.remove(""); |
duke@1 | 696 | |
duke@1 | 697 | if (lint && unmatchedAnnotations.size() > 0) { |
duke@1 | 698 | // Remove annotations processed by javac |
duke@1 | 699 | unmatchedAnnotations.keySet().removeAll(platformAnnotations); |
duke@1 | 700 | if (unmatchedAnnotations.size() > 0) { |
duke@1 | 701 | log = Log.instance(context); |
duke@1 | 702 | log.warning("proc.annotations.without.processors", |
duke@1 | 703 | unmatchedAnnotations.keySet()); |
duke@1 | 704 | } |
duke@1 | 705 | } |
duke@1 | 706 | |
duke@1 | 707 | // Run contributing processors that haven't run yet |
duke@1 | 708 | psi.runContributingProcs(renv); |
duke@1 | 709 | |
duke@1 | 710 | // Debugging |
duke@1 | 711 | if (options.get("displayFilerState") != null) |
duke@1 | 712 | filer.displayState(); |
duke@1 | 713 | } |
duke@1 | 714 | |
duke@1 | 715 | /** |
duke@1 | 716 | * Computes the set of annotations on the symbol in question. |
duke@1 | 717 | * Leave class public for external testing purposes. |
duke@1 | 718 | */ |
duke@1 | 719 | public static class ComputeAnnotationSet extends |
duke@1 | 720 | ElementScanner6<Set<TypeElement>, Set<TypeElement>> { |
duke@1 | 721 | final Elements elements; |
duke@1 | 722 | |
duke@1 | 723 | public ComputeAnnotationSet(Elements elements) { |
duke@1 | 724 | super(); |
duke@1 | 725 | this.elements = elements; |
duke@1 | 726 | } |
duke@1 | 727 | |
duke@1 | 728 | @Override |
duke@1 | 729 | public Set<TypeElement> visitPackage(PackageElement e, Set<TypeElement> p) { |
duke@1 | 730 | // Don't scan enclosed elements of a package |
duke@1 | 731 | return p; |
duke@1 | 732 | } |
duke@1 | 733 | |
duke@1 | 734 | @Override |
duke@1 | 735 | public Set<TypeElement> scan(Element e, Set<TypeElement> p) { |
duke@1 | 736 | for (AnnotationMirror annotationMirror : |
duke@1 | 737 | elements.getAllAnnotationMirrors(e) ) { |
duke@1 | 738 | Element e2 = annotationMirror.getAnnotationType().asElement(); |
duke@1 | 739 | p.add((TypeElement) e2); |
duke@1 | 740 | } |
duke@1 | 741 | return super.scan(e, p); |
duke@1 | 742 | } |
duke@1 | 743 | } |
duke@1 | 744 | |
duke@1 | 745 | private boolean callProcessor(Processor proc, |
duke@1 | 746 | Set<? extends TypeElement> tes, |
duke@1 | 747 | RoundEnvironment renv) { |
duke@1 | 748 | try { |
duke@1 | 749 | return proc.process(tes, renv); |
duke@1 | 750 | } catch (CompletionFailure ex) { |
duke@1 | 751 | StringWriter out = new StringWriter(); |
duke@1 | 752 | ex.printStackTrace(new PrintWriter(out)); |
jjg@12 | 753 | log.error("proc.cant.access", ex.sym, ex.getDetailValue(), out.toString()); |
duke@1 | 754 | return false; |
duke@1 | 755 | } catch (Throwable t) { |
duke@1 | 756 | throw new AnnotationProcessingError(t); |
duke@1 | 757 | } |
duke@1 | 758 | } |
duke@1 | 759 | |
duke@1 | 760 | |
duke@1 | 761 | // TODO: internal catch clauses?; catch and rethrow an annotation |
duke@1 | 762 | // processing error |
duke@1 | 763 | public JavaCompiler doProcessing(Context context, |
duke@1 | 764 | List<JCCompilationUnit> roots, |
duke@1 | 765 | List<ClassSymbol> classSymbols, |
duke@1 | 766 | Iterable<? extends PackageSymbol> pckSymbols) |
duke@1 | 767 | throws IOException { |
duke@1 | 768 | |
duke@1 | 769 | log = Log.instance(context); |
duke@1 | 770 | // Writer for -XprintRounds and -XprintProcessorInfo data |
duke@1 | 771 | PrintWriter xout = context.get(Log.outKey); |
duke@1 | 772 | TaskListener taskListener = context.get(TaskListener.class); |
duke@1 | 773 | |
duke@1 | 774 | |
duke@1 | 775 | AnnotationCollector collector = new AnnotationCollector(); |
duke@1 | 776 | |
duke@1 | 777 | JavaCompiler compiler = JavaCompiler.instance(context); |
duke@1 | 778 | compiler.todo.clear(); // free the compiler's resources |
duke@1 | 779 | |
duke@1 | 780 | int round = 0; |
duke@1 | 781 | |
duke@1 | 782 | // List<JCAnnotation> annotationsPresentInSource = collector.findAnnotations(roots); |
duke@1 | 783 | List<ClassSymbol> topLevelClasses = getTopLevelClasses(roots); |
duke@1 | 784 | |
duke@1 | 785 | for (ClassSymbol classSym : classSymbols) |
duke@1 | 786 | topLevelClasses = topLevelClasses.prepend(classSym); |
duke@1 | 787 | List<PackageSymbol> packageInfoFiles = |
duke@1 | 788 | getPackageInfoFiles(roots); |
duke@1 | 789 | |
duke@1 | 790 | Set<PackageSymbol> specifiedPackages = new LinkedHashSet<PackageSymbol>(); |
duke@1 | 791 | for (PackageSymbol psym : pckSymbols) |
duke@1 | 792 | specifiedPackages.add(psym); |
duke@1 | 793 | this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages); |
duke@1 | 794 | |
duke@1 | 795 | // Use annotation processing to compute the set of annotations present |
duke@1 | 796 | Set<TypeElement> annotationsPresent = new LinkedHashSet<TypeElement>(); |
duke@1 | 797 | ComputeAnnotationSet annotationComputer = new ComputeAnnotationSet(elementUtils); |
duke@1 | 798 | for (ClassSymbol classSym : topLevelClasses) |
duke@1 | 799 | annotationComputer.scan(classSym, annotationsPresent); |
duke@1 | 800 | for (PackageSymbol pkgSym : packageInfoFiles) |
duke@1 | 801 | annotationComputer.scan(pkgSym, annotationsPresent); |
duke@1 | 802 | |
duke@1 | 803 | Context currentContext = context; |
duke@1 | 804 | |
duke@1 | 805 | int roundNumber = 0; |
duke@1 | 806 | boolean errorStatus = false; |
duke@1 | 807 | |
duke@1 | 808 | runAround: |
duke@1 | 809 | while(true) { |
duke@1 | 810 | if (fatalErrors && compiler.errorCount() != 0) { |
duke@1 | 811 | errorStatus = true; |
duke@1 | 812 | break runAround; |
duke@1 | 813 | } |
duke@1 | 814 | |
duke@1 | 815 | this.context = currentContext; |
duke@1 | 816 | roundNumber++; |
duke@1 | 817 | printRoundInfo(xout, roundNumber, topLevelClasses, annotationsPresent, false); |
duke@1 | 818 | |
duke@1 | 819 | if (taskListener != null) |
duke@1 | 820 | taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); |
duke@1 | 821 | |
duke@1 | 822 | try { |
duke@1 | 823 | discoverAndRunProcs(currentContext, annotationsPresent, topLevelClasses, packageInfoFiles); |
duke@1 | 824 | } finally { |
duke@1 | 825 | if (taskListener != null) |
duke@1 | 826 | taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); |
duke@1 | 827 | } |
duke@1 | 828 | |
duke@1 | 829 | /* |
duke@1 | 830 | * Processors for round n have run to completion. Prepare |
duke@1 | 831 | * for round (n+1) by checked for errors raised by |
duke@1 | 832 | * annotation processors and then checking for syntax |
duke@1 | 833 | * errors on any generated source files. |
duke@1 | 834 | */ |
duke@1 | 835 | if (messager.errorRaised()) { |
duke@1 | 836 | errorStatus = true; |
duke@1 | 837 | break runAround; |
duke@1 | 838 | } else { |
duke@1 | 839 | if (moreToDo()) { |
duke@1 | 840 | // annotationsPresentInSource = List.nil(); |
duke@1 | 841 | annotationsPresent = new LinkedHashSet<TypeElement>(); |
duke@1 | 842 | topLevelClasses = List.nil(); |
duke@1 | 843 | packageInfoFiles = List.nil(); |
duke@1 | 844 | |
jjg@113 | 845 | compiler.close(false); |
duke@1 | 846 | currentContext = contextForNextRound(currentContext, true); |
duke@1 | 847 | |
duke@1 | 848 | JavaFileManager fileManager = currentContext.get(JavaFileManager.class); |
duke@1 | 849 | |
duke@1 | 850 | List<JavaFileObject> fileObjects = List.nil(); |
duke@1 | 851 | for (JavaFileObject jfo : filer.getGeneratedSourceFileObjects() ) { |
duke@1 | 852 | fileObjects = fileObjects.prepend(jfo); |
duke@1 | 853 | } |
duke@1 | 854 | |
duke@1 | 855 | |
duke@1 | 856 | compiler = JavaCompiler.instance(currentContext); |
duke@1 | 857 | List<JCCompilationUnit> parsedFiles = compiler.parseFiles(fileObjects); |
duke@1 | 858 | roots = cleanTrees(roots).reverse(); |
duke@1 | 859 | |
duke@1 | 860 | |
duke@1 | 861 | for (JCCompilationUnit unit : parsedFiles) |
duke@1 | 862 | roots = roots.prepend(unit); |
duke@1 | 863 | roots = roots.reverse(); |
duke@1 | 864 | |
duke@1 | 865 | // Check for errors after parsing |
duke@1 | 866 | if (compiler.parseErrors()) { |
duke@1 | 867 | errorStatus = true; |
duke@1 | 868 | break runAround; |
duke@1 | 869 | } else { |
duke@1 | 870 | ListBuffer<ClassSymbol> classes = enterNewClassFiles(currentContext); |
duke@1 | 871 | compiler.enterTrees(roots); |
duke@1 | 872 | |
duke@1 | 873 | // annotationsPresentInSource = |
duke@1 | 874 | // collector.findAnnotations(parsedFiles); |
duke@1 | 875 | classes.appendList(getTopLevelClasses(parsedFiles)); |
duke@1 | 876 | topLevelClasses = classes.toList(); |
duke@1 | 877 | packageInfoFiles = getPackageInfoFiles(parsedFiles); |
duke@1 | 878 | |
duke@1 | 879 | annotationsPresent = new LinkedHashSet<TypeElement>(); |
duke@1 | 880 | for (ClassSymbol classSym : topLevelClasses) |
duke@1 | 881 | annotationComputer.scan(classSym, annotationsPresent); |
duke@1 | 882 | for (PackageSymbol pkgSym : packageInfoFiles) |
duke@1 | 883 | annotationComputer.scan(pkgSym, annotationsPresent); |
duke@1 | 884 | |
duke@1 | 885 | updateProcessingState(currentContext, false); |
duke@1 | 886 | } |
duke@1 | 887 | } else |
duke@1 | 888 | break runAround; // No new files |
duke@1 | 889 | } |
duke@1 | 890 | } |
duke@1 | 891 | runLastRound(xout, roundNumber, errorStatus, taskListener); |
duke@1 | 892 | |
jjg@113 | 893 | compiler.close(false); |
duke@1 | 894 | currentContext = contextForNextRound(currentContext, true); |
duke@1 | 895 | compiler = JavaCompiler.instance(currentContext); |
duke@1 | 896 | filer.newRound(currentContext, true); |
duke@1 | 897 | filer.warnIfUnclosedFiles(); |
duke@1 | 898 | warnIfUnmatchedOptions(); |
duke@1 | 899 | |
duke@1 | 900 | /* |
duke@1 | 901 | * If an annotation processor raises an error in a round, |
duke@1 | 902 | * that round runs to completion and one last round occurs. |
duke@1 | 903 | * The last round may also occur because no more source or |
duke@1 | 904 | * class files have been generated. Therefore, if an error |
duke@1 | 905 | * was raised on either of the last *two* rounds, the compile |
duke@1 | 906 | * should exit with a nonzero exit code. The current value of |
duke@1 | 907 | * errorStatus holds whether or not an error was raised on the |
duke@1 | 908 | * second to last round; errorRaised() gives the error status |
duke@1 | 909 | * of the last round. |
duke@1 | 910 | */ |
duke@1 | 911 | errorStatus = errorStatus || messager.errorRaised(); |
duke@1 | 912 | |
duke@1 | 913 | |
duke@1 | 914 | // Free resources |
duke@1 | 915 | this.close(); |
duke@1 | 916 | |
duke@1 | 917 | if (taskListener != null) |
duke@1 | 918 | taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); |
duke@1 | 919 | |
duke@1 | 920 | if (errorStatus) { |
duke@1 | 921 | compiler.log.nerrors += messager.errorCount(); |
duke@1 | 922 | if (compiler.errorCount() == 0) |
duke@1 | 923 | compiler.log.nerrors++; |
jjg@310 | 924 | } else if (procOnly && !foundTypeProcessors) { |
duke@1 | 925 | compiler.todo.clear(); |
duke@1 | 926 | } else { // Final compilation |
jjg@113 | 927 | compiler.close(false); |
duke@1 | 928 | currentContext = contextForNextRound(currentContext, true); |
jjg@308 | 929 | this.context = currentContext; |
jjg@308 | 930 | updateProcessingState(currentContext, true); |
duke@1 | 931 | compiler = JavaCompiler.instance(currentContext); |
jjg@310 | 932 | if (procOnly && foundTypeProcessors) |
jjg@310 | 933 | compiler.shouldStopPolicy = CompileState.FLOW; |
duke@1 | 934 | |
duke@1 | 935 | if (true) { |
duke@1 | 936 | compiler.enterTrees(cleanTrees(roots)); |
duke@1 | 937 | } else { |
duke@1 | 938 | List<JavaFileObject> fileObjects = List.nil(); |
duke@1 | 939 | for (JCCompilationUnit unit : roots) |
duke@1 | 940 | fileObjects = fileObjects.prepend(unit.getSourceFile()); |
duke@1 | 941 | roots = null; |
duke@1 | 942 | compiler.enterTrees(compiler.parseFiles(fileObjects.reverse())); |
duke@1 | 943 | } |
duke@1 | 944 | } |
duke@1 | 945 | |
duke@1 | 946 | return compiler; |
duke@1 | 947 | } |
duke@1 | 948 | |
duke@1 | 949 | // Call the last round of annotation processing |
duke@1 | 950 | private void runLastRound(PrintWriter xout, |
duke@1 | 951 | int roundNumber, |
duke@1 | 952 | boolean errorStatus, |
duke@1 | 953 | TaskListener taskListener) throws IOException { |
duke@1 | 954 | roundNumber++; |
duke@1 | 955 | List<ClassSymbol> noTopLevelClasses = List.nil(); |
duke@1 | 956 | Set<TypeElement> noAnnotations = Collections.emptySet(); |
duke@1 | 957 | printRoundInfo(xout, roundNumber, noTopLevelClasses, noAnnotations, true); |
duke@1 | 958 | |
duke@1 | 959 | Set<Element> emptyRootElements = Collections.emptySet(); // immutable |
duke@1 | 960 | RoundEnvironment renv = new JavacRoundEnvironment(true, |
duke@1 | 961 | errorStatus, |
duke@1 | 962 | emptyRootElements, |
duke@1 | 963 | JavacProcessingEnvironment.this); |
duke@1 | 964 | if (taskListener != null) |
duke@1 | 965 | taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); |
duke@1 | 966 | |
duke@1 | 967 | try { |
duke@1 | 968 | discoveredProcs.iterator().runContributingProcs(renv); |
duke@1 | 969 | } finally { |
duke@1 | 970 | if (taskListener != null) |
duke@1 | 971 | taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); |
duke@1 | 972 | } |
duke@1 | 973 | } |
duke@1 | 974 | |
duke@1 | 975 | private void updateProcessingState(Context currentContext, boolean lastRound) { |
duke@1 | 976 | filer.newRound(currentContext, lastRound); |
duke@1 | 977 | messager.newRound(currentContext); |
duke@1 | 978 | |
duke@1 | 979 | elementUtils.setContext(currentContext); |
duke@1 | 980 | typeUtils.setContext(currentContext); |
duke@1 | 981 | } |
duke@1 | 982 | |
duke@1 | 983 | private void warnIfUnmatchedOptions() { |
duke@1 | 984 | if (!unmatchedProcessorOptions.isEmpty()) { |
duke@1 | 985 | log.warning("proc.unmatched.processor.options", unmatchedProcessorOptions.toString()); |
duke@1 | 986 | } |
duke@1 | 987 | } |
duke@1 | 988 | |
duke@1 | 989 | private void printRoundInfo(PrintWriter xout, |
duke@1 | 990 | int roundNumber, |
duke@1 | 991 | List<ClassSymbol> topLevelClasses, |
duke@1 | 992 | Set<TypeElement> annotationsPresent, |
duke@1 | 993 | boolean lastRound) { |
duke@1 | 994 | if (printRounds || verbose) { |
duke@1 | 995 | xout.println(Log.getLocalizedString("x.print.rounds", |
duke@1 | 996 | roundNumber, |
duke@1 | 997 | "{" + topLevelClasses.toString(", ") + "}", |
duke@1 | 998 | annotationsPresent, |
duke@1 | 999 | lastRound)); |
duke@1 | 1000 | } |
duke@1 | 1001 | } |
duke@1 | 1002 | |
duke@1 | 1003 | private ListBuffer<ClassSymbol> enterNewClassFiles(Context currentContext) { |
duke@1 | 1004 | ClassReader reader = ClassReader.instance(currentContext); |
jjg@113 | 1005 | Names names = Names.instance(currentContext); |
duke@1 | 1006 | ListBuffer<ClassSymbol> list = new ListBuffer<ClassSymbol>(); |
duke@1 | 1007 | |
duke@1 | 1008 | for (Map.Entry<String,JavaFileObject> entry : filer.getGeneratedClasses().entrySet()) { |
duke@1 | 1009 | Name name = names.fromString(entry.getKey()); |
duke@1 | 1010 | JavaFileObject file = entry.getValue(); |
duke@1 | 1011 | if (file.getKind() != JavaFileObject.Kind.CLASS) |
duke@1 | 1012 | throw new AssertionError(file); |
duke@1 | 1013 | ClassSymbol cs = reader.enterClass(name, file); |
duke@1 | 1014 | list.append(cs); |
duke@1 | 1015 | } |
duke@1 | 1016 | return list; |
duke@1 | 1017 | } |
duke@1 | 1018 | |
duke@1 | 1019 | /** |
duke@1 | 1020 | * Free resources related to annotation processing. |
duke@1 | 1021 | */ |
duke@1 | 1022 | public void close() { |
duke@1 | 1023 | filer.close(); |
duke@1 | 1024 | discoveredProcs = null; |
duke@1 | 1025 | } |
duke@1 | 1026 | |
duke@1 | 1027 | private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) { |
duke@1 | 1028 | List<ClassSymbol> classes = List.nil(); |
duke@1 | 1029 | for (JCCompilationUnit unit : units) { |
duke@1 | 1030 | for (JCTree node : unit.defs) { |
duke@1 | 1031 | if (node.getTag() == JCTree.CLASSDEF) { |
duke@1 | 1032 | classes = classes.prepend(((JCClassDecl) node).sym); |
duke@1 | 1033 | } |
duke@1 | 1034 | } |
duke@1 | 1035 | } |
duke@1 | 1036 | return classes.reverse(); |
duke@1 | 1037 | } |
duke@1 | 1038 | |
duke@1 | 1039 | private List<PackageSymbol> getPackageInfoFiles(List<? extends JCCompilationUnit> units) { |
duke@1 | 1040 | List<PackageSymbol> packages = List.nil(); |
duke@1 | 1041 | for (JCCompilationUnit unit : units) { |
duke@1 | 1042 | boolean isPkgInfo = unit.sourcefile.isNameCompatible("package-info", |
duke@1 | 1043 | JavaFileObject.Kind.SOURCE); |
duke@1 | 1044 | if (isPkgInfo) { |
duke@1 | 1045 | packages = packages.prepend(unit.packge); |
duke@1 | 1046 | } |
duke@1 | 1047 | } |
duke@1 | 1048 | return packages.reverse(); |
duke@1 | 1049 | } |
duke@1 | 1050 | |
duke@1 | 1051 | private Context contextForNextRound(Context context, boolean shareNames) |
duke@1 | 1052 | throws IOException |
duke@1 | 1053 | { |
duke@1 | 1054 | Context next = new Context(); |
duke@1 | 1055 | |
duke@1 | 1056 | Options options = Options.instance(context); |
duke@1 | 1057 | assert options != null; |
duke@1 | 1058 | next.put(Options.optionsKey, options); |
duke@1 | 1059 | |
duke@1 | 1060 | PrintWriter out = context.get(Log.outKey); |
duke@1 | 1061 | assert out != null; |
duke@1 | 1062 | next.put(Log.outKey, out); |
duke@1 | 1063 | |
duke@1 | 1064 | if (shareNames) { |
jjg@113 | 1065 | Names names = Names.instance(context); |
duke@1 | 1066 | assert names != null; |
jjg@113 | 1067 | next.put(Names.namesKey, names); |
duke@1 | 1068 | } |
duke@1 | 1069 | |
mcimadamore@184 | 1070 | DiagnosticListener<?> dl = context.get(DiagnosticListener.class); |
duke@1 | 1071 | if (dl != null) |
duke@1 | 1072 | next.put(DiagnosticListener.class, dl); |
duke@1 | 1073 | |
duke@1 | 1074 | TaskListener tl = context.get(TaskListener.class); |
duke@1 | 1075 | if (tl != null) |
duke@1 | 1076 | next.put(TaskListener.class, tl); |
duke@1 | 1077 | |
duke@1 | 1078 | JavaFileManager jfm = context.get(JavaFileManager.class); |
duke@1 | 1079 | assert jfm != null; |
duke@1 | 1080 | next.put(JavaFileManager.class, jfm); |
duke@1 | 1081 | if (jfm instanceof JavacFileManager) { |
duke@1 | 1082 | ((JavacFileManager)jfm).setContext(next); |
duke@1 | 1083 | } |
duke@1 | 1084 | |
jjg@113 | 1085 | Names names = Names.instance(context); |
duke@1 | 1086 | assert names != null; |
jjg@113 | 1087 | next.put(Names.namesKey, names); |
duke@1 | 1088 | |
duke@1 | 1089 | Keywords keywords = Keywords.instance(context); |
duke@1 | 1090 | assert(keywords != null); |
duke@1 | 1091 | next.put(Keywords.keywordsKey, keywords); |
duke@1 | 1092 | |
duke@1 | 1093 | JavaCompiler oldCompiler = JavaCompiler.instance(context); |
duke@1 | 1094 | JavaCompiler nextCompiler = JavaCompiler.instance(next); |
duke@1 | 1095 | nextCompiler.initRound(oldCompiler); |
duke@1 | 1096 | |
duke@1 | 1097 | JavacTaskImpl task = context.get(JavacTaskImpl.class); |
duke@1 | 1098 | if (task != null) { |
duke@1 | 1099 | next.put(JavacTaskImpl.class, task); |
duke@1 | 1100 | task.updateContext(next); |
duke@1 | 1101 | } |
duke@1 | 1102 | |
duke@1 | 1103 | context.clear(); |
duke@1 | 1104 | return next; |
duke@1 | 1105 | } |
duke@1 | 1106 | |
duke@1 | 1107 | /* |
duke@1 | 1108 | * Called retroactively to determine if a class loader was required, |
duke@1 | 1109 | * after we have failed to create one. |
duke@1 | 1110 | */ |
duke@1 | 1111 | private boolean needClassLoader(String procNames, Iterable<? extends File> workingpath) { |
duke@1 | 1112 | if (procNames != null) |
duke@1 | 1113 | return true; |
duke@1 | 1114 | |
duke@1 | 1115 | String procPath; |
duke@1 | 1116 | URL[] urls = new URL[1]; |
duke@1 | 1117 | for(File pathElement : workingpath) { |
duke@1 | 1118 | try { |
duke@1 | 1119 | urls[0] = pathElement.toURI().toURL(); |
duke@1 | 1120 | if (ServiceProxy.hasService(Processor.class, urls)) |
duke@1 | 1121 | return true; |
duke@1 | 1122 | } catch (MalformedURLException ex) { |
duke@1 | 1123 | throw new AssertionError(ex); |
duke@1 | 1124 | } |
duke@1 | 1125 | catch (ServiceProxy.ServiceConfigurationError e) { |
duke@1 | 1126 | log.error("proc.bad.config.file", e.getLocalizedMessage()); |
duke@1 | 1127 | return true; |
duke@1 | 1128 | } |
duke@1 | 1129 | } |
duke@1 | 1130 | |
duke@1 | 1131 | return false; |
duke@1 | 1132 | } |
duke@1 | 1133 | |
duke@1 | 1134 | private class AnnotationCollector extends TreeScanner { |
duke@1 | 1135 | List<JCTree> path = List.nil(); |
duke@1 | 1136 | static final boolean verbose = false; |
duke@1 | 1137 | List<JCAnnotation> annotations = List.nil(); |
duke@1 | 1138 | |
duke@1 | 1139 | public List<JCAnnotation> findAnnotations(List<? extends JCTree> nodes) { |
duke@1 | 1140 | annotations = List.nil(); |
duke@1 | 1141 | scan(nodes); |
duke@1 | 1142 | List<JCAnnotation> found = annotations; |
duke@1 | 1143 | annotations = List.nil(); |
duke@1 | 1144 | return found.reverse(); |
duke@1 | 1145 | } |
duke@1 | 1146 | |
duke@1 | 1147 | public void scan(JCTree node) { |
duke@1 | 1148 | if (node == null) |
duke@1 | 1149 | return; |
duke@1 | 1150 | Symbol sym = TreeInfo.symbolFor(node); |
duke@1 | 1151 | if (sym != null) |
duke@1 | 1152 | path = path.prepend(node); |
duke@1 | 1153 | super.scan(node); |
duke@1 | 1154 | if (sym != null) |
duke@1 | 1155 | path = path.tail; |
duke@1 | 1156 | } |
duke@1 | 1157 | |
duke@1 | 1158 | public void visitAnnotation(JCAnnotation node) { |
duke@1 | 1159 | annotations = annotations.prepend(node); |
duke@1 | 1160 | if (verbose) { |
duke@1 | 1161 | StringBuilder sb = new StringBuilder(); |
duke@1 | 1162 | for (JCTree tree : path.reverse()) { |
duke@1 | 1163 | System.err.print(sb); |
duke@1 | 1164 | System.err.println(TreeInfo.symbolFor(tree)); |
duke@1 | 1165 | sb.append(" "); |
duke@1 | 1166 | } |
duke@1 | 1167 | System.err.print(sb); |
duke@1 | 1168 | System.err.println(node); |
duke@1 | 1169 | } |
duke@1 | 1170 | } |
duke@1 | 1171 | } |
duke@1 | 1172 | |
duke@1 | 1173 | private static <T extends JCTree> List<T> cleanTrees(List<T> nodes) { |
duke@1 | 1174 | for (T node : nodes) |
duke@1 | 1175 | treeCleaner.scan(node); |
duke@1 | 1176 | return nodes; |
duke@1 | 1177 | } |
duke@1 | 1178 | |
duke@1 | 1179 | private static TreeScanner treeCleaner = new TreeScanner() { |
duke@1 | 1180 | public void scan(JCTree node) { |
duke@1 | 1181 | super.scan(node); |
duke@1 | 1182 | if (node != null) |
duke@1 | 1183 | node.type = null; |
duke@1 | 1184 | } |
duke@1 | 1185 | public void visitTopLevel(JCCompilationUnit node) { |
duke@1 | 1186 | node.packge = null; |
duke@1 | 1187 | super.visitTopLevel(node); |
duke@1 | 1188 | } |
duke@1 | 1189 | public void visitClassDef(JCClassDecl node) { |
duke@1 | 1190 | node.sym = null; |
duke@1 | 1191 | super.visitClassDef(node); |
duke@1 | 1192 | } |
duke@1 | 1193 | public void visitMethodDef(JCMethodDecl node) { |
duke@1 | 1194 | node.sym = null; |
duke@1 | 1195 | super.visitMethodDef(node); |
duke@1 | 1196 | } |
duke@1 | 1197 | public void visitVarDef(JCVariableDecl node) { |
duke@1 | 1198 | node.sym = null; |
duke@1 | 1199 | super.visitVarDef(node); |
duke@1 | 1200 | } |
duke@1 | 1201 | public void visitNewClass(JCNewClass node) { |
duke@1 | 1202 | node.constructor = null; |
duke@1 | 1203 | super.visitNewClass(node); |
duke@1 | 1204 | } |
duke@1 | 1205 | public void visitAssignop(JCAssignOp node) { |
duke@1 | 1206 | node.operator = null; |
duke@1 | 1207 | super.visitAssignop(node); |
duke@1 | 1208 | } |
duke@1 | 1209 | public void visitUnary(JCUnary node) { |
duke@1 | 1210 | node.operator = null; |
duke@1 | 1211 | super.visitUnary(node); |
duke@1 | 1212 | } |
duke@1 | 1213 | public void visitBinary(JCBinary node) { |
duke@1 | 1214 | node.operator = null; |
duke@1 | 1215 | super.visitBinary(node); |
duke@1 | 1216 | } |
duke@1 | 1217 | public void visitSelect(JCFieldAccess node) { |
duke@1 | 1218 | node.sym = null; |
duke@1 | 1219 | super.visitSelect(node); |
duke@1 | 1220 | } |
duke@1 | 1221 | public void visitIdent(JCIdent node) { |
duke@1 | 1222 | node.sym = null; |
duke@1 | 1223 | super.visitIdent(node); |
duke@1 | 1224 | } |
jjg@308 | 1225 | public void visitApply(JCMethodInvocation node) { |
jjg@308 | 1226 | scan(node.typeargs); |
jjg@308 | 1227 | super.visitApply(node); |
jjg@308 | 1228 | } |
duke@1 | 1229 | }; |
duke@1 | 1230 | |
duke@1 | 1231 | |
duke@1 | 1232 | private boolean moreToDo() { |
duke@1 | 1233 | return filer.newFiles(); |
duke@1 | 1234 | } |
duke@1 | 1235 | |
duke@1 | 1236 | /** |
duke@1 | 1237 | * {@inheritdoc} |
duke@1 | 1238 | * |
duke@1 | 1239 | * Command line options suitable for presenting to annotation |
duke@1 | 1240 | * processors. "-Afoo=bar" should be "-Afoo" => "bar". |
duke@1 | 1241 | */ |
duke@1 | 1242 | public Map<String,String> getOptions() { |
duke@1 | 1243 | return processorOptions; |
duke@1 | 1244 | } |
duke@1 | 1245 | |
duke@1 | 1246 | public Messager getMessager() { |
duke@1 | 1247 | return messager; |
duke@1 | 1248 | } |
duke@1 | 1249 | |
duke@1 | 1250 | public Filer getFiler() { |
duke@1 | 1251 | return filer; |
duke@1 | 1252 | } |
duke@1 | 1253 | |
duke@1 | 1254 | public JavacElements getElementUtils() { |
duke@1 | 1255 | return elementUtils; |
duke@1 | 1256 | } |
duke@1 | 1257 | |
duke@1 | 1258 | public JavacTypes getTypeUtils() { |
duke@1 | 1259 | return typeUtils; |
duke@1 | 1260 | } |
duke@1 | 1261 | |
duke@1 | 1262 | public SourceVersion getSourceVersion() { |
duke@1 | 1263 | return Source.toSourceVersion(source); |
duke@1 | 1264 | } |
duke@1 | 1265 | |
duke@1 | 1266 | public Locale getLocale() { |
mcimadamore@136 | 1267 | return messages.getCurrentLocale(); |
duke@1 | 1268 | } |
duke@1 | 1269 | |
duke@1 | 1270 | public Set<Symbol.PackageSymbol> getSpecifiedPackages() { |
duke@1 | 1271 | return specifiedPackages; |
duke@1 | 1272 | } |
duke@1 | 1273 | |
duke@1 | 1274 | // Borrowed from DocletInvoker and apt |
duke@1 | 1275 | // TODO: remove from apt's Main |
duke@1 | 1276 | /** |
duke@1 | 1277 | * Utility method for converting a search path string to an array |
duke@1 | 1278 | * of directory and JAR file URLs. |
duke@1 | 1279 | * |
duke@1 | 1280 | * @param path the search path string |
duke@1 | 1281 | * @return the resulting array of directory and JAR file URLs |
duke@1 | 1282 | */ |
duke@1 | 1283 | public static URL[] pathToURLs(String path) { |
duke@1 | 1284 | StringTokenizer st = new StringTokenizer(path, File.pathSeparator); |
duke@1 | 1285 | URL[] urls = new URL[st.countTokens()]; |
duke@1 | 1286 | int count = 0; |
duke@1 | 1287 | while (st.hasMoreTokens()) { |
duke@1 | 1288 | URL url = fileToURL(new File(st.nextToken())); |
duke@1 | 1289 | if (url != null) { |
duke@1 | 1290 | urls[count++] = url; |
duke@1 | 1291 | } |
duke@1 | 1292 | } |
duke@1 | 1293 | if (urls.length != count) { |
duke@1 | 1294 | URL[] tmp = new URL[count]; |
duke@1 | 1295 | System.arraycopy(urls, 0, tmp, 0, count); |
duke@1 | 1296 | urls = tmp; |
duke@1 | 1297 | } |
duke@1 | 1298 | return urls; |
duke@1 | 1299 | } |
duke@1 | 1300 | |
duke@1 | 1301 | /** |
duke@1 | 1302 | * Returns the directory or JAR file URL corresponding to the specified |
duke@1 | 1303 | * local file name. |
duke@1 | 1304 | * |
duke@1 | 1305 | * @param file the File object |
duke@1 | 1306 | * @return the resulting directory or JAR file URL, or null if unknown |
duke@1 | 1307 | */ |
duke@1 | 1308 | private static URL fileToURL(File file) { |
duke@1 | 1309 | String name; |
duke@1 | 1310 | try { |
duke@1 | 1311 | name = file.getCanonicalPath(); |
duke@1 | 1312 | } catch (IOException e) { |
duke@1 | 1313 | name = file.getAbsolutePath(); |
duke@1 | 1314 | } |
duke@1 | 1315 | name = name.replace(File.separatorChar, '/'); |
duke@1 | 1316 | if (!name.startsWith("/")) { |
duke@1 | 1317 | name = "/" + name; |
duke@1 | 1318 | } |
duke@1 | 1319 | // If the file does not exist, then assume that it's a directory |
duke@1 | 1320 | if (!file.isFile()) { |
duke@1 | 1321 | name = name + "/"; |
duke@1 | 1322 | } |
duke@1 | 1323 | try { |
duke@1 | 1324 | return new URL("file", "", name); |
duke@1 | 1325 | } catch (MalformedURLException e) { |
duke@1 | 1326 | throw new IllegalArgumentException("file"); |
duke@1 | 1327 | } |
duke@1 | 1328 | } |
duke@1 | 1329 | |
duke@1 | 1330 | |
duke@1 | 1331 | |
duke@1 | 1332 | private static final Pattern allMatches = Pattern.compile(".*"); |
duke@1 | 1333 | |
duke@1 | 1334 | private static final Pattern noMatches = Pattern.compile("(\\P{all})+"); |
duke@1 | 1335 | /** |
duke@1 | 1336 | * Convert import-style string to regex matching that string. If |
duke@1 | 1337 | * the string is a valid import-style string, return a regex that |
duke@1 | 1338 | * won't match anything. |
duke@1 | 1339 | */ |
duke@1 | 1340 | // TODO: remove version in Apt.java |
duke@1 | 1341 | public static Pattern importStringToPattern(String s, Processor p, Log log) { |
duke@1 | 1342 | if (s.equals("*")) { |
duke@1 | 1343 | return allMatches; |
duke@1 | 1344 | } else { |
duke@1 | 1345 | String t = s; |
duke@1 | 1346 | boolean star = false; |
duke@1 | 1347 | |
duke@1 | 1348 | /* |
duke@1 | 1349 | * Validate string from factory is legal. If the string |
duke@1 | 1350 | * has more than one asterisks or the asterisks does not |
duke@1 | 1351 | * appear as the last character (preceded by a period), |
duke@1 | 1352 | * the string is not legal. |
duke@1 | 1353 | */ |
duke@1 | 1354 | |
duke@1 | 1355 | boolean valid = true; |
duke@1 | 1356 | int index = t.indexOf('*'); |
duke@1 | 1357 | if (index != -1) { |
duke@1 | 1358 | // '*' must be last character... |
duke@1 | 1359 | if (index == t.length() -1) { |
duke@1 | 1360 | // ... and preceeding character must be '.' |
duke@1 | 1361 | if ( index-1 >= 0 ) { |
duke@1 | 1362 | valid = t.charAt(index-1) == '.'; |
duke@1 | 1363 | // Strip off ".*$" for identifier checks |
duke@1 | 1364 | t = t.substring(0, t.length()-2); |
duke@1 | 1365 | } |
duke@1 | 1366 | } else |
duke@1 | 1367 | valid = false; |
duke@1 | 1368 | } |
duke@1 | 1369 | |
duke@1 | 1370 | // Verify string is off the form (javaId \.)+ or javaId |
duke@1 | 1371 | if (valid) { |
duke@1 | 1372 | String[] javaIds = t.split("\\.", t.length()+2); |
duke@1 | 1373 | for(String javaId: javaIds) |
duke@1 | 1374 | valid &= SourceVersion.isIdentifier(javaId); |
duke@1 | 1375 | } |
duke@1 | 1376 | |
duke@1 | 1377 | if (!valid) { |
duke@1 | 1378 | log.warning("proc.malformed.supported.string", s, p.getClass().getName()); |
duke@1 | 1379 | return noMatches; // won't match any valid identifier |
duke@1 | 1380 | } |
duke@1 | 1381 | |
duke@1 | 1382 | String s_prime = s.replaceAll("\\.", "\\\\."); |
duke@1 | 1383 | |
duke@1 | 1384 | if (s_prime.endsWith("*")) { |
duke@1 | 1385 | s_prime = s_prime.substring(0, s_prime.length() - 1) + ".+"; |
duke@1 | 1386 | } |
duke@1 | 1387 | |
duke@1 | 1388 | return Pattern.compile(s_prime); |
duke@1 | 1389 | } |
duke@1 | 1390 | } |
duke@1 | 1391 | |
duke@1 | 1392 | /** |
duke@1 | 1393 | * For internal use by Sun Microsystems only. This method will be |
duke@1 | 1394 | * removed without warning. |
duke@1 | 1395 | */ |
duke@1 | 1396 | public Context getContext() { |
duke@1 | 1397 | return context; |
duke@1 | 1398 | } |
duke@1 | 1399 | |
duke@1 | 1400 | public String toString() { |
duke@1 | 1401 | return "javac ProcessingEnvironment"; |
duke@1 | 1402 | } |
duke@1 | 1403 | |
duke@1 | 1404 | public static boolean isValidOptionName(String optionName) { |
duke@1 | 1405 | for(String s : optionName.split("\\.", -1)) { |
duke@1 | 1406 | if (!SourceVersion.isIdentifier(s)) |
duke@1 | 1407 | return false; |
duke@1 | 1408 | } |
duke@1 | 1409 | return true; |
duke@1 | 1410 | } |
duke@1 | 1411 | } |