KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > structure > ReferenceFinderUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.structure;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
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         //no instances
43
}
44
45     //----- referenced types -
46

47     public static IType[] getTypesReferencedIn(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException {
48         SearchMatch[] results= getTypeReferencesIn(elements, null, pm);
49         Set JavaDoc 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 JavaDoc 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 JavaDoc referencedTypes= new ArrayList JavaDoc();
61         pm.beginTask("", elements.length); //$NON-NLS-1$
62
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 JavaDoc 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     //----- referenced fields ----
77

78     public static IField[] getFieldsReferencedIn(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException {
79         SearchMatch[] results= getFieldReferencesIn(elements, null, pm);
80         Set JavaDoc 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 JavaDoc 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 JavaDoc referencedFields= new ArrayList JavaDoc();
92         pm.beginTask("", elements.length); //$NON-NLS-1$
93
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 JavaDoc 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     //----- referenced methods ----
108

109     public static IMethod[] getMethodsReferencedIn(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException {
110         SearchMatch[] results= getMethodReferencesIn(elements, null, pm);
111         Set JavaDoc 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 JavaDoc 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 JavaDoc referencedMethods= new ArrayList JavaDoc();
123         pm.beginTask("", elements.length); //$NON-NLS-1$
124
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 JavaDoc 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 JavaDoc typesUsed= new HashSet JavaDoc();
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     //set of ITypeBindings
147
public static Set JavaDoc getTypesUsedInDeclaration(MethodDeclaration methodDeclaration) {
148         if (methodDeclaration == null)
149             return new HashSet JavaDoc(0);
150         Set JavaDoc result= new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc 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 helpers
174
private static Set JavaDoc extractElements(SearchMatch[] searchResults, int elementType) {
175         Set JavaDoc elements= new HashSet JavaDoc();
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