8172158: Annotation processor not run with -source <= 8
authorjlahoda
Fri, 06 Jan 2017 14:16:45 +0100
changeset 43037 3e1520a857fa
parent 43036 265376df8b48
child 43038 7b8b8750a78e
8172158: Annotation processor not run with -source <= 8 Summary: Avoiding use of module prefix to map annotations to processors when running without modules. Reviewed-by: darcy, jjg
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
langtools/test/tools/javac/modules/AnnotationProcessing.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Thu Jan 05 15:39:57 2017 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Fri Jan 06 14:16:45 2017 +0100
@@ -114,6 +114,7 @@
     private final boolean fatalErrors;
     private final boolean werror;
     private final boolean showResolveErrors;
+    private final boolean allowModules;
 
     private final JavacFiler filer;
     private final JavacMessager messager;
@@ -178,7 +179,6 @@
     private final Enter enter;
     private final Completer initialCompleter;
     private final Check chk;
-    private final ModuleSymbol defaultModule;
 
     private final Context context;
 
@@ -230,8 +230,7 @@
         chk = Check.instance(context);
         initProcessorLoader();
 
-        defaultModule = source.allowModules() && options.isUnset("noModules")
-                ? symtab.unnamedModule : symtab.noModule;
+        allowModules = source.allowModules() && options.isUnset("noModules");
     }
 
     public void setProcessors(Iterable<? extends Processor> processors) {
@@ -665,7 +664,7 @@
         private ArrayList<Pattern> supportedAnnotationPatterns;
         private ArrayList<String>  supportedOptionNames;
 
-        ProcessorState(Processor p, Log log, Source source, ProcessingEnvironment env) {
+        ProcessorState(Processor p, Log log, Source source, boolean allowModules, ProcessingEnvironment env) {
             processor = p;
             contributed = false;
 
@@ -676,7 +675,8 @@
 
                 supportedAnnotationPatterns = new ArrayList<>();
                 for (String importString : processor.getSupportedAnnotationTypes()) {
-                    supportedAnnotationPatterns.add(importStringToPattern(importString,
+                    supportedAnnotationPatterns.add(importStringToPattern(allowModules,
+                                                                          importString,
                                                                           processor,
                                                                           log));
                 }
@@ -768,7 +768,8 @@
 
                 if (psi.processorIterator.hasNext()) {
                     ProcessorState ps = new ProcessorState(psi.processorIterator.next(),
-                                                           log, source, JavacProcessingEnvironment.this);
+                                                           log, source, allowModules,
+                                                           JavacProcessingEnvironment.this);
                     psi.procStateList.add(ps);
                     return ps;
                 } else
@@ -834,7 +835,8 @@
 
         for(TypeElement a  : annotationsPresent) {
             ModuleElement mod = elementUtils.getModuleOf(a);
-            unmatchedAnnotations.put((mod != null ? mod.getSimpleName() + "/" : "") + a.getQualifiedName().toString(),
+            String moduleSpec = allowModules && mod != null ? mod.getSimpleName() + "/" : "";
+            unmatchedAnnotations.put(moduleSpec + a.getQualifiedName().toString(),
                                      a);
         }
 
@@ -1657,7 +1659,7 @@
      * regex matching that string.  If the string is not a valid
      * import-style string, return a regex that won't match anything.
      */
-    private static Pattern importStringToPattern(String s, Processor p, Log log) {
+    private static Pattern importStringToPattern(boolean allowModules, String s, Processor p, Log log) {
         String module;
         String pkg;
         int slash = s.indexOf('/');
@@ -1672,7 +1674,7 @@
             pkg = s.substring(slash + 1);
         }
         if (MatchingUtils.isValidImportString(pkg)) {
-            return Pattern.compile(module + MatchingUtils.validImportStringToPatternString(pkg));
+            return Pattern.compile((allowModules ? module : "") + MatchingUtils.validImportStringToPatternString(pkg));
         } else {
             log.warning("proc.malformed.supported.string", s, p.getClass().getName());
             return noMatches; // won't match any valid identifier
--- a/langtools/test/tools/javac/modules/AnnotationProcessing.java	Thu Jan 05 15:39:57 2017 -0800
+++ b/langtools/test/tools/javac/modules/AnnotationProcessing.java	Fri Jan 06 14:16:45 2017 +0100
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8133884 8162711 8133896
+ * @bug 8133884 8162711 8133896 8172158
  * @summary Verify that annotation processing works.
  * @library /tools/lib
  * @modules
@@ -979,6 +979,38 @@
         }
     }
 
+    @Test
+    public void testDisambiguateAnnotationsNoModules(Path base) throws Exception {
+        Path classes = base.resolve("classes");
+
+        Files.createDirectories(classes);
+
+        Path src = base.resolve("src");
+
+        tb.writeJavaFiles(src,
+                          "package api; public @interface A {}",
+                          "package api; public @interface B {}",
+                          "package impl; import api.*; @A @B public class T {}");
+
+        List<String> log = new JavacTask(tb)
+            .options("-processor", SelectAnnotationATestAP.class.getName() + "," + SelectAnnotationBTestAP.class.getName(),
+                     "-source", "8", "-target", "8")
+            .outdir(classes)
+            .files(findJavaFiles(src))
+            .run()
+            .writeAll()
+            .getOutputLines(OutputKind.STDERR);
+
+        List<String> expected = Arrays.asList("SelectAnnotationATestAP",
+                                              "SelectAnnotationBTestAP",
+                                              "SelectAnnotationATestAP",
+                                              "SelectAnnotationBTestAP");
+
+        if (!expected.equals(log)) {
+            throw new AssertionError("Output does not match; output: " + log);
+        }
+    }
+
     @SupportedAnnotationTypes("m2x/api.A")
     public static final class SelectAnnotationATestAP extends AbstractProcessor {