Advertisement
Guest User

Var decl

a guest
Nov 13th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package com.unipr;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5.  
  6. import javax.tools.JavaCompiler;
  7. import javax.tools.JavaFileObject;
  8. import javax.tools.StandardJavaFileManager;
  9. import javax.tools.ToolProvider;
  10.  
  11. import com.sun.source.util.TreePath;
  12. import com.sun.source.util.Trees;
  13. import com.sun.tools.javac.api.JavacTaskImpl;
  14. import com.sun.tools.javac.code.Kinds;
  15. import com.sun.tools.javac.main.OptionName;
  16. import com.sun.tools.javac.model.JavacElements;
  17. import com.sun.tools.javac.tree.JCTree;
  18. import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  19. import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
  20. import com.sun.tools.javac.tree.JCTree.JCIdent;
  21. import com.sun.tools.javac.tree.TreeScanner;
  22. import com.sun.tools.javac.util.List;
  23.  
  24. /**
  25.  * singletest/A.java is:
  26.  *
  27.  * public class A {
  28.  *   private static final int j = 0;
  29.  *   private int f() {
  30.  *     int x = 1;
  31.  *     return B.i + j + x;
  32.  *   }
  33.  * }
  34.  *
  35.  * singletest/B.java is:
  36.  *
  37.  * public class B {
  38.  *   public static final int i = 0;
  39.  * }
  40.  *
  41.  *
  42.  * Output:
  43.  *
  44.  * decl for i:
  45.  * public static final int i = 0
  46.  *
  47.  * decl for j:
  48.  * private static final int j = 0
  49.  *
  50.  * decl for x:
  51.  * int x = 1
  52.  */
  53. public class TreeVisitor extends TreeScanner {
  54.   private Trees trees;
  55.   private TreePath path;
  56.   private JavacElements elements;
  57.  
  58.   public TreeVisitor(JavacTaskImpl task) {
  59.     trees = Trees.instance(task);
  60.     elements = JavacElements.instance(task.getContext());
  61.   }
  62.  
  63.   public void scan(JCTree tree) {
  64.     if (tree != null) {
  65.       TreePath prev = path;
  66.       path = new TreePath(path, tree);
  67.       try {
  68.         tree.accept(this);
  69.       } finally {
  70.         path = prev;
  71.       }    
  72.     }      
  73.   }
  74.  
  75.   @Override
  76.   public void visitIdent(JCIdent tree) {
  77.     if(tree.sym != null && tree.sym.kind == Kinds.VAR) {
  78.       JCTree declTree = elements.getTree(tree.sym);
  79.       System.out.println("decl for " + tree.sym + ":\n" + (declTree == null ? "<no_decl??>" : declTree) + "\n");
  80.     }
  81.     super.visitIdent(tree);
  82.   }
  83.  
  84.   @Override
  85.   public void visitSelect(JCFieldAccess tree) {
  86.     if (tree.sym != null && tree.sym.kind == Kinds.VAR) {
  87.       JCTree declTree = (JCTree) trees.getTree(tree.sym);
  88.       System.out.println("decl for " + tree.sym + ":\n" + declTree + "\n");
  89.     }
  90.     super.visitSelect(tree);
  91.   }
  92.  
  93.   @SuppressWarnings("unchecked")
  94.   public static void main(String[] args) throws IOException {
  95.     JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
  96.     StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
  97.     Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects("./singletest/A.java");
  98.     java.util.List<String> opts = new ArrayList<String>();
  99.     opts.add(OptionName.SOURCEPATH.optionName);
  100.     opts.add("./singletest");
  101.     JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, opts, null, files); // cast to JavacTaskImpl so I can get the context later
  102.     List<JCCompilationUnit> units = (List<JCCompilationUnit>) task.parse();
  103.     task.analyze();
  104.     new TreeVisitor(task).scan(units.head);
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement