Java Code Examples for org.eclipse.jdt.core.ITypeHierarchy#getSubtypes()

The following examples show how to use org.eclipse.jdt.core.ITypeHierarchy#getSubtypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeProposalUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
static IType[] computeInheritancePath(IType subType, IType superType) throws JavaModelException {
	if (superType == null) {
		return null;
	}

	// optimization: avoid building the type hierarchy for the identity case
	if (superType.equals(subType)) {
		return new IType[] { subType };
	}

	ITypeHierarchy hierarchy= subType.newSupertypeHierarchy(new NullProgressMonitor());
	if (!hierarchy.contains(superType))
	{
		return null; // no path
	}

	List<IType> path= new LinkedList<>();
	path.add(superType);
	do {
		// any sub type must be on a hierarchy chain from superType to subType
		superType= hierarchy.getSubtypes(superType)[0];
		path.add(superType);
	} while (!superType.equals(subType)); // since the equality case is handled above, we can spare one check

	return path.toArray(new IType[path.size()]);
}
 
Example 2
Source File: DispatchRenameSupport.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected Iterable<JvmGenericType> getSubTypes(JvmGenericType type, ResourceSet tempResourceSet) {
	IType javaType = (IType) javaElementFinder.findExactElementFor(type);
	List<JvmGenericType> allSubTypes = newArrayList();
	try {
		ITypeHierarchy typeHierarchy = javaType.newTypeHierarchy(javaType.getJavaProject(),
				new NullProgressMonitor());
		for (IType subType : typeHierarchy.getSubtypes(javaType)) {
			EObject jvmSubType = jvmElementFinder.getCorrespondingJvmElement(subType, tempResourceSet);
			if (jvmSubType instanceof JvmGenericType) {
				EObject indexJvmSubType = jvmElementFinder.findJvmElementDeclarationInIndex(jvmSubType, subType
						.getJavaProject().getProject(), type.eResource().getResourceSet());
				if (indexJvmSubType instanceof JvmGenericType) {
					allSubTypes.add((JvmGenericType) indexJvmSubType);
				} else {
					EObject jvmSubTypeInOtherResourceSet = type.eResource().getResourceSet().getEObject(EcoreUtil2.getPlatformResourceOrNormalizedURI(jvmSubType), true);
					if(jvmSubTypeInOtherResourceSet instanceof JvmGenericType)
						allSubTypes.add((JvmGenericType) jvmSubTypeInOtherResourceSet);
				}
			}
		}
	} catch (JavaModelException e) {
		LOG.error("Error calculating subtypes", e);
	}
	return allSubTypes;

}
 
Example 3
Source File: SubTypeHierarchyViewer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@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)) {
					res.add(curr);
				}
			}
		} else {
			for (int i= 0; i < types.length; i++) {
				res.add(types[i]);
			}
		}
	}

}
 
Example 4
Source File: LazyGenericTypeProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Computes one inheritance path from <code>superType</code> to <code>subType</code> or
 * <code>null</code> if <code>subType</code> does not inherit from <code>superType</code>. Note
 * that there may be more than one inheritance path - this method simply returns one.
 * <p>
 * The returned array contains <code>superType</code> at its first index, and
 * <code>subType</code> at its last index. If <code>subType</code> equals <code>superType</code>
 * , an array of length 1 is returned containing that type.
 * </p>
 * 
 * @param subType the sub type
 * @param superType the super type
 * @return an inheritance path from <code>superType</code> to <code>subType</code>, or
 *         <code>null</code> if <code>subType</code> does not inherit from
 *         <code>superType</code>
 * @throws JavaModelException if this element does not exist or if an exception occurs while
 *             accessing its corresponding resource
 */
private IType[] computeInheritancePath(IType subType, IType superType) throws JavaModelException {
	if (superType == null)
		return null;

	// optimization: avoid building the type hierarchy for the identity case
	if (superType.equals(subType))
		return new IType[] { subType };

	ITypeHierarchy hierarchy= subType.newSupertypeHierarchy(getProgressMonitor());
	if (!hierarchy.contains(superType))
		return null; // no path

	List<IType> path= new LinkedList<IType>();
	path.add(superType);
	do {
		// any sub type must be on a hierarchy chain from superType to subType
		superType= hierarchy.getSubtypes(superType)[0];
		path.add(superType);
	} while (!superType.equals(subType)); // since the equality case is handled above, we can spare one check

	return path.toArray(new IType[path.size()]);
}
 
Example 5
Source File: PullUpMethodPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean anySubtypeCanBeShown(final IType type, final Map<IType, IMember[]> typeToMemberArray, final ITypeHierarchy hierarchy) {
	final IType[] subTypes= hierarchy.getSubtypes(type);
	for (int i= 0; i < subTypes.length; i++) {
		if (canBeShown(subTypes[i], typeToMemberArray, hierarchy))
			return true;
	}
	return false;
}