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

changeset 1048
f74e4269a50a
parent 1044
4844a9fd3a62
child 1054
111bbf1ad913
     1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Thu Jun 23 17:30:49 2011 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Fri Jun 24 13:52:14 2011 -0700
     1.3 @@ -295,59 +295,24 @@
     1.4  
     1.5      /**
     1.6       * Use a service loader appropriate for the platform to provide an
     1.7 -     * iterator over annotations processors.  If
     1.8 -     * java.util.ServiceLoader is present use it, otherwise, use
     1.9 -     * sun.misc.Service, otherwise fail if a loader is needed.
    1.10 +     * iterator over annotations processors; fails if a loader is
    1.11 +     * needed but unavailable.
    1.12       */
    1.13      private class ServiceIterator implements Iterator<Processor> {
    1.14 -        // The to-be-wrapped iterator.
    1.15 -        private Iterator<?> iterator;
    1.16 +        private Iterator<Processor> iterator;
    1.17          private Log log;
    1.18 -        private Class<?> loaderClass;
    1.19 -        private boolean jusl;
    1.20 -        private Object loader;
    1.21 +        private ServiceLoader<Processor> loader;
    1.22  
    1.23          ServiceIterator(ClassLoader classLoader, Log log) {
    1.24 -            String loadMethodName;
    1.25 -
    1.26              this.log = log;
    1.27              try {
    1.28                  try {
    1.29 -                    loaderClass = Class.forName("java.util.ServiceLoader");
    1.30 -                    loadMethodName = "load";
    1.31 -                    jusl = true;
    1.32 -                } catch (ClassNotFoundException cnfe) {
    1.33 -                    try {
    1.34 -                        loaderClass = Class.forName("sun.misc.Service");
    1.35 -                        loadMethodName = "providers";
    1.36 -                        jusl = false;
    1.37 -                    } catch (ClassNotFoundException cnfe2) {
    1.38 -                        // Fail softly if a loader is not actually needed.
    1.39 -                        this.iterator = handleServiceLoaderUnavailability("proc.no.service",
    1.40 -                                                                          null);
    1.41 -                        return;
    1.42 -                    }
    1.43 +                    loader = ServiceLoader.load(Processor.class, classLoader);
    1.44 +                    this.iterator = loader.iterator();
    1.45 +                } catch (Exception e) {
    1.46 +                    // Fail softly if a loader is not actually needed.
    1.47 +                    this.iterator = handleServiceLoaderUnavailability("proc.no.service", null);
    1.48                  }
    1.49 -
    1.50 -                // java.util.ServiceLoader.load or sun.misc.Service.providers
    1.51 -                Method loadMethod = loaderClass.getMethod(loadMethodName,
    1.52 -                                                          Class.class,
    1.53 -                                                          ClassLoader.class);
    1.54 -
    1.55 -                Object result = loadMethod.invoke(null,
    1.56 -                                                  Processor.class,
    1.57 -                                                  classLoader);
    1.58 -
    1.59 -                // For java.util.ServiceLoader, we have to call another
    1.60 -                // method to get the iterator.
    1.61 -                if (jusl) {
    1.62 -                    loader = result; // Store ServiceLoader to call reload later
    1.63 -                    Method m = loaderClass.getMethod("iterator");
    1.64 -                    result = m.invoke(result); // serviceLoader.iterator();
    1.65 -                }
    1.66 -
    1.67 -                // The result should now be an iterator.
    1.68 -                this.iterator = (Iterator<?>) result;
    1.69              } catch (Throwable t) {
    1.70                  log.error("proc.service.problem");
    1.71                  throw new Abort(t);
    1.72 @@ -357,25 +322,21 @@
    1.73          public boolean hasNext() {
    1.74              try {
    1.75                  return iterator.hasNext();
    1.76 +            } catch(ServiceConfigurationError sce) {
    1.77 +                log.error("proc.bad.config.file", sce.getLocalizedMessage());
    1.78 +                throw new Abort(sce);
    1.79              } catch (Throwable t) {
    1.80 -                if ("ServiceConfigurationError".
    1.81 -                    equals(t.getClass().getSimpleName())) {
    1.82 -                    log.error("proc.bad.config.file", t.getLocalizedMessage());
    1.83 -                }
    1.84                  throw new Abort(t);
    1.85              }
    1.86          }
    1.87  
    1.88          public Processor next() {
    1.89              try {
    1.90 -                return (Processor)(iterator.next());
    1.91 +                return iterator.next();
    1.92 +            } catch (ServiceConfigurationError sce) {
    1.93 +                log.error("proc.bad.config.file", sce.getLocalizedMessage());
    1.94 +                throw new Abort(sce);
    1.95              } catch (Throwable t) {
    1.96 -                if ("ServiceConfigurationError".
    1.97 -                    equals(t.getClass().getSimpleName())) {
    1.98 -                    log.error("proc.bad.config.file", t.getLocalizedMessage());
    1.99 -                } else {
   1.100 -                    log.error("proc.processor.constructor.error", t.getLocalizedMessage());
   1.101 -                }
   1.102                  throw new Abort(t);
   1.103              }
   1.104          }
   1.105 @@ -385,11 +346,9 @@
   1.106          }
   1.107  
   1.108          public void close() {
   1.109 -            if (jusl) {
   1.110 +            if (loader != null) {
   1.111                  try {
   1.112 -                    // Call java.util.ServiceLoader.reload
   1.113 -                    Method reloadMethod = loaderClass.getMethod("reload");
   1.114 -                    reloadMethod.invoke(loader);
   1.115 +                    loader.reload();
   1.116                  } catch(Exception e) {
   1.117                      ; // Ignore problems during a call to reload.
   1.118                  }

mercurial