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