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

parameter modifiers are not visible to javac plugins across compilation boundaries

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 17
    • None
    • tools
    • b23

      javac fails to read modifiers from the MethodParameters attribute in class files, which prevents plugins from accessing those modifiers across compilation boundaries. The modifiers are handled correctly if the same symbol is compiled from source in the compilation where the plugin runs.

      === ./plugin/module-info.java
      module p {
        requires transitive jdk.compiler;
        provides com.sun.source.util.Plugin with p.P;
      }
      === ./plugin/p/P.java
      package p;

      import com.sun.source.util.JavacTask;
      import com.sun.source.util.Plugin;
      import com.sun.source.util.TaskEvent;
      import com.sun.source.util.TaskListener;

      import javax.lang.model.element.Element;
      import javax.lang.model.element.ExecutableElement;
      import javax.lang.model.element.TypeElement;
      import javax.lang.model.element.VariableElement;

      public class P implements Plugin {

        @Override
        public String getName() {
          return "P";
        }

        @Override
        public void init(JavacTask javacTask, String... strings) {
          javacTask.addTaskListener(
              new TaskListener() {
                @Override
                public void finished(TaskEvent e) {
                  if (e.getKind() != TaskEvent.Kind.ENTER) {
                    return;
                  }
                  TypeElement b = javacTask.getElements().getTypeElement("B");
                  for (Element m : b.getEnclosedElements()) {
                    if (m instanceof ExecutableElement) {
                      for (VariableElement p : ((ExecutableElement) m).getParameters()) {
                        System.err.println(p.getSimpleName() + " " + p.getModifiers());
                      }
                    }
                  }
                }
              });
        }
      }

      === ./test/A.java
      class A {}
      === ./test/B.java
      class B {
        void f(final int x) {}
      }
      ===

      $ javac $(find plugin -name '*.java')

      # the final modifier is observable on parameters in the current compilation

      $ javac --processor-module-path plugin -Xplugin:P -parameters test/A.java test/B.java
      x [final]
      x [final]

      # the final modifier is not observable on parameters loaded from class files

      $ javac --processor-module-path plugin -Xplugin:P -parameters -classpath test test/A.java
      x []

            gli Guoxiong Li
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: