Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8059012

Messager.printMessage with AnnotationValue does not locate specific source value

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 8u20
    • tools
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) Client VM (build 25.20-b23, mixed mode, sharing)

      javac 1.8.0_20

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Home Premium 64-bit
      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Using 32-bit JDK

      A DESCRIPTION OF THE PROBLEM :
      The API Javadoc states:

      javax.annotation.processing.Messager

      printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v)
      Prints a message of the specified kind at the location of the annotation value inside the annotation mirror of the annotated element.

      However the location reported is the entire annotation, not just one value within it. If run from the command line, javac positions the caret (^) pointing at the start of the annotation. If run through ToolProvider, the position indexes in the resulting Diagnostic object encompass the whole annotation, not just one value.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Call javax.annotation.processing.Messager.printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) and observe the location associated with the resulting Diagnostic object or console output.

      Run with compiler interface:
      javac *.java
      java Run

      Run from command line:
      javac *.java
      javac -processor Proc Test.java


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      From compiler interface Run.java expected:
      21, 21, 34

      From command line expected (requires fixed width font):

      Found annotated element: i
        Found annotation mirror: @Anno(something="x")
          Found value: "x"
      Test.java:2: error: Here's the value
              @Anno(something="x")
                    ^
      1 error


      ACTUAL -
      From compiler interface:
      15, 15, 35

      From command line:

      Found annotated element: i
        Found annotation mirror: @Anno(something="x")
          Found value: "x"
      Test.java:2: error: Here's the value
              @Anno(something="x")
              ^
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      ----- Anno.java -----
      public @interface Anno {
      String something();
      }

      ----- Test.java -----
      class Test {
      @Anno(something="x")
      int i;
      }

      ----- Proc.java -----
      import javax.annotation.processing.*;
      import javax.lang.model.SourceVersion;
      import javax.tools.Diagnostic.Kind;
      import javax.lang.model.element.*;
      import java.util.*;

      @SupportedAnnotationTypes("Anno")
      @SupportedSourceVersion(SourceVersion.RELEASE_8)
      public class Proc extends AbstractProcessor {
      @Override
      public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
      Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Anno.class);
      for (Element el : elements) {
      System.out.println("Found annotated element: " + el);
      for (AnnotationMirror mir : el.getAnnotationMirrors()) {
      System.out.println(" Found annotation mirror: " + mir);
      AnnotationValue val = mir.getElementValues().values().iterator().next();
      System.out.println(" Found value: " + val);
      processingEnv.getMessager().printMessage(Kind.ERROR, "Here's the value",
      el, mir, val);
      }
      }
      return true;
      }
      }

      ----- Run.java -----
      import java.util.*;
      import javax.tools.*;

      public class Run {
      public static void main(String[] args) {
      JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
      StandardJavaFileManager fileMan = javac.getStandardFileManager(null, null, null);
      DiagnosticCollector<JavaFileObject> diag = new DiagnosticCollector<>();
      Iterable<? extends JavaFileObject> files = fileMan.getJavaFileObjects("Test.java");
      JavaCompiler.CompilationTask task = javac.getTask(null, fileMan, diag, null, null, files);
      task.setProcessors(Arrays.asList(new Proc()));
      task.call();
      Diagnostic<?> d = diag.getDiagnostics().get(0);
      System.out.println(d.getStartPosition() + ", " + d.getPosition() + ", " + d.getEndPosition());
      }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: