Search in sources :

Example 71 with RefactoringStatus

use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.

the class IntroduceIndirectionRefactoring method setIntermediaryMethodName.

public RefactoringStatus setIntermediaryMethodName(String newMethodName) {
    Assert.isNotNull(newMethodName);
    fIntermediaryMethodName = newMethodName;
    IJavaElement context = fIntermediaryType != null ? fIntermediaryType : (IMember) fTargetMethod;
    RefactoringStatus stat = Checks.checkMethodName(newMethodName, context);
    stat.merge(checkOverloading());
    return stat;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 72 with RefactoringStatus

use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.

the class IntroduceIndirectionRefactoring method updateMethodInvocation.

// ******************* UPDATE CALLS **********************
private RefactoringStatus updateMethodInvocation(MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) throws JavaModelException {
    RefactoringStatus status = new RefactoringStatus();
    // call as the new target method may have additional parameters
    if (originalInvocation.typeArguments().size() > 0)
        return createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_type_arguments);
    MethodInvocation newInvocation = unitRewriter.getAST().newMethodInvocation();
    List<Expression> newInvocationArgs = newInvocation.arguments();
    List<Expression> originalInvocationArgs = originalInvocation.arguments();
    // static call => always use a qualifier
    String qualifier = unitRewriter.getImportRewrite().addImport(fIntermediaryTypeBinding);
    newInvocation.setExpression(ASTNodeFactory.newName(unitRewriter.getAST(), qualifier));
    newInvocation.setName(unitRewriter.getAST().newSimpleName(getIntermediaryMethodName()));
    final Expression expression = originalInvocation.getExpression();
    if (!isStaticTarget()) {
        // Add the expression as the first parameter
        if (expression == null) {
            // There is no expression for this call. Use a (possibly qualified) "this" expression.
            ThisExpression expr = unitRewriter.getAST().newThisExpression();
            RefactoringStatus qualifierStatus = qualifyThisExpression(expr, originalInvocation, enclosing, unitRewriter);
            status.merge(qualifierStatus);
            if (qualifierStatus.hasEntries())
                // warning means don't include this invocation
                return status;
            newInvocationArgs.add(expr);
        } else {
            Expression expressionAsParam = (Expression) unitRewriter.getASTRewrite().createMoveTarget(expression);
            newInvocationArgs.add(expressionAsParam);
        }
    } else {
        if (expression != null) {
            // be side effects (e.g. inside methods) -> don't update
            if (!(expression instanceof Name) || ASTNodes.getTypeBinding((Name) expression) == null)
                return createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_static_expression_access);
        }
    }
    for (int i = 0; i < originalInvocationArgs.size(); i++) {
        Expression originalInvocationArg = originalInvocationArgs.get(i);
        Expression movedArg = (Expression) unitRewriter.getASTRewrite().createMoveTarget(originalInvocationArg);
        newInvocationArgs.add(movedArg);
    }
    unitRewriter.getASTRewrite().replace(originalInvocation, newInvocation, unitRewriter.createGroupDescription(RefactoringCoreMessages.IntroduceIndirectionRefactoring_group_description_replace_call));
    return status;
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Name(org.eclipse.jdt.core.dom.Name)

Example 73 with RefactoringStatus

use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.

the class IntroduceIndirectionRefactoring method qualifyThisExpression.

/**
	 * Attempts to qualify a "this" expression for a method invocation with an appropriate qualifier.
	 * The invoked method is analyzed according to the following specs:
	 *
	 * 'this' must be qualified iff method is declared in an enclosing type or a supertype of an enclosing type
	 *
	 * 1) The method is declared somewhere outside of the cu of the invocation
	 *      1a) inside a supertype of the current type
	 *      1b) inside a supertype of an enclosing type
	 * 2) The method is declared inside of the cu of the invocation
	 * 		2a) inside the type of the invocation
	 * 		2b) outside the type of the invocation
	 *
	 * In case of 1a) and 2b), qualify with the enclosing type.
	 * @param expr a {@link ThisExpression}
	 * @param originalInvocation the original method invocation
	 * @param enclosing the enclosing member of the original method invocation
	 * @param unitRewriter the rewrite
	 * @return resulting status
	 *
	 */
private RefactoringStatus qualifyThisExpression(ThisExpression expr, MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) {
    RefactoringStatus status = new RefactoringStatus();
    IMethodBinding methodBinding = originalInvocation.resolveMethodBinding();
    MethodDeclaration methodDeclaration = (MethodDeclaration) ASTNodes.findDeclaration(methodBinding, originalInvocation.getRoot());
    ITypeBinding currentTypeBinding = null;
    if (methodDeclaration != null) {
        // Case 1) : Declaring type is inside this cu => use its name if it's declared in an enclosing type
        if (ASTNodes.isParent(originalInvocation, methodDeclaration.getParent()))
            currentTypeBinding = methodBinding.getDeclaringClass();
        else
            currentTypeBinding = ASTNodes.getEnclosingType(originalInvocation);
    } else {
        // Case 2) : Declaring type is outside of this cu => find subclass in this cu
        ASTNode currentTypeDeclaration = getEnclosingTypeDeclaration(originalInvocation);
        currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
        while (currentTypeDeclaration != null && (Bindings.findMethodInHierarchy(currentTypeBinding, methodBinding.getName(), methodBinding.getParameterTypes()) == null)) {
            currentTypeDeclaration = getEnclosingTypeDeclaration(currentTypeDeclaration.getParent());
            currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
        }
    }
    if (currentTypeBinding == null) {
        status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_declaring_type_not_found));
        return status;
    }
    currentTypeBinding = currentTypeBinding.getTypeDeclaration();
    ITypeBinding typeOfCall = ASTNodes.getEnclosingType(originalInvocation);
    if (!typeOfCall.equals(currentTypeBinding)) {
        if (currentTypeBinding.isAnonymous()) {
            // Cannot qualify, see bug 115277
            status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_anonymous_cannot_qualify));
        } else {
            expr.setQualifier(unitRewriter.getAST().newSimpleName(currentTypeBinding.getName()));
        }
    } else {
    // do not qualify, only use "this.".
    }
    return status;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 74 with RefactoringStatus

use of org.eclipse.ltk.core.refactoring.RefactoringStatus 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 75 with RefactoringStatus

use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.

the class IntroduceFactoryRefactoring method checkSelection.

/**
	 * Determines what kind of AST node was selected, and returns an error status
	 * if the kind of node is inappropriate for this refactoring.
	 * @param pm
	 * @return a RefactoringStatus indicating whether the selection is valid
	 * @throws JavaModelException
	 */
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
    try {
        pm.beginTask(RefactoringCoreMessages.IntroduceFactory_examiningSelection, 2);
        fSelectedNode = getTargetNode(fCUHandle, fSelectionStart, fSelectionLength);
        if (fSelectedNode == null)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_notAConstructorInvocation);
        // constructor MethodDeclaration; nothing else.
        if (fSelectedNode instanceof ClassInstanceCreation) {
            ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) fSelectedNode;
            fCtorBinding = classInstanceCreation.resolveConstructorBinding();
        } else if (fSelectedNode instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) fSelectedNode;
            fCtorBinding = methodDeclaration.resolveBinding();
        }
        if (fCtorBinding == null)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unableToResolveConstructorBinding);
        // If this constructor is of a generic type, get the generic version,
        // not some instantiation thereof.
        fCtorBinding = fCtorBinding.getMethodDeclaration();
        pm.worked(1);
        // We don't handle constructors of nested types at the moment
        if (fCtorBinding.getDeclaringClass().isNested())
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unsupportedNestedTypes);
        ITypeBinding ctorType = fCtorBinding.getDeclaringClass();
        IType ctorOwningType = (IType) ctorType.getJavaElement();
        if (ctorOwningType.isBinary())
            // Can't modify binary CU; don't know what CU to put factory method
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInBinaryClass);
        if (ctorOwningType.isEnum())
            // Doesn't make sense to encapsulate enum constructors
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInEnum);
        // Put the generated factory method inside the type that owns the constructor
        fFactoryUnitHandle = ctorOwningType.getCompilationUnit();
        fFactoryCU = getASTFor(fFactoryUnitHandle);
        Name ctorOwnerName = (Name) NodeFinder.perform(fFactoryCU, ctorOwningType.getNameRange());
        fCtorOwningClass = (AbstractTypeDeclaration) ASTNodes.getParent(ctorOwnerName, AbstractTypeDeclaration.class);
        fFactoryOwningClass = fCtorOwningClass;
        pm.worked(1);
        if (fNewMethodName == null)
            //$NON-NLS-1$
            return setNewMethodName("create" + fCtorBinding.getName());
        else
            return new RefactoringStatus();
    } finally {
        pm.done();
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IType(org.eclipse.jdt.core.IType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)251 IType (org.eclipse.jdt.core.IType)62 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)53 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)30 IMethod (org.eclipse.jdt.core.IMethod)29 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)28 IJavaElement (org.eclipse.jdt.core.IJavaElement)28 Test (org.junit.Test)26 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)24 ArrayList (java.util.ArrayList)23 BaseTest (org.eclipse.che.plugin.java.server.che.BaseTest)21 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)19 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)18 IFile (org.eclipse.core.resources.IFile)16 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 CoreException (org.eclipse.core.runtime.CoreException)15 IField (org.eclipse.jdt.core.IField)15 IStatus (org.eclipse.core.runtime.IStatus)14 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)13