1 11 package org.eclipse.jdt.internal.corext.refactoring.util; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Comparator ; 16 import java.util.HashSet ; 17 import java.util.List ; 18 import java.util.Set ; 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 private JavaElementUtil(){ 47 } 48 49 public static String 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(); } 55 } 56 57 public static String createFieldSignature(IField field){ 58 return JavaModelUtil.getFullyQualifiedName(field.getDeclaringType()) + "." + field.getElementName(); } 60 61 public static String 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 result= new HashSet (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 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 result= new ArrayList (); 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 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 result= new ArrayList (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 List result= new ArrayList (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 element) { 178 return (element instanceof IPackageFragment) && ((IPackageFragment)element).isDefaultPackage(); 179 } 180 181 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 subpackages= new ArrayList (); 193 subpackages.add(pack); 194 String 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 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 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 comparator= new Comparator (){ 226 public int compare(Object o1, Object 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 |