1 11 package org.eclipse.jdt.internal.core.util; 12 13 import org.eclipse.jdt.core.IInitializer; 14 import org.eclipse.jdt.core.IMember; 15 import org.eclipse.jdt.core.ISourceRange; 16 import org.eclipse.jdt.core.JavaModelException; 17 import org.eclipse.jdt.core.dom.ASTNode; 18 import org.eclipse.jdt.core.dom.ASTVisitor; 19 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; 20 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; 21 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; 22 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 23 import org.eclipse.jdt.core.dom.CompilationUnit; 24 import org.eclipse.jdt.core.dom.EnumConstantDeclaration; 25 import org.eclipse.jdt.core.dom.EnumDeclaration; 26 import org.eclipse.jdt.core.dom.IBinding; 27 import org.eclipse.jdt.core.dom.ImportDeclaration; 28 import org.eclipse.jdt.core.dom.Initializer; 29 import org.eclipse.jdt.core.dom.MethodDeclaration; 30 import org.eclipse.jdt.core.dom.PackageDeclaration; 31 import org.eclipse.jdt.core.dom.TypeDeclaration; 32 import org.eclipse.jdt.core.dom.TypeParameter; 33 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 34 import org.eclipse.jdt.internal.core.SourceRefElement; 35 36 public class DOMFinder extends ASTVisitor { 37 38 public ASTNode foundNode = null; 39 public IBinding foundBinding = null; 40 41 private CompilationUnit ast; 42 private SourceRefElement element; 43 private boolean resolveBinding; 44 private int rangeStart = -1, rangeLength = 0; 45 46 public DOMFinder(CompilationUnit ast, SourceRefElement element, boolean resolveBinding) { 47 this.ast = ast; 48 this.element = element; 49 this.resolveBinding = resolveBinding; 50 } 51 52 protected boolean found(ASTNode node, ASTNode name) { 53 if (name.getStartPosition() == this.rangeStart && name.getLength() == this.rangeLength) { 54 this.foundNode = node; 55 return true; 56 } 57 return false; 58 } 59 60 public ASTNode search() throws JavaModelException { 61 ISourceRange range = null; 62 if (this.element instanceof IMember && !(this.element instanceof IInitializer)) 63 range = ((IMember) this.element).getNameRange(); 64 else 65 range = this.element.getSourceRange(); 66 this.rangeStart = range.getOffset(); 67 this.rangeLength = range.getLength(); 68 this.ast.accept(this); 69 return this.foundNode; 70 } 71 72 public boolean visit(AnnotationTypeDeclaration node) { 73 if (found(node, node.getName()) && this.resolveBinding) 74 this.foundBinding = node.resolveBinding(); 75 return true; 76 } 77 78 public boolean visit(AnnotationTypeMemberDeclaration node) { 79 if (found(node, node.getName()) && this.resolveBinding) 80 this.foundBinding = node.resolveBinding(); 81 return true; 82 } 83 84 public boolean visit(AnonymousClassDeclaration node) { 85 ASTNode name; 86 ASTNode parent = node.getParent(); 87 switch (parent.getNodeType()) { 88 case ASTNode.CLASS_INSTANCE_CREATION: 89 name = ((ClassInstanceCreation) parent).getType(); 90 break; 91 case ASTNode.ENUM_CONSTANT_DECLARATION: 92 name = ((EnumConstantDeclaration) parent).getName(); 93 break; 94 default: 95 return true; 96 } 97 if (found(node, name) && this.resolveBinding) 98 this.foundBinding = node.resolveBinding(); 99 return true; 100 } 101 102 public boolean visit(EnumConstantDeclaration node) { 103 if (found(node, node.getName()) && this.resolveBinding) 104 this.foundBinding = node.resolveVariable(); 105 return true; 106 } 107 108 public boolean visit(EnumDeclaration node) { 109 if (found(node, node.getName()) && this.resolveBinding) 110 this.foundBinding = node.resolveBinding(); 111 return true; 112 } 113 114 public boolean visit(ImportDeclaration node) { 115 if (found(node, node) && this.resolveBinding) 116 this.foundBinding = node.resolveBinding(); 117 return true; 118 } 119 120 public boolean visit(Initializer node) { 121 found(node, node); 123 return true; 124 } 125 126 public boolean visit(MethodDeclaration node) { 127 if (found(node, node.getName()) && this.resolveBinding) 128 this.foundBinding = node.resolveBinding(); 129 return true; 130 } 131 132 public boolean visit(PackageDeclaration node) { 133 if (found(node, node) && this.resolveBinding) 134 this.foundBinding = node.resolveBinding(); 135 return true; 136 } 137 138 public boolean visit(TypeDeclaration node) { 139 if (found(node, node.getName()) && this.resolveBinding) 140 this.foundBinding = node.resolveBinding(); 141 return true; 142 } 143 144 public boolean visit(TypeParameter node) { 145 if (found(node, node.getName()) && this.resolveBinding) 146 this.foundBinding = node.resolveBinding(); 147 return true; 148 } 149 150 public boolean visit(VariableDeclarationFragment node) { 151 if (found(node, node.getName()) && this.resolveBinding) 152 this.foundBinding = node.resolveBinding(); 153 return true; 154 } 155 } 156 | Popular Tags |