Search in sources :

Example 11 with ITypeHierarchy

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

the class ChangeSignatureProcessor method checkInitialConditions.

/* (non-Javadoc)
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws CoreException {
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 5);
        RefactoringStatus result = Checks.checkIfCuBroken(fMethod);
        if (result.hasFatalError())
            return result;
        if (fMethod == null || !fMethod.exists()) {
            String message = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_method_deleted, BasicElementLabels.getFileName(getCu()));
            return RefactoringStatus.createFatalErrorStatus(message);
        }
        if (fMethod.getDeclaringType().isInterface()) {
            fTopMethod = MethodChecks.overridesAnotherMethod(fMethod, fMethod.getDeclaringType().newSupertypeHierarchy(new SubProgressMonitor(monitor, 1)));
            monitor.worked(1);
        } else if (MethodChecks.isVirtual(fMethod)) {
            ITypeHierarchy hierarchy = getCachedTypeHierarchy(new SubProgressMonitor(monitor, 1));
            fTopMethod = MethodChecks.isDeclaredInInterface(fMethod, hierarchy, new SubProgressMonitor(monitor, 1));
            if (fTopMethod == null)
                fTopMethod = MethodChecks.overridesAnotherMethod(fMethod, hierarchy);
        }
        if (fTopMethod == null)
            fTopMethod = fMethod;
        if (!fTopMethod.equals(fMethod)) {
            if (fTopMethod.getDeclaringType().isInterface()) {
                RefactoringStatusContext context = JavaStatusContext.create(fTopMethod);
                String message = Messages.format(RefactoringCoreMessages.MethodChecks_implements, new String[] { JavaElementUtil.createMethodSignature(fTopMethod), BasicElementLabels.getJavaElementName(fTopMethod.getDeclaringType().getFullyQualifiedName('.')) });
                return RefactoringStatus.createStatus(RefactoringStatus.FATAL, message, context, Corext.getPluginId(), RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE, fTopMethod);
            } else {
                RefactoringStatusContext context = JavaStatusContext.create(fTopMethod);
                String message = Messages.format(RefactoringCoreMessages.MethodChecks_overrides, new String[] { JavaElementUtil.createMethodSignature(fTopMethod), BasicElementLabels.getJavaElementName(fTopMethod.getDeclaringType().getFullyQualifiedName('.')) });
                return RefactoringStatus.createStatus(RefactoringStatus.FATAL, message, context, Corext.getPluginId(), RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD, fTopMethod);
            }
        }
        if (monitor.isCanceled())
            throw new OperationCanceledException();
        if (fBaseCuRewrite == null || !fBaseCuRewrite.getCu().equals(getCu())) {
            fBaseCuRewrite = new CompilationUnitRewrite(getCu());
            fBaseCuRewrite.getASTRewrite().setTargetSourceRangeComputer(new TightSourceRangeComputer());
        }
        RefactoringStatus[] status = TypeContextChecker.checkMethodTypesSyntax(fMethod, getParameterInfos(), fReturnTypeInfo);
        for (int i = 0; i < status.length; i++) {
            result.merge(status[i]);
        }
        monitor.worked(1);
        result.merge(createExceptionInfoList());
        monitor.worked(1);
        return result;
    } finally {
        monitor.done();
    }
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 12 with ITypeHierarchy

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

the class MemberVisibilityAdjustor method thresholdTypeToType.

/**
	 * Returns the visibility threshold from a type to another type.
	 *
	 * @param referencing the referencing type
	 * @param referenced the referenced type
	 * @param monitor the progress monitor to use
	 * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
	 * @throws JavaModelException if the java elements could not be accessed
	 */
private ModifierKeyword thresholdTypeToType(final IType referencing, final IType referenced, final IProgressMonitor monitor) throws JavaModelException {
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    final ICompilationUnit referencedUnit = referenced.getCompilationUnit();
    if (referencing.equals(referenced.getDeclaringType()))
        keyword = ModifierKeyword.PRIVATE_KEYWORD;
    else {
        final ITypeHierarchy hierarchy = getTypeHierarchy(referencing, new SubProgressMonitor(monitor, 1));
        final IType[] types = hierarchy.getSupertypes(referencing);
        IType superType = null;
        for (int index = 0; index < types.length; index++) {
            superType = types[index];
            if (superType.equals(referenced)) {
                keyword = null;
                return keyword;
            }
        }
    }
    final ICompilationUnit typeUnit = referencing.getCompilationUnit();
    if (referencedUnit != null && referencedUnit.equals(typeUnit)) {
        if (referenced.getDeclaringType() != null)
            keyword = null;
        else
            keyword = ModifierKeyword.PRIVATE_KEYWORD;
    } else if (referencedUnit != null && typeUnit != null && referencedUnit.getParent().equals(typeUnit.getParent()))
        keyword = null;
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Example 13 with ITypeHierarchy

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

the class ContentAssistHistory method remember.

/**
	 * 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);
    }
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) JavaModelException(org.eclipse.jdt.core.JavaModelException) IType(org.eclipse.jdt.core.IType)

Example 14 with ITypeHierarchy

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

the class LazyGenericTypeProposal method computeInheritancePath.

/**
	 * 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 org.eclipse.jdt.core.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))
        // 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 : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) LinkedList(java.util.LinkedList) IType(org.eclipse.jdt.core.IType)

Example 15 with ITypeHierarchy

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

the class IndexSelector method getFocusedElementsAndTypes.

/*
 * 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 };
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) MethodPattern(org.eclipse.jdt.internal.core.search.matching.MethodPattern) IType(org.eclipse.jdt.core.IType) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod)

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