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

javac, com.sun.source.util.Trees fail to retrieve class trees at GENERATE event

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8
    • tools
    • None

      The following "jtreg" test:

      /*
       * @test
       * @bug 8011026
       * @summary javac, com.sun.source.util.Trees fail to retrieve class trees at GENERATE
       * event
       */

      import com.sun.source.tree.ClassTree;
      import java.io.File;
      import java.io.IOException;
      import java.util.Arrays;

      import javax.tools.JavaCompiler;
      import javax.tools.JavaFileObject;
      import javax.tools.StandardJavaFileManager;
      import javax.tools.ToolProvider;

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

      public class TreesIntfGivesNoInnerClassTreeAtGenerateEventTest {

          public static void main(String[] args) throws IOException {
              new TreesIntfGivesNoInnerClassTreeAtGenerateEventTest().run(args);
          }

          void run(String[] args) throws IOException {
              checkBridgeSymbol(TaskEvent.Kind.ANALYZE);
              checkBridgeSymbol(TaskEvent.Kind.GENERATE);
          }

          void checkBridgeSymbol(final TaskEvent.Kind kind) throws IOException {
              System.out.println("Waiting for events of kind " + kind.name());
              JavaCompiler c = ToolProvider.getSystemJavaCompiler();
              StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
              Iterable<? extends JavaFileObject> fos =
                      fm.getJavaFileObjectsFromFiles(
                      Arrays.asList(new File(System.getProperty("test.src"),
                      this.getClass().getSimpleName() + ".java")));
              JavacTask task = (JavacTask) c.getTask(null, fm, null,
                      Arrays.asList("-d", System.getProperty("user.dir")), null, fos);
              final Trees trees = Trees.instance(task);

              task.addTaskListener(new TaskListener() {

                  @Override
                  public void started(TaskEvent e) {}

                  @Override
                  public void finished(TaskEvent e) {
                      if (e.getKind() == kind) {
                          ClassTree classTree = trees.getTree(e.getTypeElement());
                          if (classTree == null) {
                              System.out.println("No tree for type element " +
                                      e.getTypeElement().toString());
                          } else {
                              System.out.println("We have tree for type element " +
                                      e.getTypeElement().toString());
                          }
                      }
                  }
              });
              
              task.call();
          }
          
          class Inner {}

      }

      Prints out:

      Waiting for events of kind ANALYZE
      We have tree for type element TreesIntfGivesNoInnerClassTreeAtGenerateEventTest
      Waiting for events of kind GENERATE
      No tree for type element <anonymous TreesIntfGivesNoInnerClassTreeAtGenerateEventTest$1>
      No tree for type element TreesIntfGivesNoInnerClassTreeAtGenerateEventTest.Inner
      No tree for type element TreesIntfGivesNoInnerClassTreeAtGenerateEventTest

            vromero Vicente Arturo Romero Zaldivar
            vromero Vicente Arturo Romero Zaldivar
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: