1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.IRegion; 21 22 import org.eclipse.search.ui.text.Match; 23 24 import org.eclipse.jdt.core.IJavaElement; 25 import org.eclipse.jdt.core.dom.ASTNode; 26 import org.eclipse.jdt.core.dom.ASTVisitor; 27 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 28 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; 29 import org.eclipse.jdt.core.dom.CompilationUnit; 30 import org.eclipse.jdt.core.dom.IMethodBinding; 31 import org.eclipse.jdt.core.dom.ITypeBinding; 32 import org.eclipse.jdt.core.dom.MethodDeclaration; 33 import org.eclipse.jdt.core.dom.Name; 34 import org.eclipse.jdt.core.dom.Type; 35 import org.eclipse.jdt.core.dom.TypeDeclarationStatement; 36 37 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 38 import org.eclipse.jdt.internal.corext.dom.Bindings; 39 import org.eclipse.jdt.internal.corext.dom.NodeFinder; 40 41 42 47 public class ImplementOccurrencesFinder implements org.eclipse.jdt.internal.ui.search.IOccurrencesFinder { 48 49 50 private class MethodVisitor extends ASTVisitor { 51 52 55 public boolean visit(MethodDeclaration node) { 56 IMethodBinding binding= node.resolveBinding(); 57 if (binding != null) { 58 IMethodBinding method= Bindings.findOverriddenMethodInHierarchy(fSelectedType, binding); 59 if (method != null) 60 fResult.add(node.getName()); 61 } 62 return super.visit(node); 63 } 64 65 68 public boolean visit(AnonymousClassDeclaration node) { 69 return false; 71 } 72 73 76 public boolean visit(TypeDeclarationStatement node) { 77 return false; 79 } 80 } 81 82 83 private ASTNode fStart; 84 private List fResult; 85 private ASTNode fSelectedNode; 86 private ITypeBinding fSelectedType; 87 88 public ImplementOccurrencesFinder() { 89 fResult= new ArrayList (); 90 } 91 92 public String initialize(CompilationUnit root, int offset, int length) { 93 return initialize(root, NodeFinder.perform(root, offset, length)); 94 } 95 96 public String initialize(CompilationUnit root, ASTNode node) { 97 if (!(node instanceof Name)) 98 return SearchMessages.ImplementOccurrencesFinder_invalidTarget; 99 100 fSelectedNode= ASTNodes.getNormalizedNode(node); 101 if (!(fSelectedNode instanceof Type)) 102 return SearchMessages.ImplementOccurrencesFinder_invalidTarget; 103 104 ASTNode typeDeclaration= fSelectedNode.getParent(); 105 if (!(typeDeclaration instanceof AbstractTypeDeclaration)) 106 return SearchMessages.ImplementOccurrencesFinder_invalidTarget; 107 108 fSelectedType= ((Type)fSelectedNode).resolveBinding(); 109 if (fSelectedType == null) 110 return SearchMessages.ImplementOccurrencesFinder_invalidTarget; 111 112 fStart= typeDeclaration; 113 return null; 114 } 115 116 119 public List perform() { 120 fStart.accept(new MethodVisitor()); 121 if (fSelectedNode != null) 122 fResult.add(fSelectedNode); 123 124 return fResult; 125 } 126 127 public void collectOccurrenceMatches(IJavaElement element, IDocument document, Collection resultingMatches) { 128 for (Iterator iter= fResult.iterator(); iter.hasNext();) { 129 ASTNode node= (ASTNode) iter.next(); 130 int startPosition= node.getStartPosition(); 131 int length= node.getLength(); 132 try { 133 int line= document.getLineOfOffset(startPosition); 134 IRegion region= document.getLineInformation(line); 135 String lineContents= document.get(region.getOffset(), region.getLength()).trim(); 136 JavaElementLine groupKey= new JavaElementLine(element, line, lineContents); 137 resultingMatches.add(new Match(groupKey, startPosition, length)); 138 } catch (BadLocationException e) { 139 } 141 } 142 } 143 144 147 public String getJobLabel() { 148 return SearchMessages.ImplementOccurrencesFinder_searchfor ; 149 } 150 151 public String getElementName() { 152 if (fSelectedNode != null) { 153 return ASTNodes.asString(fSelectedNode); 154 } 155 return null; 156 } 157 158 public String getUnformattedPluralLabel() { 159 return SearchMessages.ImplementOccurrencesFinder_label_plural; 160 } 161 162 public String getUnformattedSingularLabel() { 163 return SearchMessages.ImplementOccurrencesFinder_label_singular; 164 } 165 166 public void releaseAST() { 167 fStart= null; 168 fSelectedType= null; 169 } 170 171 } 172 | Popular Tags |