langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
changeset 3764 f0077f165983
parent 3656 d4e34b76b0c3
child 4871 655bba719625
equal deleted inserted replaced
3763:be9e8bdd7ead 3764:f0077f165983
   285      */
   285      */
   286     private class ServiceIterator implements Iterator<Processor> {
   286     private class ServiceIterator implements Iterator<Processor> {
   287         // The to-be-wrapped iterator.
   287         // The to-be-wrapped iterator.
   288         private Iterator<?> iterator;
   288         private Iterator<?> iterator;
   289         private Log log;
   289         private Log log;
       
   290         private Class<?> loaderClass;
       
   291         private boolean jusl;
       
   292         private Object loader;
   290 
   293 
   291         ServiceIterator(ClassLoader classLoader, Log log) {
   294         ServiceIterator(ClassLoader classLoader, Log log) {
   292             Class<?> loaderClass;
       
   293             String loadMethodName;
   295             String loadMethodName;
   294             boolean jusl;
       
   295 
   296 
   296             this.log = log;
   297             this.log = log;
   297             try {
   298             try {
   298                 try {
   299                 try {
   299                     loaderClass = Class.forName("java.util.ServiceLoader");
   300                     loaderClass = Class.forName("java.util.ServiceLoader");
   322                                                   classLoader);
   323                                                   classLoader);
   323 
   324 
   324                 // For java.util.ServiceLoader, we have to call another
   325                 // For java.util.ServiceLoader, we have to call another
   325                 // method to get the iterator.
   326                 // method to get the iterator.
   326                 if (jusl) {
   327                 if (jusl) {
       
   328                     loader = result; // Store ServiceLoader to call reload later
   327                     Method m = loaderClass.getMethod("iterator");
   329                     Method m = loaderClass.getMethod("iterator");
   328                     result = m.invoke(result); // serviceLoader.iterator();
   330                     result = m.invoke(result); // serviceLoader.iterator();
   329                 }
   331                 }
   330 
   332 
   331                 // The result should now be an iterator.
   333                 // The result should now be an iterator.
   362             }
   364             }
   363         }
   365         }
   364 
   366 
   365         public void remove() {
   367         public void remove() {
   366             throw new UnsupportedOperationException();
   368             throw new UnsupportedOperationException();
       
   369         }
       
   370 
       
   371         public void close() {
       
   372             if (jusl) {
       
   373                 try {
       
   374                     // Call java.util.ServiceLoader.reload
       
   375                     Method reloadMethod = loaderClass.getMethod("reload");
       
   376                     reloadMethod.invoke(loader);
       
   377                 } catch(Exception e) {
       
   378                     ; // Ignore problems during a call to reload.
       
   379                 }
       
   380             }
   367         }
   381         }
   368     }
   382     }
   369 
   383 
   370 
   384 
   371     private static class NameProcessIterator implements Iterator<Processor> {
   385     private static class NameProcessIterator implements Iterator<Processor> {
   550     /**
   564     /**
   551      * This class holds information about the processors that have
   565      * This class holds information about the processors that have
   552      * been discoverd so far as well as the means to discover more, if
   566      * been discoverd so far as well as the means to discover more, if
   553      * necessary.  A single iterator should be used per round of
   567      * necessary.  A single iterator should be used per round of
   554      * annotation processing.  The iterator first visits already
   568      * annotation processing.  The iterator first visits already
   555      * discovered processors then fails over to the service provided
   569      * discovered processors then fails over to the service provider
   556      * mechanism if additional queries are made.
   570      * mechanism if additional queries are made.
   557      */
   571      */
   558     class DiscoveredProcessors implements Iterable<ProcessorState> {
   572     class DiscoveredProcessors implements Iterable<ProcessorState> {
   559 
   573 
   560         class ProcessorStateIterator implements Iterator<ProcessorState> {
   574         class ProcessorStateIterator implements Iterator<ProcessorState> {
   621         }
   635         }
   622 
   636 
   623         DiscoveredProcessors(Iterator<? extends Processor> processorIterator) {
   637         DiscoveredProcessors(Iterator<? extends Processor> processorIterator) {
   624             this.processorIterator = processorIterator;
   638             this.processorIterator = processorIterator;
   625             this.procStateList = new ArrayList<ProcessorState>();
   639             this.procStateList = new ArrayList<ProcessorState>();
       
   640         }
       
   641 
       
   642         /**
       
   643          * Free jar files, etc. if using a service loader.
       
   644          */
       
   645         public void close() {
       
   646             if (processorIterator != null &&
       
   647                 processorIterator instanceof ServiceIterator) {
       
   648                 ((ServiceIterator) processorIterator).close();
       
   649             }
   626         }
   650         }
   627     }
   651     }
   628 
   652 
   629     private void discoverAndRunProcs(Context context,
   653     private void discoverAndRunProcs(Context context,
   630                                      Set<TypeElement> annotationsPresent,
   654                                      Set<TypeElement> annotationsPresent,
   908         * should exit with a nonzero exit code.  The current value of
   932         * should exit with a nonzero exit code.  The current value of
   909         * errorStatus holds whether or not an error was raised on the
   933         * errorStatus holds whether or not an error was raised on the
   910         * second to last round; errorRaised() gives the error status
   934         * second to last round; errorRaised() gives the error status
   911         * of the last round.
   935         * of the last round.
   912         */
   936         */
   913        errorStatus = errorStatus || messager.errorRaised();
   937         errorStatus = errorStatus || messager.errorRaised();
   914 
   938 
   915 
   939 
   916         // Free resources
   940         // Free resources
   917         this.close();
   941         this.close();
   918 
   942 
  1021     /**
  1045     /**
  1022      * Free resources related to annotation processing.
  1046      * Free resources related to annotation processing.
  1023      */
  1047      */
  1024     public void close() throws IOException {
  1048     public void close() throws IOException {
  1025         filer.close();
  1049         filer.close();
       
  1050         if (discoveredProcs != null) // Make calling close idempotent
       
  1051             discoveredProcs.close();
  1026         discoveredProcs = null;
  1052         discoveredProcs = null;
  1027         if (processorClassLoader != null && processorClassLoader instanceof Closeable)
  1053         if (processorClassLoader != null && processorClassLoader instanceof Closeable)
  1028             ((Closeable) processorClassLoader).close();
  1054             ((Closeable) processorClassLoader).close();
  1029     }
  1055     }
  1030 
  1056