1 19 20 package org.netbeans.test.java.gui.parser; 21 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.Tree; 24 import com.sun.source.util.SourcePositions; 25 import com.sun.source.util.Trees; 26 import java.io.PrintStream ; 27 import javax.lang.model.element.Element; 28 import javax.lang.model.element.ExecutableElement; 29 import javax.lang.model.element.PackageElement; 30 import javax.lang.model.element.TypeElement; 31 import javax.lang.model.element.TypeParameterElement; 32 import javax.lang.model.element.VariableElement; 33 import javax.lang.model.util.ElementScanner6; 34 import org.netbeans.api.java.source.CompilationController; 35 36 40 public class ElementVisitor extends ElementScanner6<Void ,PrintStream > { 41 42 private CompilationController cc; 43 44 private boolean canceled = false; 45 46 private String text; 47 void cancel() { 48 canceled = true; 49 } 50 51 public ElementVisitor(CompilationController cc, String text) { 52 this.text = text; 53 this.cc = cc; 54 } 55 56 @Override 57 public Void visitPackage(PackageElement arg0, PrintStream arg1) { 58 if(!canceled) { 59 arg1.print("Package Element: "); 60 arg1.print(arg0.getSimpleName()); 61 long[] pos = getPosition(arg0); 62 arg1.println(" "+pos[0]+" "+pos[1]); 63 if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1])); 64 return super.visitPackage(arg0, arg1); 65 } 66 return null; 67 } 68 69 @Override 70 public Void visitType(TypeElement arg0, PrintStream arg1) { 71 if(!canceled) { 72 arg1.print("Type Element: "); 73 arg1.print(arg0.getSimpleName()); 74 long[] pos = getPosition(arg0); 75 arg1.println(" "+pos[0]+" "+pos[1]); 76 if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1])); 77 return super.visitType(arg0, arg1); 78 } 79 return null; 80 81 } 82 83 @Override 84 public Void visitVariable(VariableElement arg0, PrintStream arg1) { 85 if(!canceled) { 86 arg1.print("Variable Element: "); 87 arg1.print(arg0.getSimpleName()); 88 long[] pos = getPosition(arg0); 89 arg1.println(" "+pos[0]+" "+pos[1]); 90 if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1])); 91 return super.visitVariable(arg0, arg1); 92 } 93 return null; 94 } 95 96 @Override 97 public Void visitExecutable(ExecutableElement arg0, PrintStream arg1) { 98 if(!canceled) { 99 arg1.print("Executable Element: "); 100 arg1.print(arg0.getSimpleName()); 101 long[] pos = getPosition(arg0); 102 arg1.println(" "+pos[0]+" "+pos[1]); 103 if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1])); 104 return super.visitExecutable(arg0, arg1); 105 } 106 return null; 107 } 108 109 @Override 110 public Void visitTypeParameter(TypeParameterElement arg0, PrintStream arg1) { 111 if(!canceled) { 112 arg1.print("TypeParameter Element: "); 113 arg1.print(arg0.getSimpleName()); 114 long[] pos = getPosition(arg0); 115 arg1.println(" "+pos[0]+" "+pos[1]); 116 if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1])); 117 return super.visitTypeParameter(arg0, arg1); 118 } 119 return null; 120 } 121 122 123 124 private long[] getPosition( Element e ) { 125 Trees trees = cc.getTrees(); 126 CompilationUnitTree cut = cc.getCompilationUnit(); 127 Tree t = trees.getTree(e); 128 if ( t == null ) { 129 return new long[]{-1,-1}; 130 } 131 SourcePositions sourcePositions = trees.getSourcePositions(); 132 return new long[] {sourcePositions.getStartPosition(cut, t),sourcePositions.getEndPosition(cut, t)}; 133 } 134 135 } 136 | Popular Tags |