1 12 package org.eclipse.jdt.internal.corext.callhierarchy; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.SubProgressMonitor; 19 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IMember; 22 import org.eclipse.jdt.core.IMethod; 23 import org.eclipse.jdt.core.IType; 24 import org.eclipse.jdt.core.JavaModelException; 25 26 import org.eclipse.jdt.internal.ui.JavaPlugin; 27 28 31 public class Implementors { 32 private static IImplementorFinder[] IMPLEMENTOR_FINDERS= new IImplementorFinder[] { new JavaImplementorFinder() }; 33 private static Implementors fgInstance; 34 35 38 public static Implementors getInstance() { 39 if (fgInstance == null) { 40 fgInstance = new Implementors(); 41 } 42 43 return fgInstance; 44 } 45 46 56 public IJavaElement[] searchForImplementors(IJavaElement[] elements, 57 IProgressMonitor progressMonitor) { 58 if ((elements != null) && (elements.length > 0)) { 59 IJavaElement element = elements[0]; 60 61 try { 62 if (element instanceof IMember) { 63 IMember member = (IMember) element; 64 IType type = member.getDeclaringType(); 65 66 if (type.isInterface()) { 67 IType[] implementingTypes = findImplementingTypes(type, 68 progressMonitor); 69 70 if (member.getElementType() == IJavaElement.METHOD) { 71 return findMethods((IMethod)member, implementingTypes, progressMonitor); 72 } else { 73 return implementingTypes; 74 } 75 } 76 } 77 } catch (JavaModelException e) { 78 JavaPlugin.log(e); 79 } 80 } 81 82 return null; 83 } 84 85 96 public IJavaElement[] searchForInterfaces(IJavaElement[] elements, 97 IProgressMonitor progressMonitor) { 98 if ((elements != null) && (elements.length > 0)) { 99 IJavaElement element = elements[0]; 100 101 if (element instanceof IMember) { 102 IMember member = (IMember) element; 103 IType type = member.getDeclaringType(); 104 105 IType[] implementingTypes = findInterfaces(type, progressMonitor); 106 107 if (!progressMonitor.isCanceled()) { 108 if (member.getElementType() == IJavaElement.METHOD) { 109 return findMethods((IMethod)member, implementingTypes, progressMonitor); 110 } else { 111 return implementingTypes; 112 } 113 } 114 } 115 } 116 117 return null; 118 } 119 120 private IImplementorFinder[] getImplementorFinders() { 121 return IMPLEMENTOR_FINDERS; 122 } 123 124 private IType[] findImplementingTypes(IType type, IProgressMonitor progressMonitor) { 125 Collection implementingTypes = new ArrayList (); 126 127 IImplementorFinder[] finders = getImplementorFinders(); 128 129 for (int i = 0; (i < finders.length) && !progressMonitor.isCanceled(); i++) { 130 Collection types = finders[i].findImplementingTypes(type, 131 new SubProgressMonitor(progressMonitor, 10, 132 SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 133 134 if (types != null) { 135 implementingTypes.addAll(types); 136 } 137 } 138 139 return (IType[]) implementingTypes.toArray(new IType[implementingTypes.size()]); 140 } 141 142 private IType[] findInterfaces(IType type, IProgressMonitor progressMonitor) { 143 Collection interfaces = new ArrayList (); 144 145 IImplementorFinder[] finders = getImplementorFinders(); 146 147 for (int i = 0; (i < finders.length) && !progressMonitor.isCanceled(); i++) { 148 Collection types = finders[i].findInterfaces(type, 149 new SubProgressMonitor(progressMonitor, 10, 150 SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 151 152 if (types != null) { 153 interfaces.addAll(types); 154 } 155 } 156 157 return (IType[]) interfaces.toArray(new IType[interfaces.size()]); 158 } 159 160 169 private IJavaElement[] findMethods(IMethod method, IType[] types, 170 IProgressMonitor progressMonitor) { 171 Collection foundMethods = new ArrayList (); 172 173 SubProgressMonitor subProgressMonitor = new SubProgressMonitor(progressMonitor, 174 10, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL); 175 subProgressMonitor.beginTask("", types.length); 177 try { 178 for (int i = 0; i < types.length; i++) { 179 IType type = types[i]; 180 IMethod[] methods = type.findMethods(method); 181 182 if (methods != null) { 183 for (int j = 0; j < methods.length; j++) { 184 foundMethods.add(methods[j]); 185 } 186 } 187 188 subProgressMonitor.worked(1); 189 } 190 } finally { 191 subProgressMonitor.done(); 192 } 193 194 return (IJavaElement[]) foundMethods.toArray(new IJavaElement[foundMethods.size()]); 195 } 196 } 197 | Popular Tags |