Search in sources :

Example 11 with IMethod

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

the class RenameMethodProcessor method doCheckFinalConditions.

@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
    try {
        RefactoringStatus result = new RefactoringStatus();
        //$NON-NLS-1$
        pm.beginTask("", 9);
        // TODO workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=40367
        if (!Checks.isAvailable(fMethod)) {
            result.addFatalError(RefactoringCoreMessages.RenameMethodProcessor_is_binary, JavaStatusContext.create(fMethod));
            return result;
        }
        result.merge(Checks.checkIfCuBroken(fMethod));
        if (result.hasFatalError())
            return result;
        pm.setTaskName(RefactoringCoreMessages.RenameMethodRefactoring_taskName_checkingPreconditions);
        result.merge(checkNewElementName(getNewElementName()));
        if (result.hasFatalError())
            return result;
        boolean mustAnalyzeShadowing;
        IMethod[] newNameMethods = searchForDeclarationsOfClashingMethods(new SubProgressMonitor(pm, 1));
        if (newNameMethods.length == 0) {
            mustAnalyzeShadowing = false;
            pm.worked(1);
        } else {
            IType[] outerTypes = searchForOuterTypesOfReferences(newNameMethods, new SubProgressMonitor(pm, 1));
            if (outerTypes.length > 0) {
                //There exists a reference to a clashing method, where the reference is in a nested type.
                //That nested type could be a type in a ripple method's hierarchy, which could
                //cause the reference to bind to the new ripple method instead of to
                //its old binding (a method of an enclosing scope).
                //-> Getting *more* references than before -> Semantics not preserved.
                //Examples: RenameVirtualMethodInClassTests#testFail39() and #testFail41()
                //TODO: could pass declaringTypes to the RippleMethodFinder and check whether
                //a hierarchy contains one of outerTypes (or an outer type of an outerType, recursively).
                mustAnalyzeShadowing = true;
            } else {
                boolean hasOldRefsInInnerTypes = true;
                //(recursively), whether they declare a rippleMethod
                if (hasOldRefsInInnerTypes) {
                    //There exists a reference to a ripple method in a nested type
                    //of a type in the hierarchy of any ripple method.
                    //When that reference is renamed, and one of the supertypes of the
                    //nested type declared a method matching the new name, then
                    //the renamed reference will bind to the method in its supertype,
                    //since inherited methods bind stronger than methods from enclosing scopes.
                    //Getting *less* references than before -> Semantics not preserved.
                    //Examples: RenamePrivateMethodTests#testFail2(), RenamePrivateMethodTests#testFail5()
                    mustAnalyzeShadowing = true;
                } else {
                    mustAnalyzeShadowing = false;
                }
            }
        }
        String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(getCurrentElementName()));
        ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
        initializeMethodsToRename(new SubProgressMonitor(pm, 1), binaryRefs);
        pm.setTaskName(RefactoringCoreMessages.RenameMethodRefactoring_taskName_searchingForReferences);
        fOccurrences = getOccurrences(new SubProgressMonitor(pm, 3), result, binaryRefs);
        binaryRefs.addErrorIfNecessary(result);
        pm.setTaskName(RefactoringCoreMessages.RenameMethodRefactoring_taskName_checkingPreconditions);
        if (fUpdateReferences)
            result.merge(checkRelatedMethods());
        //removes CUs with syntax errors
        result.merge(analyzeCompilationUnits());
        pm.worked(1);
        if (result.hasFatalError())
            return result;
        createChanges(new SubProgressMonitor(pm, 1), result);
        if (fUpdateReferences & mustAnalyzeShadowing)
            result.merge(analyzeRenameChanges(new SubProgressMonitor(pm, 1)));
        else
            pm.worked(1);
        return result;
    } finally {
        pm.done();
    }
}
Also used : ReferencesInBinaryContext(org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Example 12 with IMethod

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

the class RenameMethodProcessor method checkRelatedMethods.

private RefactoringStatus checkRelatedMethods() throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    for (Iterator<IMethod> iter = fMethodsToRename.iterator(); iter.hasNext(); ) {
        IMethod method = iter.next();
        result.merge(Checks.checkIfConstructorName(method, getNewElementName(), method.getDeclaringType().getElementName()));
        String[] msgData = new String[] { BasicElementLabels.getJavaElementName(method.getElementName()), BasicElementLabels.getJavaElementName(method.getDeclaringType().getFullyQualifiedName('.')) };
        if (!method.exists()) {
            result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_not_in_model, msgData));
            continue;
        }
        if (method.isBinary())
            result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_binary, msgData));
        if (method.isReadOnly())
            result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_read_only, msgData));
        if (JdtFlags.isNative(method))
            result.addError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_native_1, msgData));
    }
    return result;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod)

Example 13 with IMethod

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

the class RenameMethodProcessor method classesDeclareMethodName.

//-------
private static IMethod[] classesDeclareMethodName(ITypeHierarchy hier, List<IType> classes, IMethod method, String newName) throws CoreException {
    Set<IMethod> result = new HashSet<IMethod>();
    IType type = method.getDeclaringType();
    List<IType> subtypes = Arrays.asList(hier.getAllSubtypes(type));
    int parameterCount = method.getParameterTypes().length;
    boolean isMethodPrivate = JdtFlags.isPrivate(method);
    for (Iterator<IType> iter = classes.iterator(); iter.hasNext(); ) {
        IType clazz = iter.next();
        IMethod[] methods = clazz.getMethods();
        boolean isSubclass = subtypes.contains(clazz);
        for (int j = 0; j < methods.length; j++) {
            IMethod foundMethod = Checks.findMethod(newName, parameterCount, false, new IMethod[] { methods[j] });
            if (foundMethod == null)
                continue;
            if (isSubclass || type.equals(clazz))
                result.add(foundMethod);
            else if ((!isMethodPrivate) && (!JdtFlags.isPrivate(methods[j])))
                result.add(foundMethod);
        }
    }
    return result.toArray(new IMethod[result.size()]);
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) HashSet(java.util.HashSet) IType(org.eclipse.jdt.core.IType)

Example 14 with IMethod

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

the class RenameMethodProcessor method getDeclarationCUs.

private ICompilationUnit[] getDeclarationCUs() {
    Set<ICompilationUnit> cus = new HashSet<ICompilationUnit>();
    for (Iterator<IMethod> iter = fMethodsToRename.iterator(); iter.hasNext(); ) {
        IMethod method = iter.next();
        cus.add(method.getCompilationUnit());
    }
    return cus.toArray(new ICompilationUnit[cus.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethod(org.eclipse.jdt.core.IMethod) HashSet(java.util.HashSet)

Example 15 with IMethod

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

the class RenameMethodProcessor method searchForDeclarationsOfClashingMethods.

private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException {
    final List<IMethod> results = new ArrayList<IMethod>();
    SearchPattern pattern = createNewMethodPattern();
    IJavaSearchScope scope = RefactoringScopeFactory.create(getMethod().getJavaProject());
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object method = match.getElement();
            if (// check for bug 90138: [refactoring] [rename] Renaming method throws internal exception
            method instanceof IMethod)
                results.add((IMethod) method);
            else
                //$NON-NLS-1$
                JavaPlugin.logErrorMessage("Unexpected element in search match: " + match.toString());
        }
    };
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    return results.toArray(new IMethod[results.size()]);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) ArrayList(java.util.ArrayList) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)160 IType (org.eclipse.jdt.core.IType)87 IJavaElement (org.eclipse.jdt.core.IJavaElement)36 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)28 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)22 IField (org.eclipse.jdt.core.IField)21 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)14 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)13 IMember (org.eclipse.jdt.core.IMember)13 JavaModelException (org.eclipse.jdt.core.JavaModelException)12 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 HashSet (java.util.HashSet)8 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)8 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)7