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

can't get the Element object for the local and anonymous class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 6
    • tools
    • x86
    • windows_xp

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      I can't get the javax.lang.model.element.Element object for the local and anonymous class by using com.sun.source.util.Trees.instance(javax.annotation.processing.processingEnv).getElement(getCurrentPath()) in com.sun.source.util.TreePathScanner#visitClass().
      -----
      import java.util.Set;
      import javax.annotation.processing.*;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.*;
      import com.sun.source.tree.*;
      import com.sun.source.util.*;

      @SupportedAnnotationTypes("*")
      @SupportedSourceVersion(SourceVersion.RELEASE_6)
      public class ListType extends AbstractProcessor {
          @Override
          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
              class LocalClass extends Thread {
                  @Override
                  public void run() {
                  }
              }
              new Thread() {
                  @Override
                  public void run() {
                  }
              };
              for (Element e: roundEnv.getRootElements()) {
                  new ListTypeVisitor<Void, Void>().scan(Trees.instance(processingEnv).getPath(e), null);
              }
              return true;
          }

          private class ListTypeVisitor<R, P> extends TreePathScanner<R, P> {
              @Override
              public R visitClass(ClassTree t, P p) {
                  Element e = Trees.instance(processingEnv).getElement(getCurrentPath());
                  System.out.println(t.getSimpleName() + ":" + e);
                  return super.visitClass(t, p);
              }

              @Override
              public R visitMethod(MethodTree t, P p) {
                  Element e = Trees.instance(processingEnv).getElement(getCurrentPath());
                  System.out.println("\t" + t.getName() + ":" + e);
                  return super.visitMethod(t, p);
              }
          }
      }
      -----
      I can't write an annotation processor like this.
      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6329722
      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6500703
      Pluggable Annotation Processing API doesn't support local and anonymous classes, too.
      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6587158

      P.S.
      I can't get the TypeElement object of a local and anonymous class by using javax.annotation.processing.processingEnv.getElementUtils().getTypeElement("Processor.1LocalClass") and javax.annotation.processing.processingEnv.getElementUtils().getTypeElement("Processor.1"), too.
      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6416679


      REPRODUCIBILITY :
      This bug can be reproduced always.

            jjg Jonathan Gibbons
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: