src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java

Print this page




 278         java.util.List<Processor> pl = Collections.emptyList();
 279         return pl.iterator();
 280     }
 281 
 282     /**
 283      * Handle a security exception thrown during initializing the
 284      * Processor iterator.
 285      */
 286     private void handleException(String key, Exception e) {
 287         if (e != null) {
 288             log.error(key, e.getLocalizedMessage());
 289             throw new Abort(e);
 290         } else {
 291             log.error(key);
 292             throw new Abort();
 293         }
 294     }
 295 
 296     /**
 297      * Use a service loader appropriate for the platform to provide an
 298      * iterator over annotations processors.  If
 299      * java.util.ServiceLoader is present use it, otherwise, use
 300      * sun.misc.Service, otherwise fail if a loader is needed.
 301      */
 302     private class ServiceIterator implements Iterator<Processor> {
 303         // The to-be-wrapped iterator.
 304         private Iterator<?> iterator;
 305         private Log log;
 306         private Class<?> loaderClass;
 307         private boolean jusl;
 308         private Object loader;
 309 
 310         ServiceIterator(ClassLoader classLoader, Log log) {
 311             String loadMethodName;
 312 
 313             this.log = log;
 314             try {
 315                 try {
 316                     loaderClass = Class.forName("java.util.ServiceLoader");
 317                     loadMethodName = "load";
 318                     jusl = true;
 319                 } catch (ClassNotFoundException cnfe) {
 320                     try {
 321                         loaderClass = Class.forName("sun.misc.Service");
 322                         loadMethodName = "providers";
 323                         jusl = false;
 324                     } catch (ClassNotFoundException cnfe2) {
 325                         // Fail softly if a loader is not actually needed.
 326                         this.iterator = handleServiceLoaderUnavailability("proc.no.service",
 327                                                                           null);
 328                         return;
 329                     }
 330                 }
 331 
 332                 // java.util.ServiceLoader.load or sun.misc.Service.providers
 333                 Method loadMethod = loaderClass.getMethod(loadMethodName,
 334                                                           Class.class,
 335                                                           ClassLoader.class);
 336 
 337                 Object result = loadMethod.invoke(null,
 338                                                   Processor.class,
 339                                                   classLoader);
 340 
 341                 // For java.util.ServiceLoader, we have to call another
 342                 // method to get the iterator.
 343                 if (jusl) {
 344                     loader = result; // Store ServiceLoader to call reload later
 345                     Method m = loaderClass.getMethod("iterator");
 346                     result = m.invoke(result); // serviceLoader.iterator();
 347                 }
 348 
 349                 // The result should now be an iterator.
 350                 this.iterator = (Iterator<?>) result;
 351             } catch (Throwable t) {
 352                 log.error("proc.service.problem");
 353                 throw new Abort(t);
 354             }
 355         }
 356 
 357         public boolean hasNext() {
 358             try {
 359                 return iterator.hasNext();



 360             } catch (Throwable t) {
 361                 if ("ServiceConfigurationError".
 362                     equals(t.getClass().getSimpleName())) {
 363                     log.error("proc.bad.config.file", t.getLocalizedMessage());
 364                 }
 365                 throw new Abort(t);
 366             }
 367         }
 368 
 369         public Processor next() {
 370             try {
 371                 return (Processor)(iterator.next());



 372             } catch (Throwable t) {
 373                 if ("ServiceConfigurationError".
 374                     equals(t.getClass().getSimpleName())) {
 375                     log.error("proc.bad.config.file", t.getLocalizedMessage());
 376                 } else {
 377                     log.error("proc.processor.constructor.error", t.getLocalizedMessage());
 378                 }
 379                 throw new Abort(t);
 380             }
 381         }
 382 
 383         public void remove() {
 384             throw new UnsupportedOperationException();
 385         }
 386 
 387         public void close() {
 388             if (jusl) {
 389                 try {
 390                     // Call java.util.ServiceLoader.reload
 391                     Method reloadMethod = loaderClass.getMethod("reload");
 392                     reloadMethod.invoke(loader);
 393                 } catch(Exception e) {
 394                     ; // Ignore problems during a call to reload.
 395                 }
 396             }
 397         }
 398     }
 399 
 400 
 401     private static class NameProcessIterator implements Iterator<Processor> {
 402         Processor nextProc = null;
 403         Iterator<String> names;
 404         ClassLoader processorCL;
 405         Log log;
 406 
 407         NameProcessIterator(String names, ClassLoader processorCL, Log log) {
 408             this.names = Arrays.asList(names.split(",")).iterator();
 409             this.processorCL = processorCL;
 410             this.log = log;
 411         }
 412 




 278         java.util.List<Processor> pl = Collections.emptyList();
 279         return pl.iterator();
 280     }
 281 
 282     /**
 283      * Handle a security exception thrown during initializing the
 284      * Processor iterator.
 285      */
 286     private void handleException(String key, Exception e) {
 287         if (e != null) {
 288             log.error(key, e.getLocalizedMessage());
 289             throw new Abort(e);
 290         } else {
 291             log.error(key);
 292             throw new Abort();
 293         }
 294     }
 295 
 296     /**
 297      * Use a service loader appropriate for the platform to provide an
 298      * iterator over annotations processors; fails if a loader is
 299      * needed but unavailable.

 300      */
 301     private class ServiceIterator implements Iterator<Processor> {
 302         private Iterator<Processor> iterator;

 303         private Log log;
 304         private ServiceLoader<Processor> loader;


 305 
 306         ServiceIterator(ClassLoader classLoader, Log log) {


 307             this.log = log;
 308             try {
 309                 try {
 310                     loader = ServiceLoader.load(Processor.class, classLoader);
 311                     this.iterator = loader.iterator();
 312                 } catch (Exception e) {






 313                     // Fail softly if a loader is not actually needed.
 314                     this.iterator = handleServiceLoaderUnavailability("proc.no.service", null);



 315                 }




















 316             } catch (Throwable t) {
 317                 log.error("proc.service.problem");
 318                 throw new Abort(t);
 319             }
 320         }
 321 
 322         public boolean hasNext() {
 323             try {
 324                 return iterator.hasNext();
 325             } catch(ServiceConfigurationError sce) {
 326                 log.error("proc.bad.config.file", sce.getLocalizedMessage());
 327                 throw new Abort(sce);
 328             } catch (Throwable t) {




 329                 throw new Abort(t);
 330             }
 331         }
 332 
 333         public Processor next() {
 334             try {
 335                 return iterator.next();
 336             } catch (ServiceConfigurationError sce) {
 337                 log.error("proc.bad.config.file", sce.getLocalizedMessage());
 338                 throw new Abort(sce);
 339             } catch (Throwable t) {






 340                 throw new Abort(t);
 341             }
 342         }
 343 
 344         public void remove() {
 345             throw new UnsupportedOperationException();
 346         }
 347 
 348         public void close() {
 349             if (loader != null) {
 350                 try {
 351                     loader.reload();


 352                 } catch(Exception e) {
 353                     ; // Ignore problems during a call to reload.
 354                 }
 355             }
 356         }
 357     }
 358 
 359 
 360     private static class NameProcessIterator implements Iterator<Processor> {
 361         Processor nextProc = null;
 362         Iterator<String> names;
 363         ClassLoader processorCL;
 364         Log log;
 365 
 366         NameProcessIterator(String names, ClassLoader processorCL, Log log) {
 367             this.names = Arrays.asList(names.split(",")).iterator();
 368             this.processorCL = processorCL;
 369             this.log = log;
 370         }
 371