Search in sources :

Example 1 with ThisExpression

use of org.eclipse.jdt.core.dom.ThisExpression in project che by eclipse.

the class UnresolvedElementsSubProcessor method addMissingCastParentsProposal.

private static void addMissingCastParentsProposal(ICompilationUnit cu, MethodInvocation invocationNode, Collection<ICommandAccess> proposals) {
    Expression sender = invocationNode.getExpression();
    if (sender instanceof ThisExpression) {
        return;
    }
    ITypeBinding senderBinding = sender.resolveTypeBinding();
    if (senderBinding == null || Modifier.isFinal(senderBinding.getModifiers())) {
        return;
    }
    if (sender instanceof Name && ((Name) sender).resolveBinding() instanceof ITypeBinding) {
        // static access
        return;
    }
    ASTNode parent = invocationNode.getParent();
    while (parent instanceof Expression && parent.getNodeType() != ASTNode.CAST_EXPRESSION) {
        parent = parent.getParent();
    }
    boolean hasCastProposal = false;
    if (parent instanceof CastExpression) {
        //	(TestCase) x.getName() -> ((TestCase) x).getName
        hasCastProposal = useExistingParentCastProposal(cu, (CastExpression) parent, sender, invocationNode.getName(), getArgumentTypes(invocationNode.arguments()), proposals);
    }
    if (!hasCastProposal) {
        // x.getName() -> ((TestCase) x).getName
        Expression target = sender;
        while (target instanceof ParenthesizedExpression) {
            target = ((ParenthesizedExpression) target).getExpression();
        }
        String label;
        if (target.getNodeType() != ASTNode.CAST_EXPRESSION) {
            String targetName = null;
            if (target.getLength() <= 18) {
                targetName = ASTNodes.asString(target);
            }
            if (targetName == null) {
                label = CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast_description;
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
            }
        } else {
            String targetName = null;
            if (target.getLength() <= 18) {
                targetName = ASTNodes.asString(((CastExpression) target).getExpression());
            }
            if (targetName == null) {
                label = CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast_description;
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
            }
        }
        proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));
    }
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) CastCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CastCorrectionProposal)

Example 2 with ThisExpression

use of org.eclipse.jdt.core.dom.ThisExpression in project che by eclipse.

the class SourceProvider method updateImplicitReceivers.

private void updateImplicitReceivers(ASTRewrite rewriter, CallContext context) {
    if (context.receiver == null)
        return;
    List<Expression> implicitReceivers = fAnalyzer.getImplicitReceivers();
    for (Iterator<Expression> iter = implicitReceivers.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, context.importer);
        if (node instanceof MethodInvocation) {
            final MethodInvocation inv = (MethodInvocation) node;
            rewriter.set(inv, MethodInvocation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, (IMethodBinding) inv.getName().resolveBinding(), importRewriteContext), null);
        } else if (node instanceof ClassInstanceCreation) {
            final ClassInstanceCreation inst = (ClassInstanceCreation) node;
            rewriter.set(inst, ClassInstanceCreation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, inst.resolveConstructorBinding(), importRewriteContext), null);
        } else if (node instanceof ThisExpression) {
            rewriter.replace(node, rewriter.createStringPlaceholder(context.receiver, ASTNode.METHOD_INVOCATION), null);
        } else if (node instanceof FieldAccess) {
            final FieldAccess access = (FieldAccess) node;
            rewriter.set(access, FieldAccess.EXPRESSION_PROPERTY, createReceiver(rewriter, context, access.resolveFieldBinding(), importRewriteContext), null);
        } else if (node instanceof SimpleName && ((SimpleName) node).resolveBinding() instanceof IVariableBinding) {
            IVariableBinding vb = (IVariableBinding) ((SimpleName) node).resolveBinding();
            if (vb.isField()) {
                Expression receiver = createReceiver(rewriter, context, vb, importRewriteContext);
                if (receiver != null) {
                    FieldAccess access = node.getAST().newFieldAccess();
                    ASTNode target = rewriter.createMoveTarget(node);
                    access.setName((SimpleName) target);
                    access.setExpression(receiver);
                    rewriter.replace(node, access, null);
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 3 with ThisExpression

use of org.eclipse.jdt.core.dom.ThisExpression in project che by eclipse.

the class CallInliner method computeReceiver.

private void computeReceiver() throws BadLocationException {
    Expression receiver = Invocations.getExpression(fInvocation);
    if (receiver == null)
        return;
    final boolean isName = receiver instanceof Name;
    if (isName)
        fContext.receiverIsStatic = ((Name) receiver).resolveBinding() instanceof ITypeBinding;
    if (ASTNodes.isLiteral(receiver) || isName || receiver instanceof ThisExpression) {
        fContext.receiver = fBuffer.getDocument().get(receiver.getStartPosition(), receiver.getLength());
        return;
    }
    switch(fSourceProvider.getReceiversToBeUpdated()) {
        case 0:
            // Make sure we evaluate the current receiver. Best is to assign to
            // local.
            fLocals.add(createLocalDeclaration(receiver.resolveTypeBinding(), //$NON-NLS-1$
            fInvocationScope.createName("r", true), (Expression) fRewrite.createCopyTarget(receiver)));
            return;
        case 1:
            fContext.receiver = fBuffer.getDocument().get(receiver.getStartPosition(), receiver.getLength());
            return;
        default:
            //$NON-NLS-1$
            String local = fInvocationScope.createName("r", true);
            fLocals.add(createLocalDeclaration(receiver.resolveTypeBinding(), local, (Expression) fRewrite.createCopyTarget(receiver)));
            fContext.receiver = local;
            return;
    }
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 4 with ThisExpression

use of org.eclipse.jdt.core.dom.ThisExpression in project che by eclipse.

the class ExtractMethodRefactoring method createCallNodes.

//---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
    List<ASTNode> result = new ArrayList<ASTNode>(2);
    IVariableBinding[] locals = fAnalyzer.getCallerLocals();
    for (int i = 0; i < locals.length; i++) {
        result.add(createDeclaration(locals[i], null));
    }
    MethodInvocation invocation = fAST.newMethodInvocation();
    invocation.setName(fAST.newSimpleName(fMethodName));
    ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
    RefactoringStatus status = new RefactoringStatus();
    while (fDestination != typeNode) {
        fAnalyzer.checkInput(status, fMethodName, typeNode);
        if (!status.isOK()) {
            SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
            if ((modifiers & Modifier.STATIC) == 0) {
                ThisExpression thisExpression = fAST.newThisExpression();
                thisExpression.setQualifier(destinationTypeName);
                invocation.setExpression(thisExpression);
            } else {
                invocation.setExpression(destinationTypeName);
            }
            break;
        }
        typeNode = typeNode.getParent();
    }
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo parameter = fParameterInfos.get(i);
        arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
    }
    if (fLinkedProposalModel != null) {
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
    }
    ASTNode call;
    int returnKind = fAnalyzer.getReturnKind();
    switch(returnKind) {
        case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
            IVariableBinding binding = fAnalyzer.getReturnLocal();
            if (binding != null) {
                VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
                call = decl;
            } else {
                Assignment assignment = fAST.newAssignment();
                assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
                assignment.setRightHandSide(invocation);
                call = assignment;
            }
            break;
        case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression(invocation);
            call = rs;
            break;
        default:
            call = invocation;
    }
    if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
        call = fAST.newExpressionStatement((Expression) call);
    }
    result.add(call);
    // return;
    if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
        result.add(fAST.newReturnStatement());
    }
    return result.toArray(new ASTNode[result.size()]);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 5 with ThisExpression

use of org.eclipse.jdt.core.dom.ThisExpression 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)

Aggregations

ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)19 SimpleName (org.eclipse.jdt.core.dom.SimpleName)17 Expression (org.eclipse.jdt.core.dom.Expression)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)12 Name (org.eclipse.jdt.core.dom.Name)10 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)8 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)8 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 AST (org.eclipse.jdt.core.dom.AST)5 Assignment (org.eclipse.jdt.core.dom.Assignment)5 Block (org.eclipse.jdt.core.dom.Block)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4