langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
changeset 45504 ea7475564d07
parent 44291 e1b620ac6c98
child 45744 db6aedca2c8c
equal deleted inserted replaced
45503:d23ae2d67a5d 45504:ea7475564d07
    67 import com.sun.tools.javac.model.JavacElements;
    67 import com.sun.tools.javac.model.JavacElements;
    68 import com.sun.tools.javac.model.JavacTypes;
    68 import com.sun.tools.javac.model.JavacTypes;
    69 import com.sun.tools.javac.platform.PlatformDescription;
    69 import com.sun.tools.javac.platform.PlatformDescription;
    70 import com.sun.tools.javac.platform.PlatformDescription.PluginInfo;
    70 import com.sun.tools.javac.platform.PlatformDescription.PluginInfo;
    71 import com.sun.tools.javac.resources.CompilerProperties.Errors;
    71 import com.sun.tools.javac.resources.CompilerProperties.Errors;
       
    72 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
    72 import com.sun.tools.javac.tree.*;
    73 import com.sun.tools.javac.tree.*;
    73 import com.sun.tools.javac.tree.JCTree.*;
    74 import com.sun.tools.javac.tree.JCTree.*;
    74 import com.sun.tools.javac.util.Abort;
    75 import com.sun.tools.javac.util.Abort;
    75 import com.sun.tools.javac.util.Assert;
    76 import com.sun.tools.javac.util.Assert;
    76 import com.sun.tools.javac.util.ClientCodeException;
    77 import com.sun.tools.javac.util.ClientCodeException;
   410                 } catch (Exception e) {
   411                 } catch (Exception e) {
   411                     // Fail softly if a loader is not actually needed.
   412                     // Fail softly if a loader is not actually needed.
   412                     this.iterator = handleServiceLoaderUnavailability("proc.no.service", null);
   413                     this.iterator = handleServiceLoaderUnavailability("proc.no.service", null);
   413                 }
   414                 }
   414             } catch (Throwable t) {
   415             } catch (Throwable t) {
   415                 log.error("proc.service.problem");
   416                 log.error(Errors.ProcServiceProblem);
   416                 throw new Abort(t);
   417                 throw new Abort(t);
   417             }
   418             }
   418         }
   419         }
   419 
   420 
   420         ServiceIterator(ServiceLoader<Processor> loader, Log log) {
   421         ServiceIterator(ServiceLoader<Processor> loader, Log log) {
   426         @Override
   427         @Override
   427         public boolean hasNext() {
   428         public boolean hasNext() {
   428             try {
   429             try {
   429                 return internalHasNext();
   430                 return internalHasNext();
   430             } catch(ServiceConfigurationError sce) {
   431             } catch(ServiceConfigurationError sce) {
   431                 log.error("proc.bad.config.file", sce.getLocalizedMessage());
   432                 log.error(Errors.ProcBadConfigFile(sce.getLocalizedMessage()));
   432                 throw new Abort(sce);
   433                 throw new Abort(sce);
   433             } catch (Throwable t) {
   434             } catch (Throwable t) {
   434                 throw new Abort(t);
   435                 throw new Abort(t);
   435             }
   436             }
   436         }
   437         }
   442         @Override
   443         @Override
   443         public Processor next() {
   444         public Processor next() {
   444             try {
   445             try {
   445                 return internalNext();
   446                 return internalNext();
   446             } catch (ServiceConfigurationError sce) {
   447             } catch (ServiceConfigurationError sce) {
   447                 log.error("proc.bad.config.file", sce.getLocalizedMessage());
   448                 log.error(Errors.ProcBadConfigFile(sce.getLocalizedMessage()));
   448                 throw new Abort(sce);
   449                 throw new Abort(sce);
   449             } catch (Throwable t) {
   450             } catch (Throwable t) {
   450                 throw new Abort(t);
   451                 throw new Abort(t);
   451             }
   452             }
   452         }
   453         }
   559                 try {
   560                 try {
   560                     Class<?> processorClass = processorCL.loadClass(processorName);
   561                     Class<?> processorClass = processorCL.loadClass(processorName);
   561                     ensureReadable(processorClass);
   562                     ensureReadable(processorClass);
   562                     return (Processor) processorClass.getConstructor().newInstance();
   563                     return (Processor) processorClass.getConstructor().newInstance();
   563                 } catch (ClassNotFoundException cnfe) {
   564                 } catch (ClassNotFoundException cnfe) {
   564                     log.error("proc.processor.not.found", processorName);
   565                     log.error(Errors.ProcProcessorNotFound(processorName));
   565                     return null;
   566                     return null;
   566                 } catch (ClassCastException cce) {
   567                 } catch (ClassCastException cce) {
   567                     log.error("proc.processor.wrong.type", processorName);
   568                     log.error(Errors.ProcProcessorWrongType(processorName));
   568                     return null;
   569                     return null;
   569                 } catch (Exception e ) {
   570                 } catch (Exception e ) {
   570                     log.error("proc.processor.cant.instantiate", processorName);
   571                     log.error(Errors.ProcProcessorCantInstantiate(processorName));
   571                     return null;
   572                     return null;
   572                 }
   573                 }
   573             } catch (ClientCodeException e) {
   574             } catch (ClientCodeException e) {
   574                 throw e;
   575                 throw e;
   575             } catch (Throwable t) {
   576             } catch (Throwable t) {
   705          */
   706          */
   706         private void checkSourceVersionCompatibility(Source source, Log log) {
   707         private void checkSourceVersionCompatibility(Source source, Log log) {
   707             SourceVersion procSourceVersion = processor.getSupportedSourceVersion();
   708             SourceVersion procSourceVersion = processor.getSupportedSourceVersion();
   708 
   709 
   709             if (procSourceVersion.compareTo(Source.toSourceVersion(source)) < 0 )  {
   710             if (procSourceVersion.compareTo(Source.toSourceVersion(source)) < 0 )  {
   710                 log.warning("proc.processor.incompatible.source.version",
   711                 log.warning(Warnings.ProcProcessorIncompatibleSourceVersion(procSourceVersion,
   711                             procSourceVersion,
   712                                                                             processor.getClass().getName(),
   712                             processor.getClass().getName(),
   713                                                                             source.name));
   713                             source.name);
       
   714             }
   714             }
   715         }
   715         }
   716 
   716 
   717         private boolean checkOptionName(String optionName, Log log) {
   717         private boolean checkOptionName(String optionName, Log log) {
   718             boolean valid = isValidOptionName(optionName);
   718             boolean valid = isValidOptionName(optionName);
   719             if (!valid)
   719             if (!valid)
   720                 log.error("proc.processor.bad.option.name",
   720                 log.error(Errors.ProcProcessorBadOptionName(optionName,
   721                             optionName,
   721                                                             processor.getClass().getName()));
   722                             processor.getClass().getName());
       
   723             return valid;
   722             return valid;
   724         }
   723         }
   725 
   724 
   726         public boolean annotationSupported(String annotationName) {
   725         public boolean annotationSupported(String annotationName) {
   727             for(Pattern p: supportedAnnotationPatterns) {
   726             for(Pattern p: supportedAnnotationPatterns) {
   902 
   901 
   903         if (lint && unmatchedAnnotations.size() > 0) {
   902         if (lint && unmatchedAnnotations.size() > 0) {
   904             // Remove annotations processed by javac
   903             // Remove annotations processed by javac
   905             unmatchedAnnotations.keySet().removeAll(platformAnnotations);
   904             unmatchedAnnotations.keySet().removeAll(platformAnnotations);
   906             if (unmatchedAnnotations.size() > 0) {
   905             if (unmatchedAnnotations.size() > 0) {
   907                 log.warning("proc.annotations.without.processors",
   906                 log.warning(Warnings.ProcAnnotationsWithoutProcessors(unmatchedAnnotations.keySet()));
   908                             unmatchedAnnotations.keySet());
       
   909             }
   907             }
   910         }
   908         }
   911 
   909 
   912         // Run contributing processors that haven't run yet
   910         // Run contributing processors that haven't run yet
   913         psi.runContributingProcs(renv);
   911         psi.runContributingProcs(renv);
   965                                          Set<? extends TypeElement> tes,
   963                                          Set<? extends TypeElement> tes,
   966                                          RoundEnvironment renv) {
   964                                          RoundEnvironment renv) {
   967         try {
   965         try {
   968             return proc.process(tes, renv);
   966             return proc.process(tes, renv);
   969         } catch (ClassFinder.BadClassFile ex) {
   967         } catch (ClassFinder.BadClassFile ex) {
   970             log.error("proc.cant.access.1", ex.sym, ex.getDetailValue());
   968             log.error(Errors.ProcCantAccess1(ex.sym, ex.getDetailValue()));
   971             return false;
   969             return false;
   972         } catch (CompletionFailure ex) {
   970         } catch (CompletionFailure ex) {
   973             StringWriter out = new StringWriter();
   971             StringWriter out = new StringWriter();
   974             ex.printStackTrace(new PrintWriter(out));
   972             ex.printStackTrace(new PrintWriter(out));
   975             log.error("proc.cant.access", ex.sym, ex.getDetailValue(), out.toString());
   973             log.error(Errors.ProcCantAccess(ex.sym, ex.getDetailValue(), out.toString()));
   976             return false;
   974             return false;
   977         } catch (ClientCodeException e) {
   975         } catch (ClientCodeException e) {
   978             throw e;
   976             throw e;
   979         } catch (Throwable t) {
   977         } catch (Throwable t) {
   980             throw new AnnotationProcessingError(t);
   978             throw new AnnotationProcessingError(t);
  1383         return true;
  1381         return true;
  1384     }
  1382     }
  1385 
  1383 
  1386     private void warnIfUnmatchedOptions() {
  1384     private void warnIfUnmatchedOptions() {
  1387         if (!unmatchedProcessorOptions.isEmpty()) {
  1385         if (!unmatchedProcessorOptions.isEmpty()) {
  1388             log.warning("proc.unmatched.processor.options", unmatchedProcessorOptions.toString());
  1386             log.warning(Warnings.ProcUnmatchedProcessorOptions(unmatchedProcessorOptions.toString()));
  1389         }
  1387         }
  1390     }
  1388     }
  1391 
  1389 
  1392     /**
  1390     /**
  1393      * Free resources related to annotation processing.
  1391      * Free resources related to annotation processing.
  1488                     return true;
  1486                     return true;
  1489             } catch (MalformedURLException ex) {
  1487             } catch (MalformedURLException ex) {
  1490                 throw new AssertionError(ex);
  1488                 throw new AssertionError(ex);
  1491             }
  1489             }
  1492             catch (ServiceProxy.ServiceConfigurationError e) {
  1490             catch (ServiceProxy.ServiceConfigurationError e) {
  1493                 log.error("proc.bad.config.file", e.getLocalizedMessage());
  1491                 log.error(Errors.ProcBadConfigFile(e.getLocalizedMessage()));
  1494                 return true;
  1492                 return true;
  1495             }
  1493             }
  1496         }
  1494         }
  1497 
  1495 
  1498         return false;
  1496         return false;
  1680             pkg = s.substring(slash + 1);
  1678             pkg = s.substring(slash + 1);
  1681         }
  1679         }
  1682         if (MatchingUtils.isValidImportString(pkg)) {
  1680         if (MatchingUtils.isValidImportString(pkg)) {
  1683             return Pattern.compile(module + MatchingUtils.validImportStringToPatternString(pkg));
  1681             return Pattern.compile(module + MatchingUtils.validImportStringToPatternString(pkg));
  1684         } else {
  1682         } else {
  1685             log.warning("proc.malformed.supported.string", s, p.getClass().getName());
  1683             log.warning(Warnings.ProcMalformedSupportedString(s, p.getClass().getName()));
  1686             return noMatches; // won't match any valid identifier
  1684             return noMatches; // won't match any valid identifier
  1687         }
  1685         }
  1688     }
  1686     }
  1689 
  1687 
  1690     /**
  1688     /**