KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > JavaElementUtil


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.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21
22 import org.eclipse.core.resources.ResourcesPlugin;
23
24 import org.eclipse.jdt.core.IClasspathEntry;
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IField;
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.IMember;
30 import org.eclipse.jdt.core.IMethod;
31 import org.eclipse.jdt.core.IPackageFragment;
32 import org.eclipse.jdt.core.IPackageFragmentRoot;
33 import org.eclipse.jdt.core.ISourceReference;
34 import org.eclipse.jdt.core.IType;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.JavaModelException;
37 import org.eclipse.jdt.core.Signature;
38
39 import org.eclipse.jdt.internal.corext.SourceRange;
40 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
41 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
42
43 public class JavaElementUtil {
44     
45     //no instances
46
private JavaElementUtil(){
47     }
48     
49     public static String JavaDoc createMethodSignature(IMethod method){
50         try {
51             return Signature.toString(method.getSignature(), method.getElementName(), method.getParameterNames(), false, ! method.isConstructor());
52         } catch(JavaModelException e) {
53             return method.getElementName(); //fallback
54
}
55     }
56     
57     public static String JavaDoc createFieldSignature(IField field){
58         return JavaModelUtil.getFullyQualifiedName(field.getDeclaringType()) + "." + field.getElementName(); //$NON-NLS-1$
59
}
60     
61     public static String JavaDoc createSignature(IMember member){
62         switch (member.getElementType()){
63             case IJavaElement.FIELD:
64                 return createFieldSignature((IField)member);
65             case IJavaElement.TYPE:
66                 return JavaModelUtil.getFullyQualifiedName(((IType)member));
67             case IJavaElement.INITIALIZER:
68                 return RefactoringCoreMessages.JavaElementUtil_initializer;
69             case IJavaElement.METHOD:
70                 return createMethodSignature((IMethod)member);
71             default:
72                 Assert.isTrue(false);
73                 return null;
74         }
75     }
76     
77     public static IJavaElement[] getElementsOfType(IJavaElement[] elements, int type){
78         Set JavaDoc result= new HashSet JavaDoc(elements.length);
79         for (int i= 0; i < elements.length; i++) {
80             IJavaElement element= elements[i];
81             if (element.getElementType() == type)
82                 result.add(element);
83         }
84         return (IJavaElement[]) result.toArray(new IJavaElement[result.size()]);
85     }
86
87     public static IType getMainType(ICompilationUnit cu) throws JavaModelException{
88         IType[] types= cu.getTypes();
89         for (int i = 0; i < types.length; i++) {
90             if (isMainType(types[i]))
91                 return types[i];
92         }
93         return null;
94     }
95     
96     public static boolean isMainType(IType type) throws JavaModelException{
97         if (! type.exists())
98             return false;
99
100         if (type.isBinary())
101             return false;
102             
103         if (type.getCompilationUnit() == null)
104             return false;
105         
106         if (type.getDeclaringType() != null)
107             return false;
108         
109         return isPrimaryType(type) || isCuOnlyType(type);
110     }
111
112
113     private static boolean isPrimaryType(IType type){
114         return type.equals(type.getCompilationUnit().findPrimaryType());
115     }
116
117
118     private static boolean isCuOnlyType(IType type) throws JavaModelException{
119         return type.getCompilationUnit().getTypes().length == 1;
120     }
121
122     /** see org.eclipse.jdt.internal.core.JavaElement#isAncestorOf(org.eclipse.jdt.core.IJavaElement) */
123     public static boolean isAncestorOf(IJavaElement ancestor, IJavaElement child) {
124         IJavaElement parent= child.getParent();
125         while (parent != null && !parent.equals(ancestor)) {
126             parent= parent.getParent();
127         }
128         return parent != null;
129     }
130     
131     public static IMethod[] getAllConstructors(IType type) throws JavaModelException {
132         if (JavaModelUtil.isInterfaceOrAnnotation(type))
133             return new IMethod[0];
134         List JavaDoc result= new ArrayList JavaDoc();
135         IMethod[] methods= type.getMethods();
136         for (int i= 0; i < methods.length; i++) {
137             IMethod iMethod= methods[i];
138             if (iMethod.isConstructor())
139                 result.add(iMethod);
140         }
141         return (IMethod[]) result.toArray(new IMethod[result.size()]);
142     }
143
144     /**
145      * Returns an array of projects that have the specified root on their
146      * classpaths.
147      */

148     public static IJavaProject[] getReferencingProjects(IPackageFragmentRoot root) throws JavaModelException {
149         IClasspathEntry cpe= root.getRawClasspathEntry();
150         IJavaProject[] allJavaProjects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
151         List JavaDoc result= new ArrayList JavaDoc(allJavaProjects.length);
152         for (int i= 0; i < allJavaProjects.length; i++) {
153             IJavaProject project= allJavaProjects[i];
154             IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(cpe);
155             if (roots.length > 0)
156                 result.add(project);
157         }
158         return (IJavaProject[]) result.toArray(new IJavaProject[result.size()]);
159     }
160     
161     public static IMember[] merge(IMember[] a1, IMember[] a2) {
162         // Don't use hash sets since ordering is important for some refactorings.
163
List JavaDoc result= new ArrayList JavaDoc(a1.length + a2.length);
164         for (int i= 0; i < a1.length; i++) {
165             IMember member= a1[i];
166             if (!result.contains(member))
167                 result.add(member);
168         }
169         for (int i= 0; i < a2.length; i++) {
170             IMember member= a2[i];
171             if (!result.contains(member))
172                 result.add(member);
173         }
174         return (IMember[]) result.toArray(new IMember[result.size()]);
175     }
176
177     public static boolean isDefaultPackage(Object JavaDoc element) {
178         return (element instanceof IPackageFragment) && ((IPackageFragment)element).isDefaultPackage();
179     }
180     
181     /**
182      * @param pack a package fragment
183      * @return an array containing the given package and all subpackages
184      * @throws JavaModelException
185      */

186     public static IPackageFragment[] getPackageAndSubpackages(IPackageFragment pack) throws JavaModelException {
187         if (pack.isDefaultPackage())
188             return new IPackageFragment[] { pack };
189         
190         IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent();
191         IJavaElement[] allPackages= root.getChildren();
192         ArrayList JavaDoc subpackages= new ArrayList JavaDoc();
193         subpackages.add(pack);
194         String JavaDoc prefix= pack.getElementName() + '.';
195         for (int i= 0; i < allPackages.length; i++) {
196             IPackageFragment currentPackage= (IPackageFragment) allPackages[i];
197             if (currentPackage.getElementName().startsWith(prefix))
198                 subpackages.add(currentPackage);
199         }
200         return (IPackageFragment[]) subpackages.toArray(new IPackageFragment[subpackages.size()]);
201     }
202     
203     /**
204      * @param pack the package fragment; may not be null
205      * @return the parent package fragment, or null if the given package fragment is the default package or a top level package
206      */

207     public static IPackageFragment getParentSubpackage(IPackageFragment pack) {
208         if (pack.isDefaultPackage())
209             return null;
210         
211         final int index= pack.getElementName().lastIndexOf('.');
212         if (index == -1)
213             return null;
214
215         final IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent();
216         final String JavaDoc newPackageName= pack.getElementName().substring(0, index);
217         final IPackageFragment parent= root.getPackageFragment(newPackageName);
218         if (parent.exists())
219             return parent;
220         else
221             return null;
222     }
223     
224     public static IMember[] sortByOffset(IMember[] members){
225         Comparator JavaDoc comparator= new Comparator JavaDoc(){
226             public int compare(Object JavaDoc o1, Object JavaDoc o2){
227                 try{
228                     return ((IMember) o1).getNameRange().getOffset() - ((IMember) o2).getNameRange().getOffset();
229                 } catch (JavaModelException e){
230                     return 0;
231                 }
232             }
233         };
234         Arrays.sort(members, comparator);
235         return members;
236     }
237     
238     public static boolean isSourceAvailable(ISourceReference sourceReference) {
239         try {
240             return SourceRange.isAvailable(sourceReference.getSourceRange());
241         } catch (JavaModelException e) {
242             return false;
243         }
244     }
245 }
246
Popular Tags