Search in sources :

Example 46 with IMethod

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

the class MethodOverrideTester method findOverridingMethodInType.

/**
	 * Finds an overriding method in a type.
	 * @param overridingType The type to find methods in
	 * @param overridden The overridden method
	 * @return The overriding method or <code>null</code> if no method is overriding.
	 * @throws JavaModelException if a problem occurs
	 */
public IMethod findOverridingMethodInType(IType overridingType, IMethod overridden) throws JavaModelException {
    int flags = overridden.getFlags();
    if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overridden.isConstructor())
        return null;
    IMethod[] overridingMethods = overridingType.getMethods();
    for (int i = 0; i < overridingMethods.length; i++) {
        IMethod overriding = overridingMethods[i];
        flags = overriding.getFlags();
        if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overriding.isConstructor())
            continue;
        if (isSubsignature(overriding, overridden)) {
            return overriding;
        }
    }
    return null;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod)

Example 47 with IMethod

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

the class MethodOverrideTester method findDeclaringMethod.

/**
	 * Finds the method that declares the given method. A declaring method is the 'original' method declaration that does
	 * not override nor implement a method. <code>null</code> is returned it the given method does not override
	 * a method. When searching, super class are examined before implemented interfaces.
	 * @param overriding the overriding method
	 * @param testVisibility If true the result is tested on visibility. Null is returned if the method is not visible.
	 * @return the declaring method, or <code>null</code>
	 * @throws JavaModelException if a problem occurs
	 */
public IMethod findDeclaringMethod(IMethod overriding, boolean testVisibility) throws JavaModelException {
    IMethod result = null;
    IMethod overridden = findOverriddenMethod(overriding, testVisibility);
    while (overridden != null) {
        result = overridden;
        overridden = findOverriddenMethod(result, testVisibility);
    }
    return result;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod)

Example 48 with IMethod

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

the class MethodOverrideTester method getVariableErasure.

private String getVariableErasure(IMember context, String variableName) throws JavaModelException {
    IType type;
    if (context instanceof IMethod) {
        String subst = getMethodSubstitions((IMethod) context).getErasure(variableName);
        if (subst != null) {
            return subst;
        }
        type = context.getDeclaringType();
    } else {
        type = (IType) context;
    }
    String subst = getTypeSubstitions(type).getErasure(variableName);
    if (subst != null) {
        return subst;
    }
    IJavaElement parent = type.getParent();
    if (parent instanceof IMethod) {
        return getVariableErasure((IMethod) parent, variableName);
    } else if (type.getDeclaringType() != null) {
        return getVariableErasure(type.getDeclaringType(), variableName);
    }
    // not a type variable
    return variableName;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 49 with IMethod

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

the class MethodsSourcePositionComparator method compareInTheSameType.

private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
    try {
        IMethod firstMethod = (IMethod) firstMethodBinding.getJavaElement();
        IMethod secondMethod = (IMethod) secondMethodBinding.getJavaElement();
        if (firstMethod == null || secondMethod == null) {
            return 0;
        }
        ISourceRange firstSourceRange = firstMethod.getSourceRange();
        ISourceRange secondSourceRange = secondMethod.getSourceRange();
        if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) {
            return firstMethod.getElementName().compareTo(secondMethod.getElementName());
        } else {
            return firstSourceRange.getOffset() - secondSourceRange.getOffset();
        }
    } catch (JavaModelException e) {
        return 0;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 50 with IMethod

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

the class MethodOverrideTester method findOverriddenMethod.

/**
	 * Finds the method that is overridden by the given method.
	 * First the super class is examined and then the implemented interfaces.
	 * @param overriding the overriding method
	 * @param testVisibility If true the result is tested on visibility. Null is returned if the method is not visible.
	 * @return a method that is directly overridden by the given method, or <code>null</code>
	 * @throws JavaModelException if a problem occurs
	 */
public IMethod findOverriddenMethod(IMethod overriding, boolean testVisibility) throws JavaModelException {
    int flags = overriding.getFlags();
    if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overriding.isConstructor()) {
        return null;
    }
    IType type = overriding.getDeclaringType();
    IType superClass = fHierarchy.getSuperclass(type);
    if (superClass != null) {
        IMethod res = findOverriddenMethodInHierarchy(superClass, overriding);
        if (res != null) {
            if (!testVisibility || JavaModelUtil.isVisibleInHierarchy(res, type.getPackageFragment())) {
                return res;
            }
        }
    }
    IType[] interfaces = fHierarchy.getSuperInterfaces(type);
    for (int i = 0; i < interfaces.length; i++) {
        IMethod res = findOverriddenMethodInHierarchy(interfaces[i], overriding);
        if (res != null) {
            // methods from interfaces are always public and therefore visible
            return res;
        }
    }
    return null;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)217 IType (org.eclipse.jdt.core.IType)112 IJavaElement (org.eclipse.jdt.core.IJavaElement)56 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)37 ArrayList (java.util.ArrayList)35 JavaModelException (org.eclipse.jdt.core.JavaModelException)32 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 IField (org.eclipse.jdt.core.IField)27 Test (org.junit.Test)22 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)19 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)19 IMember (org.eclipse.jdt.core.IMember)15 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)14 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)13 IJavaProject (org.eclipse.jdt.core.IJavaProject)12 HashSet (java.util.HashSet)11 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 ISourceRange (org.eclipse.jdt.core.ISourceRange)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)9