Search in sources :

Example 26 with IMethod

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

the class IntroduceIndirectionRefactoring method updateTargetVisibility.

private RefactoringStatus updateTargetVisibility(IProgressMonitor monitor) throws JavaModelException, CoreException {
    RefactoringStatus result = new RefactoringStatus();
    // Adjust the visibility of the method and of the referenced type. Note that
    // the target method may not be in the target type; and in this case, the type
    // of the target method does not need a visibility adjustment.
    // This method is called after all other changes have been
    // created. Changes induced by this method will be attached to those changes.
    result.merge(adjustVisibility((IType) fIntermediaryFirstParameterType.getJavaElement(), fIntermediaryType, monitor));
    if (result.hasError())
        // binary
        return result;
    ModifierKeyword neededVisibility = getNeededVisibility(fTargetMethod, fIntermediaryType);
    if (neededVisibility != null) {
        result.merge(adjustVisibility(fTargetMethod, neededVisibility, monitor));
        if (result.hasError())
            // binary
            return result;
        // Need to adjust the overridden methods of the target method.
        ITypeHierarchy hierarchy = fTargetMethod.getDeclaringType().newTypeHierarchy(null);
        MethodOverrideTester tester = new MethodOverrideTester(fTargetMethod.getDeclaringType(), hierarchy);
        IType[] subtypes = hierarchy.getAllSubtypes(fTargetMethod.getDeclaringType());
        for (int i = 0; i < subtypes.length; i++) {
            IMethod method = tester.findOverridingMethodInType(subtypes[i], fTargetMethod);
            if (method != null && method.exists()) {
                result.merge(adjustVisibility(method, neededVisibility, monitor));
                if (monitor.isCanceled())
                    throw new OperationCanceledException();
                if (result.hasError())
                    // binary
                    return result;
            }
        }
    }
    return result;
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 27 with IMethod

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

the class IntroduceFactoryRefactoring method searchForCallsTo.

/**
	 * Search for all calls to the given <code>IMethodBinding</code> in the project
	 * that contains the compilation unit <code>fCUHandle</code>.
	 * @param methodBinding
	 * @param pm
	 * @param status
	 * @return an array of <code>SearchResultGroup</code>'s that identify the search matches
	 * @throws JavaModelException
	 */
private SearchResultGroup[] searchForCallsTo(IMethodBinding methodBinding, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
    IMethod method = (IMethod) methodBinding.getJavaElement();
    final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(createSearchPattern(method, methodBinding));
    engine.setFiltering(true, true);
    engine.setScope(createSearchScope(method, methodBinding));
    engine.setStatus(status);
    engine.searchPattern(new SubProgressMonitor(pm, 1));
    return (SearchResultGroup[]) engine.getResults();
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 28 with IMethod

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

the class InlineMethodRefactoring method checkOverridden.

private void checkOverridden(RefactoringStatus status, IProgressMonitor pm) throws JavaModelException {
    //$NON-NLS-1$
    pm.beginTask("", 9);
    pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden);
    MethodDeclaration decl = fSourceProvider.getDeclaration();
    IMethod method = (IMethod) decl.resolveBinding().getJavaElement();
    if (method == null || Flags.isPrivate(method.getFlags())) {
        pm.worked(8);
        return;
    }
    IType type = method.getDeclaringType();
    ITypeHierarchy hierarchy = type.newTypeHierarchy(new SubProgressMonitor(pm, 6));
    checkSubTypes(status, method, hierarchy.getAllSubtypes(type), new SubProgressMonitor(pm, 1));
    checkSuperClasses(status, method, hierarchy.getAllSuperclasses(type), new SubProgressMonitor(pm, 1));
    checkSuperInterfaces(status, method, hierarchy.getAllSuperInterfaces(type), new SubProgressMonitor(pm, 1));
    //$NON-NLS-1$
    pm.setTaskName("");
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IMethod(org.eclipse.jdt.core.IMethod) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Example 29 with IMethod

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

the class InlineMethodRefactoring method resolveSourceProvider.

private static SourceProvider resolveSourceProvider(RefactoringStatus status, ITypeRoot typeRoot, ASTNode invocation) {
    CompilationUnit root = (CompilationUnit) invocation.getRoot();
    IMethodBinding methodBinding = Invocations.resolveBinding(invocation);
    if (methodBinding == null) {
        status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
        return null;
    }
    MethodDeclaration declaration = (MethodDeclaration) root.findDeclaringNode(methodBinding);
    if (declaration != null) {
        return new SourceProvider(typeRoot, declaration);
    }
    IMethod method = (IMethod) methodBinding.getJavaElement();
    if (method != null) {
        CompilationUnit methodDeclarationAstRoot;
        ICompilationUnit methodCu = method.getCompilationUnit();
        if (methodCu != null) {
            methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(methodCu, true);
        } else {
            IClassFile classFile = method.getClassFile();
            if (!JavaElementUtil.isSourceAvailable(classFile)) {
                String methodLabel = JavaElementLabels.getTextLabel(method, JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES);
                status.addFatalError(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_error_classFile, methodLabel));
                return null;
            }
            methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(classFile, true);
        }
        ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getMethodDeclaration().getKey());
        if (node instanceof MethodDeclaration) {
            return new SourceProvider(methodDeclarationAstRoot.getTypeRoot(), (MethodDeclaration) node);
        }
    }
    status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) IClassFile(org.eclipse.jdt.core.IClassFile) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 30 with IMethod

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

the class IntroduceIndirectionRefactoring method updateReferences.

private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    //$NON-NLS-1$
    monitor.beginTask("", 90);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
    if (result.hasFatalError())
        return result;
    result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup group = references[i];
        SearchMatch[] searchResults = group.getSearchResults();
        CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
        for (int j = 0; j < searchResults.length; j++) {
            SearchMatch match = searchResults[j];
            if (match.isInsideDocComment())
                continue;
            IMember enclosingMember = (IMember) match.getElement();
            ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
            if (target instanceof SuperMethodInvocation) {
                // Cannot retarget calls to super - add a warning
                result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
                continue;
            }
            //$NON-NLS-1$
            Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
            MethodInvocation invocation = (MethodInvocation) target;
            ITypeBinding typeBinding = getExpressionType(invocation);
            if (fIntermediaryFirstParameterType == null) {
                // no highest type yet
                fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
            } else {
                // check if current type is higher
                result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
            }
            if (result.hasFatalError())
                return result;
            // create an edit for this particular call
            result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
            if (monitor.isCanceled())
                throw new OperationCanceledException();
        }
        if (!isRewriteKept(group.getCompilationUnit()))
            createChangeAndDiscardRewrite(group.getCompilationUnit());
        monitor.worked(ticksPerCU);
    }
    monitor.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IMember(org.eclipse.jdt.core.IMember) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)144 IType (org.eclipse.jdt.core.IType)79 IJavaElement (org.eclipse.jdt.core.IJavaElement)30 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)28 ArrayList (java.util.ArrayList)18 IField (org.eclipse.jdt.core.IField)15 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 Test (org.junit.Test)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 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)6