6575445: Update annotation processor to only use java.util.ServiceLoader

Fri, 24 Jun 2011 13:52:14 -0700

author
darcy
date
Fri, 24 Jun 2011 13:52:14 -0700
changeset 1048
f74e4269a50a
parent 1047
9eb36cac6b64
child 1049
858ae8fec72f

6575445: Update annotation processor to only use java.util.ServiceLoader
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/resources/compiler.properties file | annotate | diff | comparison | revisions
test/tools/javac/diags/examples.not-yet.txt file | annotate | diff | comparison | revisions
     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                  }
     2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jun 23 17:30:49 2011 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Jun 24 13:52:14 2011 -0700
     2.3 @@ -637,8 +637,7 @@
     2.4      Class names, ''{0}'', are only accepted if annotation processing is explicitly requested
     2.5  
     2.6  compiler.err.proc.no.service=\
     2.7 -    A service loader class could not be found.\n\
     2.8 -    Either java.util.ServiceLoader or sun.misc.Service must be available.
     2.9 +    A ServiceLoader was not usable and is required for annotation processing.
    2.10  
    2.11  compiler.err.proc.processor.bad.option.name=\
    2.12      Bad option name ''{0}'' provided by processor ''{1}''
    2.13 @@ -647,9 +646,6 @@
    2.14  compiler.err.proc.processor.cant.instantiate=\
    2.15      Could not instantiate an instance of processor ''{0}''
    2.16  
    2.17 -compiler.err.proc.processor.constructor.error=\
    2.18 -    Exception thrown while constructing Processor object: {0}
    2.19 -
    2.20  # 0: string
    2.21  compiler.err.proc.processor.not.found=\
    2.22      Annotation processor ''{0}'' not found
     3.1 --- a/test/tools/javac/diags/examples.not-yet.txt	Thu Jun 23 17:30:49 2011 -0700
     3.2 +++ b/test/tools/javac/diags/examples.not-yet.txt	Fri Jun 24 13:52:14 2011 -0700
     3.3 @@ -31,7 +31,6 @@
     3.4  compiler.err.proc.cant.create.loader                    # security exception from service loader
     3.5  compiler.err.proc.no.service                            # JavacProcessingEnvironment: no service loader available
     3.6  compiler.err.proc.processor.bad.option.name             # cannot happen? masked by javac.err.invalid.A.key
     3.7 -compiler.err.proc.processor.constructor.error
     3.8  compiler.err.proc.service.problem                       # JavacProcessingEnvironment: catch Throwable from service loader
     3.9  compiler.err.signature.doesnt.match.intf                # UNUSED
    3.10  compiler.err.signature.doesnt.match.supertype           # UNUSED

mercurial