1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Sat Nov 17 19:01:03 2012 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Mon Nov 19 11:38:49 2012 -0800 1.3 @@ -145,6 +145,7 @@ 1.4 Source source; 1.5 1.6 private ClassLoader processorClassLoader; 1.7 + private SecurityException processorClassLoaderException; 1.8 1.9 /** 1.10 * JavacMessages object used for localization 1.11 @@ -155,7 +156,15 @@ 1.12 1.13 private Context context; 1.14 1.15 - public JavacProcessingEnvironment(Context context, Iterable<? extends Processor> processors) { 1.16 + /** Get the JavacProcessingEnvironment instance for this context. */ 1.17 + public static JavacProcessingEnvironment instance(Context context) { 1.18 + JavacProcessingEnvironment instance = context.get(JavacProcessingEnvironment.class); 1.19 + if (instance == null) 1.20 + instance = new JavacProcessingEnvironment(context); 1.21 + return instance; 1.22 + } 1.23 + 1.24 + protected JavacProcessingEnvironment(Context context) { 1.25 this.context = context; 1.26 log = Log.instance(context); 1.27 source = Source.instance(context); 1.28 @@ -184,6 +193,11 @@ 1.29 unmatchedProcessorOptions = initUnmatchedProcessorOptions(); 1.30 messages = JavacMessages.instance(context); 1.31 taskListener = MultiTaskListener.instance(context); 1.32 + initProcessorClassLoader(); 1.33 + } 1.34 + 1.35 + public void setProcessors(Iterable<? extends Processor> processors) { 1.36 + Assert.checkNull(discoveredProcs); 1.37 initProcessorIterator(context, processors); 1.38 } 1.39 1.40 @@ -199,6 +213,23 @@ 1.41 return Collections.unmodifiableSet(platformAnnotations); 1.42 } 1.43 1.44 + private void initProcessorClassLoader() { 1.45 + JavaFileManager fileManager = context.get(JavaFileManager.class); 1.46 + try { 1.47 + // If processorpath is not explicitly set, use the classpath. 1.48 + processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) 1.49 + ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH) 1.50 + : fileManager.getClassLoader(CLASS_PATH); 1.51 + 1.52 + if (processorClassLoader != null && processorClassLoader instanceof Closeable) { 1.53 + JavaCompiler compiler = JavaCompiler.instance(context); 1.54 + compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader); 1.55 + } 1.56 + } catch (SecurityException e) { 1.57 + processorClassLoaderException = e; 1.58 + } 1.59 + } 1.60 + 1.61 private void initProcessorIterator(Context context, Iterable<? extends Processor> processors) { 1.62 Log log = Log.instance(context); 1.63 Iterator<? extends Processor> processorIterator; 1.64 @@ -217,18 +248,7 @@ 1.65 processorIterator = processors.iterator(); 1.66 } else { 1.67 String processorNames = options.get(PROCESSOR); 1.68 - JavaFileManager fileManager = context.get(JavaFileManager.class); 1.69 - try { 1.70 - // If processorpath is not explicitly set, use the classpath. 1.71 - processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) 1.72 - ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH) 1.73 - : fileManager.getClassLoader(CLASS_PATH); 1.74 - 1.75 - if (processorClassLoader != null && processorClassLoader instanceof Closeable) { 1.76 - JavaCompiler compiler = JavaCompiler.instance(context); 1.77 - compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader); 1.78 - } 1.79 - 1.80 + if (processorClassLoaderException == null) { 1.81 /* 1.82 * If the "-processor" option is used, search the appropriate 1.83 * path for the named class. Otherwise, use a service 1.84 @@ -239,14 +259,15 @@ 1.85 } else { 1.86 processorIterator = new ServiceIterator(processorClassLoader, log); 1.87 } 1.88 - } catch (SecurityException e) { 1.89 + } else { 1.90 /* 1.91 * A security exception will occur if we can't create a classloader. 1.92 * Ignore the exception if, with hindsight, we didn't need it anyway 1.93 * (i.e. no processor was specified either explicitly, or implicitly, 1.94 * in service configuration file.) Otherwise, we cannot continue. 1.95 */ 1.96 - processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader", e); 1.97 + processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader", 1.98 + processorClassLoaderException); 1.99 } 1.100 } 1.101 discoveredProcs = new DiscoveredProcessors(processorIterator); 1.102 @@ -1473,13 +1494,19 @@ 1.103 } 1.104 1.105 /** 1.106 - * For internal use only. This method will be 1.107 - * removed without warning. 1.108 + * For internal use only. This method may be removed without warning. 1.109 */ 1.110 public Context getContext() { 1.111 return context; 1.112 } 1.113 1.114 + /** 1.115 + * For internal use only. This method may be removed without warning. 1.116 + */ 1.117 + public ClassLoader getProcessorClassLoader() { 1.118 + return processorClassLoader; 1.119 + } 1.120 + 1.121 public String toString() { 1.122 return "javac ProcessingEnvironment"; 1.123 }