static final IMethod[] hierarchyDeclaresMethodName( IProgressMonitor pm, ITypeHierarchy hierarchy, IMethod method, String newName) throws CoreException { try { Set<IMethod> result = new HashSet<>(); IType type = method.getDeclaringType(); IMethod foundMethod = Checks.findMethod(newName, method.getParameterTypes().length, false, type); if (foundMethod != null) result.add(foundMethod); IMethod[] foundInHierarchyClasses = classesDeclareMethodName( hierarchy, Arrays.asList(hierarchy.getAllClasses()), method, newName); if (foundInHierarchyClasses != null) result.addAll(Arrays.asList(foundInHierarchyClasses)); IType[] implementingClasses = hierarchy.getImplementingClasses(type); IMethod[] foundInImplementingClasses = classesDeclareMethodName(hierarchy, Arrays.asList(implementingClasses), method, newName); if (foundInImplementingClasses != null) result.addAll(Arrays.asList(foundInImplementingClasses)); return result.toArray(new IMethod[result.size()]); } finally { if (pm != null) { pm.done(); } } }
/* * @see TypeHierarchyContentProvider.getTypesInHierarchy */ @Override protected final void getTypesInHierarchy(IType type, List<IType> res) { ITypeHierarchy hierarchy = getHierarchy(); if (hierarchy != null) { IType[] types = hierarchy.getSubtypes(type); if (isObject(type)) { for (int i = 0; i < types.length; i++) { IType curr = types[i]; if (!isAnonymousFromInterface( curr)) { // no anonymous classes on 'Object' -> will be children of interface res.add(curr); } } } else { boolean isHierarchyOnType = (hierarchy.getType() != null); boolean isClass = !Flags.isInterface(hierarchy.getCachedFlags(type)); if (isClass || isHierarchyOnType) { for (int i = 0; i < types.length; i++) { res.add(types[i]); } } else { for (int i = 0; i < types.length; i++) { IType curr = types[i]; // no classes implementing interfaces, only if anonymous if (Flags.isInterface(hierarchy.getCachedFlags(curr)) || isAnonymous(curr)) { res.add(curr); } } } } } }
/** * Returns all activities found in the given project (including those in libraries, except for * android.jar itself) * * @param project the project * @return a list of activity classes as fully qualified class names */ @SuppressWarnings("restriction") // BinaryType @NonNull public static List<String> getProjectActivities(IProject project) { final List<String> activities = new ArrayList<String>(); try { final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project); if (javaProject != null) { IType[] activityTypes = new IType[0]; IType activityType = javaProject.findType(SdkConstants.CLASS_ACTIVITY); if (activityType != null) { ITypeHierarchy hierarchy = activityType.newTypeHierarchy(javaProject, new NullProgressMonitor()); activityTypes = hierarchy.getAllSubtypes(activityType); for (IType type : activityTypes) { if (type instanceof BinaryType && (type.getClassFile() == null || type.getClassFile().getResource() == null)) { continue; } activities.add(type.getFullyQualifiedName()); } } } } catch (CoreException e) { AdtPlugin.log(e, null); } return activities; }
/** {@inheritDoc} */ public Object[] getChildren(Object element) { // AspectJ Change begin if (element instanceof ICompilationUnit) { element = AJCompilationUnitManager.mapToAJCompilationUnit((ICompilationUnit) element); } // AspectJ Change end if (fShowOnlyMainType) { if (element instanceof ICompilationUnit) { element = getMainType((ICompilationUnit) element); } else if (element instanceof IClassFile) { element = getMainType((IClassFile) element); } if (element == null) return NO_CHILDREN; } if (fShowInheritedMembers && element instanceof IType) { IType type = (IType) element; if (type.getDeclaringType() == null) { ITypeHierarchy th = getSuperTypeHierarchy(type); if (th != null) { List children = new ArrayList(); IType[] superClasses = th.getAllSupertypes(type); children.addAll(Arrays.asList(super.getChildren(type))); for (int i = 0, scLength = superClasses.length; i < scLength; i++) children.addAll(Arrays.asList(super.getChildren(superClasses[i]))); return children.toArray(); } } } return super.getChildren(element); }
public List<IType> getSupertypes() throws JavaModelException { ITypeHierarchy typeHierarchy = SuperTypeHierarchyCache.getTypeHierarchy(_type); List<IType> types = new LinkedList<IType>(); types.add(_type); for (IType type : typeHierarchy.getAllSupertypes(_type)) { types.add(type); } return types; }
@Override protected IType getParentType(IType type) { ITypeHierarchy hierarchy = getHierarchy(); if (hierarchy != null) { return hierarchy.getSuperclass(type); // don't handle interfaces } return null; }
private int getDepth(ITypeHierarchy hierarchy, IType input) { int count = 0; IType superType = hierarchy.getSuperclass(input); while (superType != null) { count++; superType = hierarchy.getSuperclass(superType); } return count; }
public int getExpandLevel() { ITypeHierarchy hierarchy = getHierarchy(); if (hierarchy != null) { IType input = hierarchy.getType(); if (input != null) { return getDepth(hierarchy, input) + 2; } else { return 5; } } return 2; }
public List<IType> getSubtypesInProject(IJavaProject project) throws JavaModelException { // System.out.println("TypeCache.getSubtypesOf: " + type.getFullyQualifiedName() + " (hits=" + // SubTypeHierarchyCache.getCacheHits() + ",misses=" + SubTypeHierarchyCache.getCacheMisses() // + ")"); ITypeHierarchy typeHierarchy = SubTypeHierarchyCache.getTypeHierarchyInProject(_type, project); List<IType> types = new LinkedList<IType>(); IType[] subtypes = typeHierarchy.getAllSubtypes(_type); for (int subtypeNum = subtypes.length - 1; subtypeNum >= 0; subtypeNum--) { types.add(subtypes[subtypeNum]); } types.add(_type); return types; }
/* * Ensure that the hierarchy using a working copy owner doesn't have primary working copy owner type * that are hidden by a type of the working copy owner * (regression test for bug 133372 [hierarchy] Type hierarchy returns type twice if executed on working copy layer) */ public void testHierarchy() throws CoreException { try { createFile("/P/Y.java", "public class Y extends X {\n" + "}"); WorkingCopyOwner owner = new TestWorkingCopyOwner(); this.workingCopy = getCompilationUnit("/P/Y.java").getWorkingCopy(owner, null /*no progress*/); IType focus = getCompilationUnit("/P/X.java").getType("X"); ITypeHierarchy hierarchy = focus.newTypeHierarchy(owner, null /*no progress*/); IType[] subtypes = hierarchy.getSubtypes(focus); assertTypesEqual("Unexpected types", "Y\n", subtypes); } finally { deleteFile("/P/Y.java"); } }
/** * @param progressMonitor * @param uriMappings * @param resource * @param methodsStack * @throws CoreException */ private void resolveResourcesUriMappings( final Resource resource, final String uriTemplateFragment, final Map<ResolvedUriMapping, Stack<ResourceMethod>> uriMappings, final Stack<ResourceMethod> methodsStack, final IProgressMonitor progressMonitor) throws CoreException { // resource resourceMethods and subresources resourceMethods are treated the same way for (ResourceMethod resourceMethod : resource.getAllMethods()) { String uriPathTemplate = resolveURIPathTemplate(uriTemplateFragment, resource, resourceMethod); MediaTypeCapabilities mediaTypeCapabilities = resolveMediaTypeCapabilities(resource, resourceMethod); UriMapping resourceUriMapping = resourceMethod.getUriMapping(); ResolvedUriMapping uriMapping = new ResolvedUriMapping( resourceUriMapping.getHTTPMethod(), uriPathTemplate, resourceUriMapping.getQueryParams(), mediaTypeCapabilities); @SuppressWarnings("unchecked") Stack<ResourceMethod> stack = (Stack<ResourceMethod>) methodsStack.clone(); stack.add(resourceMethod); uriMappings.put(uriMapping, stack); } // TODO : verify support chain of subresource locators // TODO : stack resourceMethods and detect+prevent cycles for (ResourceMethod resourceMethod : resource.getSubresourceLocators()) { String uriPathTemplate = resolveURIPathTemplate(uriTemplateFragment, resource, resourceMethod); IType returnType = resourceMethod.getReturnType(); if (returnType == null) { continue; } ITypeHierarchy subresourceTypeHierarchy = JdtUtils.resolveTypeHierarchy(returnType, false, progressMonitor); for (IType subresourceType : subresourceTypeHierarchy.getSubtypes(returnType)) { Resource subresource = getByType(subresourceType); if (subresource != null && !subresource.isRootResource()) { @SuppressWarnings("unchecked") Stack<ResourceMethod> stack = (Stack<ResourceMethod>) methodsStack.clone(); stack.add(resourceMethod); resolveResourcesUriMappings( subresource, uriPathTemplate, uriMappings, stack, progressMonitor); } } } }
/* (non-Javadoc) * @see org.eclipse.jdt.internal.ui.typehierarchy.TypeHierarchyContentProvider#getRootTypes(java.util.List) */ @Override protected final void getRootTypes(List<IType> res) { ITypeHierarchy hierarchy = getHierarchy(); if (hierarchy != null) { IType input = hierarchy.getType(); if (input == null) { IType[] classes = hierarchy.getRootClasses(); for (int i = 0; i < classes.length; i++) { res.add(classes[i]); } IType[] interfaces = hierarchy.getRootInterfaces(); for (int i = 0; i < interfaces.length; i++) { res.add(interfaces[i]); } } else { if (Flags.isInterface(hierarchy.getCachedFlags(input))) { res.add(input); } else if (isAnonymousFromInterface(input)) { res.add(hierarchy.getSuperInterfaces(input)[0]); } else { IType[] roots = hierarchy.getRootClasses(); for (int i = 0; i < roots.length; i++) { if (isObject(roots[i])) { res.add(roots[i]); return; } } res.addAll(Arrays.asList(roots)); // something wrong with the hierarchy } } } }
/* * Create the list of focused jars or projects. */ private static IJavaElement[] getFocusedElementsAndTypes( SearchPattern pattern, IJavaElement focusElement, ObjectVector superTypes) throws JavaModelException { if (pattern instanceof MethodPattern) { // For method pattern, it needs to walk along the focus type super hierarchy // and add jars/projects of all the encountered types. IType type = (IType) pattern.focus.getAncestor(IJavaElement.TYPE); MethodPattern methodPattern = (MethodPattern) pattern; String selector = new String(methodPattern.selector); int parameterCount = methodPattern.parameterCount; ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null); IType[] allTypes = superHierarchy.getAllSupertypes(type); int length = allTypes.length; SimpleSet focusSet = new SimpleSet(length + 1); if (focusElement != null) focusSet.add(focusElement); for (int i = 0; i < length; i++) { IMethod[] methods = allTypes[i].getMethods(); int mLength = methods.length; for (int m = 0; m < mLength; m++) { if (parameterCount == methods[m].getNumberOfParameters() && methods[m].getElementName().equals(selector)) { IPackageFragmentRoot root = (IPackageFragmentRoot) allTypes[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); IJavaElement element = root.isArchive() ? root : root.getParent(); focusSet.add(element); if (superTypes != null) superTypes.add(allTypes[i]); break; } } } // Rebuilt a contiguous array IJavaElement[] focuses = new IJavaElement[focusSet.elementSize]; Object[] values = focusSet.values; int count = 0; for (int i = values.length; --i >= 0; ) { if (values[i] != null) { focuses[count++] = (IJavaElement) values[i]; } } return focuses; } if (focusElement == null) return new IJavaElement[0]; return new IJavaElement[] {focusElement}; }
private void checkOverridden(RefactoringStatus status, IProgressMonitor pm) throws JavaModelException { pm.beginTask("", 9); // $NON-NLS-1$ pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden); MethodDeclaration decl = fSourceProvider.getDeclaration(); IMethod method = (IMethod) decl.resolveBinding().getJavaElement(); if (method == null || Flags.isPrivate(method.getFlags())) { pm.worked(8); return; } IType type = method.getDeclaringType(); ITypeHierarchy hierarchy = type.newTypeHierarchy(new SubProgressMonitor(pm, 6)); checkSubTypes(status, method, hierarchy.getAllSubtypes(type), new SubProgressMonitor(pm, 1)); checkSuperClasses( status, method, hierarchy.getAllSuperclasses(type), new SubProgressMonitor(pm, 1)); checkSuperInterfaces( status, method, hierarchy.getAllSuperInterfaces(type), new SubProgressMonitor(pm, 1)); pm.setTaskName(""); // $NON-NLS-1$ }
/** * Remembers the selection of a right hand side type (proposal type) for a certain left hand side * (expected type) in content assist. * * @param lhs the left hand side / expected type * @param rhs the selected right hand side */ public void remember(IType lhs, IType rhs) { Assert.isLegal(lhs != null); Assert.isLegal(rhs != null); try { if (!isCacheableRHS(rhs)) return; ITypeHierarchy hierarchy = rhs.newSupertypeHierarchy(getProgressMonitor()); if (hierarchy.contains(lhs)) { // TODO remember for every member of the LHS hierarchy or not? Yes for now. IType[] allLHSides = hierarchy.getAllSupertypes(lhs); String rhsQualifiedName = rhs.getFullyQualifiedName(); for (int i = 0; i < allLHSides.length; i++) rememberInternal(allLHSides[i], rhsQualifiedName); rememberInternal(lhs, rhsQualifiedName); } } catch (JavaModelException x) { JavaPlugin.log(x); } }
/** * Finds a method declaration in a type's hierarchy. The search is top down, so this returns the * first declaration of the method in the hierarchy. This searches for a method with a name and * signature. Parameter types are only compared by the simple name, no resolving for the fully * qualified type name is done. Constructors are only compared by parameters, not the name. * * @param type Searches in this type's supertypes. * @param name The name of the method to find * @param paramTypes The type signatures of the parameters e.g. <code>{"QString;","I"}</code> * @param isConstructor If the method is a constructor * @return The first method found or null, if nothing found */ private static IMethod findMethodDeclarationInHierarchy( ITypeHierarchy hierarchy, IType type, String name, String[] paramTypes, boolean isConstructor) throws JavaModelException { IType[] superTypes = hierarchy.getAllSupertypes(type); for (int i = superTypes.length - 1; i >= 0; i--) { IMethod first = JavaModelUtil.findMethod(name, paramTypes, isConstructor, superTypes[i]); if (first != null && !Flags.isPrivate(first.getFlags())) { // the order getAllSupertypes does make assumptions of the order of inner elements -> search // recursively IMethod res = findMethodDeclarationInHierarchy( hierarchy, first.getDeclaringType(), name, paramTypes, isConstructor); if (res != null) { return res; } return first; } } return null; }
// ------- private static IMethod[] classesDeclareMethodName( ITypeHierarchy hier, List<IType> classes, IMethod method, String newName) throws CoreException { Set<IMethod> result = new HashSet<>(); IType type = method.getDeclaringType(); List<IType> subtypes = Arrays.asList(hier.getAllSubtypes(type)); int parameterCount = method.getParameterTypes().length; boolean isMethodPrivate = JdtFlags.isPrivate(method); for (Iterator<IType> iter = classes.iterator(); iter.hasNext(); ) { IType clazz = iter.next(); IMethod[] methods = clazz.getMethods(); boolean isSubclass = subtypes.contains(clazz); for (int j = 0; j < methods.length; j++) { IMethod foundMethod = Checks.findMethod(newName, parameterCount, false, new IMethod[] {methods[j]}); if (foundMethod == null) continue; if (isSubclass || type.equals(clazz)) result.add(foundMethod); else if ((!isMethodPrivate) && (!JdtFlags.isPrivate(methods[j]))) result.add(foundMethod); } } return result.toArray(new IMethod[result.size()]); }
public static XArrayList<IJavaElement> getSuperInterfaces(IJavaElement type) throws Exception { ITypeHierarchy h = ((IType) type).newTypeHierarchy(null); return IJavaElementAnalyzer.convertArray2XArrayList(h.getSuperInterfaces((IType) type)); }