/** * determines if the element implements a given interface * * @param element the element to check for the interface * @param qname the fully qualified name of the interface to check for * @return true if the element does implement the interface, false otherwise */ private boolean implementsInterface(IJavaElement element, String qname) { try { IType type = getType(element); if (type != null) { IType[] itypes = type.newSupertypeHierarchy(new NullProgressMonitor()).getAllInterfaces(); for (int i = 0; i < itypes.length; i++) { if (itypes[i].getFullyQualifiedName().equals(qname)) { return true; } } } } catch (JavaModelException e) { } return false; }
/** * Determines if the element has qname as a parent class * * @param element the element to check for the parent class definition * @param qname the fully qualified name of the (potential) parent class * @return true if qname is a parent class, false otherwise */ private boolean hasSuperclass(IJavaElement element, String qname) { try { IType type = getType(element); if (type != null) { IType[] stypes = type.newSupertypeHierarchy(new NullProgressMonitor()).getAllSuperclasses(type); for (int i = 0; i < stypes.length; i++) { if (stypes[i].getFullyQualifiedName().equals(qname) || stypes[i].getElementName().equals(qname)) { return true; } } } } catch (JavaModelException e) { } return false; }
/* * 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}; }
/** * 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); } }