1 19 package org.netbeans.modules.java.editor.overridden; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.MethodTree; 23 import com.sun.source.tree.Tree; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import javax.lang.model.element.Element; 29 import javax.lang.model.element.ElementKind; 30 import javax.lang.model.element.ExecutableElement; 31 import javax.lang.model.element.Modifier; 32 import javax.lang.model.element.TypeElement; 33 import javax.swing.text.Document ; 34 import org.netbeans.api.java.source.CompilationInfo; 35 import org.netbeans.api.java.source.ElementHandle; 36 import org.netbeans.api.java.source.support.CancellableTreePathScanner; 37 import org.netbeans.api.java.source.ClassIndex; 38 39 43 class IsOverriddenVisitor extends CancellableTreePathScanner<Void , Tree> { 44 45 private CompilationInfo info; 46 private ClassIndex uq; 47 private Document doc; 48 49 Map <ElementHandle<TypeElement>, List <ElementHandle<ExecutableElement>>> type2Declaration; 50 Map <ElementHandle<ExecutableElement>, MethodTree> declaration2Tree; 51 Map <ElementHandle<TypeElement>, ClassTree> declaration2Class; 52 53 private Map <TypeElement, ElementHandle<TypeElement>> type2Handle; 54 55 IsOverriddenVisitor(Document doc, CompilationInfo info) { 56 this.doc = doc; 57 this.info = info; 58 this.uq = info.getJavaSource().getClasspathInfo().getClassIndex(); 59 60 type2Declaration = new HashMap <ElementHandle<TypeElement>, List <ElementHandle<ExecutableElement>>>(); 61 declaration2Tree = new HashMap <ElementHandle<ExecutableElement>, MethodTree>(); 62 declaration2Class = new HashMap <ElementHandle<TypeElement>, ClassTree>(); 63 64 type2Handle = new HashMap <TypeElement, ElementHandle<TypeElement>>(); 65 } 66 67 private ElementHandle<TypeElement> getHandle(TypeElement type) { 68 ElementHandle<TypeElement> result = type2Handle.get(type); 69 70 if (result == null) { 71 type2Handle.put(type, result = ElementHandle.create(type)); 72 } 73 74 return result; 75 } 76 77 @Override 78 public Void visitMethod(MethodTree tree, Tree d) { 79 if (currentClass != null) { 80 Element el = info.getTrees().getElement(getCurrentPath()); 81 82 if (el != null && el.getKind() == ElementKind.METHOD) { 83 if (!el.getModifiers().contains(Modifier.PRIVATE) && !el.getModifiers().contains(Modifier.STATIC)) { 84 ExecutableElement overridee = (ExecutableElement) el; 85 List <ElementHandle<ExecutableElement>> methods = type2Declaration.get(currentClass); 86 87 if (methods == null) { 88 type2Declaration.put(currentClass, methods = new ArrayList <ElementHandle<ExecutableElement>>()); 89 } 90 91 ElementHandle<ExecutableElement> methodHandle = ElementHandle.create(overridee); 92 93 methods.add(methodHandle); 94 declaration2Tree.put(methodHandle, tree); 95 } 96 } 97 } 98 99 super.visitMethod(tree, tree); 100 return null; 101 } 102 103 @Override 104 public Void visitClass(ClassTree tree, Tree d) { 105 Element decl = info.getTrees().getElement(getCurrentPath()); 106 107 if (decl != null && (decl.getKind().isClass() || decl.getKind().isInterface())) { 108 ElementHandle<TypeElement> oldCurrentClass = currentClass; 109 110 currentClass = getHandle((TypeElement) decl); 111 declaration2Class.put(currentClass, tree); 112 super.visitClass(tree, d); 113 currentClass = oldCurrentClass; 114 } else { 115 super.visitClass(tree, d); 116 } 117 118 return null; 119 } 120 121 private ElementHandle<TypeElement> currentClass; 122 123 } | Popular Tags |