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

ExecutableElement.getReceiverType doesn't return receiver types for methods loaded from bytecode

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 22
    • None
    • tools
    • b24

      javac does not initialize receiver types of methods read from class files, which results in ExecutableElement.getReceiverType incorrectly reporting that methods don't have a receiver type.

      Repro:

      === P.java
      import java.util.Set;
      import javax.annotation.processing.AbstractProcessor;
      import javax.annotation.processing.RoundEnvironment;
      import javax.annotation.processing.SupportedAnnotationTypes;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.Element;
      import javax.lang.model.element.ExecutableElement;
      import javax.lang.model.element.TypeElement;
      import javax.tools.Diagnostic;

      @SupportedAnnotationTypes("*")
      public class P extends AbstractProcessor {
        @Override
        public SourceVersion getSupportedSourceVersion() {
          return SourceVersion.latestSupported();
        }

        boolean first = true;

        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
          if (first) {
            for (Element e :
                processingEnv.getElementUtils().getTypeElement("Test").getEnclosedElements()) {
              if (e.getSimpleName().contentEquals("f")) {
                ExecutableElement f = (ExecutableElement) e;
                processingEnv
                    .getMessager()
                    .printMessage(Diagnostic.Kind.NOTE, f.getReceiverType().toString(), f);
              }
            }
            first = false;
          }
          return false;
        }
      }
      === Test.java
      class Test {
        void f(Test this) {}
      }
      ===

      The receiver type is available if 'Test' is compiled from source:

      $ javac -processor P -implicit:none Test.java
      Test.java:2: Note: Test
        void f(Test this) {}
             ^

      The receiver type is incorrectly reported as 'none' if 'Test' is loaded from the classpath:

      $ javac -processor P -implicit:none Test
      Note: none

        There are no Sub-Tasks for this issue.

            cushon Liam Miller-Cushon
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: