Search in sources :

Example 21 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project flux by eclipse.

the class TypeProposalUtils method computeInheritancePath.

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))
        // no path
        return null;
    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 (// since the equality case is handled above, we can spare one check
    !superType.equals(subType));
    return path.toArray(new IType[path.size()]);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) LinkedList(java.util.LinkedList) IType(org.eclipse.jdt.core.IType)

Example 22 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class RenameFieldProcessor method canEnableGetterRenaming.

//-- getter/setter --------------------------------------------------
/**
	 * @return Error message or <code>null</code> if getter can be renamed.
	 * @throws CoreException should not happen
	 */
public String canEnableGetterRenaming() throws CoreException {
    if (fField.getDeclaringType().isInterface())
        //$NON-NLS-1$
        return getGetter() == null ? "" : null;
    IMethod getter = getGetter();
    if (getter == null)
        //$NON-NLS-1$
        return "";
    final NullProgressMonitor monitor = new NullProgressMonitor();
    if (MethodChecks.isVirtual(getter)) {
        final ITypeHierarchy hierarchy = getter.getDeclaringType().newTypeHierarchy(monitor);
        if (MethodChecks.isDeclaredInInterface(getter, hierarchy, monitor) != null || MethodChecks.overridesAnotherMethod(getter, hierarchy) != null)
            return RefactoringCoreMessages.RenameFieldRefactoring_declared_in_supertype;
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod)

Example 23 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class RenameFieldProcessor method canEnableSetterRenaming.

/**
	 * @return Error message or <code>null</code> if setter can be renamed.
	 * @throws CoreException should not happen
	 */
public String canEnableSetterRenaming() throws CoreException {
    if (fField.getDeclaringType().isInterface())
        //$NON-NLS-1$
        return getSetter() == null ? "" : null;
    IMethod setter = getSetter();
    if (setter == null)
        //$NON-NLS-1$
        return "";
    final NullProgressMonitor monitor = new NullProgressMonitor();
    if (MethodChecks.isVirtual(setter)) {
        final ITypeHierarchy hierarchy = setter.getDeclaringType().newTypeHierarchy(monitor);
        if (MethodChecks.isDeclaredInInterface(setter, hierarchy, monitor) != null || MethodChecks.overridesAnotherMethod(setter, hierarchy) != null)
            return RefactoringCoreMessages.RenameFieldRefactoring_declared_in_supertype;
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod)

Example 24 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class MethodChecks method getTopmostMethod.

/**
	 * Locates the topmost method of an override ripple and returns it. If none
	 * is found, null is returned.
	 *
	 * @param method the IMethod which may be part of a ripple
	 * @param typeHierarchy a ITypeHierarchy of the declaring type of the method. May be null
	 * @param monitor an IProgressMonitor
	 * @return the topmost method of the ripple, or null if none
	 * @throws JavaModelException
	 */
public static IMethod getTopmostMethod(IMethod method, ITypeHierarchy typeHierarchy, IProgressMonitor monitor) throws JavaModelException {
    Assert.isNotNull(method);
    ITypeHierarchy hierarchy = typeHierarchy;
    IMethod topmostMethod = null;
    final IType declaringType = method.getDeclaringType();
    if (!declaringType.isInterface()) {
        if ((hierarchy == null) || !declaringType.equals(hierarchy.getType()))
            hierarchy = declaringType.newTypeHierarchy(monitor);
        IMethod inInterface = isDeclaredInInterface(method, hierarchy, monitor);
        if (inInterface != null && !inInterface.equals(method))
            topmostMethod = inInterface;
    }
    if (topmostMethod == null) {
        if (hierarchy == null)
            hierarchy = declaringType.newSupertypeHierarchy(monitor);
        IMethod overrides = overridesAnotherMethod(method, hierarchy);
        if (overrides != null && !overrides.equals(method))
            topmostMethod = overrides;
    }
    return topmostMethod;
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 25 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class RippleMethodFinder2 method findAllRippleMethods.

private IMethod[] findAllRippleMethods(IProgressMonitor pm, WorkingCopyOwner owner) throws CoreException {
    //$NON-NLS-1$
    pm.beginTask("", 4);
    findAllDeclarations(new SubProgressMonitor(pm, 1), owner);
    //check for bug 81058:
    if (!fDeclarations.contains(fMethod))
        //$NON-NLS-1$
        Assert.isTrue(false, "Search for method declaration did not find original element: " + fMethod.toString());
    createHierarchyOfDeclarations(new SubProgressMonitor(pm, 1), owner);
    createTypeToMethod();
    createUnionFind();
    if (pm.isCanceled())
        throw new OperationCanceledException();
    fHierarchy = null;
    fRootTypes = null;
    Map<IType, List<IType>> partitioning = new HashMap<IType, List<IType>>();
    for (Iterator<IType> iter = fTypeToMethod.keySet().iterator(); iter.hasNext(); ) {
        IType type = iter.next();
        IType rep = fUnionFind.find(type);
        List<IType> types = partitioning.get(rep);
        if (types == null)
            types = new ArrayList<IType>();
        types.add(type);
        partitioning.put(rep, types);
    }
    Assert.isTrue(partitioning.size() > 0);
    if (partitioning.size() == 1)
        return fDeclarations.toArray(new IMethod[fDeclarations.size()]);
    //Multiple partitions; must look out for nasty marriage cases
    //(types inheriting method from two ancestors, but without redeclaring it).
    IType methodTypeRep = fUnionFind.find(fMethod.getDeclaringType());
    List<IType> relatedTypes = partitioning.get(methodTypeRep);
    boolean hasRelatedInterfaces = false;
    List<IMethod> relatedMethods = new ArrayList<IMethod>();
    for (Iterator<IType> iter = relatedTypes.iterator(); iter.hasNext(); ) {
        IType relatedType = iter.next();
        relatedMethods.add(fTypeToMethod.get(relatedType));
        if (relatedType.isInterface())
            hasRelatedInterfaces = true;
    }
    //Definition: An alien type is a type that is not a related type. The set of
    // alien types diminishes as new types become related (a.k.a marry a relatedType).
    List<IMethod> alienDeclarations = new ArrayList<IMethod>(fDeclarations);
    fDeclarations = null;
    alienDeclarations.removeAll(relatedMethods);
    List<IType> alienTypes = new ArrayList<IType>();
    boolean hasAlienInterfaces = false;
    for (Iterator<IMethod> iter = alienDeclarations.iterator(); iter.hasNext(); ) {
        IMethod alienDeclaration = iter.next();
        IType alienType = alienDeclaration.getDeclaringType();
        alienTypes.add(alienType);
        if (alienType.isInterface())
            hasAlienInterfaces = true;
    }
    if (//no nasty marriage scenarios without types to marry with...
    alienTypes.size() == 0)
        return relatedMethods.toArray(new IMethod[relatedMethods.size()]);
    if (//no nasty marriage scenarios without interfaces...
    !hasRelatedInterfaces && !hasAlienInterfaces)
        return relatedMethods.toArray(new IMethod[relatedMethods.size()]);
    //find all subtypes of related types:
    HashSet<IType> relatedSubTypes = new HashSet<IType>();
    List<IType> relatedTypesToProcess = new ArrayList<IType>(relatedTypes);
    while (relatedTypesToProcess.size() > 0) {
        //TODO: would only need subtype hierarchies of all top-of-ripple relatedTypesToProcess
        for (Iterator<IType> iter = relatedTypesToProcess.iterator(); iter.hasNext(); ) {
            if (pm.isCanceled())
                throw new OperationCanceledException();
            IType relatedType = iter.next();
            ITypeHierarchy hierarchy = getCachedHierarchy(relatedType, owner, new SubProgressMonitor(pm, 1));
            if (hierarchy == null)
                hierarchy = relatedType.newTypeHierarchy(owner, new SubProgressMonitor(pm, 1));
            IType[] allSubTypes = hierarchy.getAllSubtypes(relatedType);
            for (int i = 0; i < allSubTypes.length; i++) relatedSubTypes.add(allSubTypes[i]);
        }
        //processed; make sure loop terminates
        relatedTypesToProcess.clear();
        HashSet<IType> marriedAlienTypeReps = new HashSet<IType>();
        for (Iterator<IType> iter = alienTypes.iterator(); iter.hasNext(); ) {
            if (pm.isCanceled())
                throw new OperationCanceledException();
            IType alienType = iter.next();
            IMethod alienMethod = fTypeToMethod.get(alienType);
            ITypeHierarchy hierarchy = getCachedHierarchy(alienType, owner, new SubProgressMonitor(pm, 1));
            if (hierarchy == null)
                hierarchy = alienType.newTypeHierarchy(owner, new SubProgressMonitor(pm, 1));
            IType[] allSubtypes = hierarchy.getAllSubtypes(alienType);
            for (int i = 0; i < allSubtypes.length; i++) {
                IType subtype = allSubtypes[i];
                if (relatedSubTypes.contains(subtype)) {
                    if (JavaModelUtil.isVisibleInHierarchy(alienMethod, subtype.getPackageFragment())) {
                        marriedAlienTypeReps.add(fUnionFind.find(alienType));
                    } else {
                    // not overridden
                    }
                }
            }
        }
        if (marriedAlienTypeReps.size() == 0)
            return relatedMethods.toArray(new IMethod[relatedMethods.size()]);
        for (Iterator<IType> iter = marriedAlienTypeReps.iterator(); iter.hasNext(); ) {
            IType marriedAlienTypeRep = iter.next();
            List<IType> marriedAlienTypes = partitioning.get(marriedAlienTypeRep);
            for (Iterator<IType> iterator = marriedAlienTypes.iterator(); iterator.hasNext(); ) {
                IType marriedAlienInterfaceType = iterator.next();
                relatedMethods.add(fTypeToMethod.get(marriedAlienInterfaceType));
            }
            //not alien any more
            alienTypes.removeAll(marriedAlienTypes);
            //process freshly married types again
            relatedTypesToProcess.addAll(marriedAlienTypes);
        }
    }
    fRootReps = null;
    fRootHierarchies = null;
    fTypeToMethod = null;
    fUnionFind = null;
    return relatedMethods.toArray(new IMethod[relatedMethods.size()]);
}
Also used : HashMap(java.util.HashMap) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) ArrayList(java.util.ArrayList) List(java.util.List) IMethod(org.eclipse.jdt.core.IMethod) HashSet(java.util.HashSet)

Aggregations

ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)37 IType (org.eclipse.jdt.core.IType)25 IMethod (org.eclipse.jdt.core.IMethod)14 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 ArrayList (java.util.ArrayList)6 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)4 MethodOverrideTester (org.eclipse.jdt.internal.corext.util.MethodOverrideTester)4 Type (org.eclipse.che.ide.ext.java.shared.dto.model.Type)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 RefactoringStatusContext (org.eclipse.ltk.core.refactoring.RefactoringStatusContext)2 HashMap (java.util.HashMap)1 List (java.util.List)1