1 11 package org.eclipse.jdt.internal.corext.refactoring.structure; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 22 import org.eclipse.jdt.core.IField; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IMethod; 25 import org.eclipse.jdt.core.IType; 26 import org.eclipse.jdt.core.JavaModelException; 27 import org.eclipse.jdt.core.WorkingCopyOwner; 28 import org.eclipse.jdt.core.dom.ITypeBinding; 29 import org.eclipse.jdt.core.dom.MethodDeclaration; 30 import org.eclipse.jdt.core.dom.Name; 31 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 32 import org.eclipse.jdt.core.dom.Type; 33 import org.eclipse.jdt.core.search.SearchEngine; 34 import org.eclipse.jdt.core.search.SearchMatch; 35 36 import org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor; 37 import org.eclipse.jdt.internal.corext.util.SearchUtils; 38 39 public class ReferenceFinderUtil { 40 41 private ReferenceFinderUtil(){ 42 } 44 45 47 public static IType[] getTypesReferencedIn(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException { 48 SearchMatch[] results= getTypeReferencesIn(elements, null, pm); 49 Set referencedTypes= extractElements(results, IJavaElement.TYPE); 50 return (IType[]) referencedTypes.toArray(new IType[referencedTypes.size()]); 51 } 52 53 public static IType[] getTypesReferencedIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 54 SearchMatch[] results= getTypeReferencesIn(elements, owner, pm); 55 Set referencedTypes= extractElements(results, IJavaElement.TYPE); 56 return (IType[]) referencedTypes.toArray(new IType[referencedTypes.size()]); 57 } 58 59 private static SearchMatch[] getTypeReferencesIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 60 List referencedTypes= new ArrayList (); 61 pm.beginTask("", elements.length); for (int i = 0; i < elements.length; i++) { 63 referencedTypes.addAll(getTypeReferencesIn(elements[i], owner, new SubProgressMonitor(pm, 1))); 64 } 65 pm.done(); 66 return (SearchMatch[]) referencedTypes.toArray(new SearchMatch[referencedTypes.size()]); 67 } 68 69 private static List getTypeReferencesIn(IJavaElement element, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 70 CollectingSearchRequestor requestor= new CollectingSearchRequestor(); 71 SearchEngine engine= owner != null ? new SearchEngine(owner) : new SearchEngine(); 72 engine.searchDeclarationsOfReferencedTypes(element, requestor, pm); 73 return requestor.getResults(); 74 } 75 76 78 public static IField[] getFieldsReferencedIn(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException { 79 SearchMatch[] results= getFieldReferencesIn(elements, null, pm); 80 Set referencedFields= extractElements(results, IJavaElement.FIELD); 81 return (IField[]) referencedFields.toArray(new IField[referencedFields.size()]); 82 } 83 84 public static IField[] getFieldsReferencedIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 85 SearchMatch[] results= getFieldReferencesIn(elements, owner, pm); 86 Set referencedFields= extractElements(results, IJavaElement.FIELD); 87 return (IField[]) referencedFields.toArray(new IField[referencedFields.size()]); 88 } 89 90 private static SearchMatch[] getFieldReferencesIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 91 List referencedFields= new ArrayList (); 92 pm.beginTask("", elements.length); for (int i = 0; i < elements.length; i++) { 94 referencedFields.addAll(getFieldReferencesIn(elements[i], owner, new SubProgressMonitor(pm, 1))); 95 } 96 pm.done(); 97 return (SearchMatch[]) referencedFields.toArray(new SearchMatch[referencedFields.size()]); 98 } 99 100 private static List getFieldReferencesIn(IJavaElement element, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 101 CollectingSearchRequestor requestor= new CollectingSearchRequestor(); 102 SearchEngine engine= owner != null ? new SearchEngine(owner) : new SearchEngine(); 103 engine.searchDeclarationsOfAccessedFields(element, requestor, pm); 104 return requestor.getResults(); 105 } 106 107 109 public static IMethod[] getMethodsReferencedIn(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException { 110 SearchMatch[] results= getMethodReferencesIn(elements, null, pm); 111 Set referencedMethods= extractElements(results, IJavaElement.METHOD); 112 return (IMethod[]) referencedMethods.toArray(new IMethod[referencedMethods.size()]); 113 } 114 115 public static IMethod[] getMethodsReferencedIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 116 SearchMatch[] results= getMethodReferencesIn(elements, owner, pm); 117 Set referencedMethods= extractElements(results, IJavaElement.METHOD); 118 return (IMethod[]) referencedMethods.toArray(new IMethod[referencedMethods.size()]); 119 } 120 121 private static SearchMatch[] getMethodReferencesIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 122 List referencedMethods= new ArrayList (); 123 pm.beginTask("", elements.length); for (int i = 0; i < elements.length; i++) { 125 referencedMethods.addAll(getMethodReferencesIn(elements[i], owner, new SubProgressMonitor(pm, 1))); 126 } 127 pm.done(); 128 return (SearchMatch[]) referencedMethods.toArray(new SearchMatch[referencedMethods.size()]); 129 } 130 131 private static List getMethodReferencesIn(IJavaElement element, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException { 132 CollectingSearchRequestor requestor= new CollectingSearchRequestor(); 133 SearchEngine engine= owner != null ? new SearchEngine(owner) : new SearchEngine(); 134 engine.searchDeclarationsOfSentMessages(element, requestor, pm); 135 return requestor.getResults(); 136 } 137 138 public static ITypeBinding[] getTypesReferencedInDeclarations(MethodDeclaration[] methods) { 139 Set typesUsed= new HashSet (); 140 for (int i= 0; i < methods.length; i++) { 141 typesUsed.addAll(getTypesUsedInDeclaration(methods[i])); 142 } 143 return (ITypeBinding[]) typesUsed.toArray(new ITypeBinding[typesUsed.size()]); 144 } 145 146 public static Set getTypesUsedInDeclaration(MethodDeclaration methodDeclaration) { 148 if (methodDeclaration == null) 149 return new HashSet (0); 150 Set result= new HashSet (); 151 ITypeBinding binding= null; 152 Type returnType= methodDeclaration.getReturnType2(); 153 if (returnType != null) { 154 binding = returnType.resolveBinding(); 155 if (binding != null) 156 result.add(binding); 157 } 158 159 for (Iterator iter= methodDeclaration.parameters().iterator(); iter.hasNext();) { 160 binding = ((SingleVariableDeclaration)iter.next()).getType().resolveBinding(); 161 if (binding != null) 162 result.add(binding); 163 } 164 165 for (Iterator iter= methodDeclaration.thrownExceptions().iterator(); iter.hasNext();) { 166 binding = ((Name)iter.next()).resolveTypeBinding(); 167 if (binding != null) 168 result.add(binding); 169 } 170 return result; 171 } 172 173 private static Set extractElements(SearchMatch[] searchResults, int elementType) { 175 Set elements= new HashSet (); 176 for (int i= 0; i < searchResults.length; i++) { 177 IJavaElement el= SearchUtils.getEnclosingJavaElement(searchResults[i]); 178 if (el.exists() && el.getElementType() == elementType) 179 elements.add(el); 180 } 181 return elements; 182 } 183 } 184 | Popular Tags |