Search in sources :

Example 51 with Expression

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

the class CallContext method getReceiverType.

public ITypeBinding getReceiverType() {
    Expression expression = Invocations.getExpression(invocation);
    if (expression != null) {
        return expression.resolveTypeBinding();
    }
    IMethodBinding method = Invocations.resolveBinding(invocation);
    if (method != null) {
        return method.getDeclaringClass();
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Expression(org.eclipse.jdt.core.dom.Expression)

Example 52 with Expression

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

the class CallInliner method needsExplicitCast.

/**
	 * @param status the status
	 * @return <code>true</code> if explicit cast is needed otherwise <code>false</code>
	 */
private boolean needsExplicitCast(RefactoringStatus status) {
    // returned expression then we don't need an explicit cast.
    if (fSourceProvider.returnTypeMatchesReturnExpressions())
        return false;
    List<Expression> returnExprs = fSourceProvider.getReturnExpressions();
    // method invocations
    if (returnExprs.size() != 1)
        return false;
    if (fTargetNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) fTargetNode.getParent();
        if (methodInvocation.getExpression() == fTargetNode)
            return false;
        IMethodBinding method = methodInvocation.resolveMethodBinding();
        if (method == null) {
            status.addError(RefactoringCoreMessages.CallInliner_cast_analysis_error, JavaStatusContext.create(fCUnit, methodInvocation));
            return false;
        }
        ITypeBinding[] parameters = method.getParameterTypes();
        int argumentIndex = methodInvocation.arguments().indexOf(fInvocation);
        ITypeBinding parameterType = returnExprs.get(0).resolveTypeBinding();
        if (method.isVarargs() && argumentIndex >= parameters.length - 1) {
            argumentIndex = parameters.length - 1;
            parameterType = parameterType.createArrayType(1);
        }
        parameters[argumentIndex] = parameterType;
        ITypeBinding type = ASTNodes.getReceiverTypeBinding(methodInvocation);
        TypeBindingVisitor visitor = new AmbiguousMethodAnalyzer(fTypeEnvironment, method, fTypeEnvironment.create(parameters));
        if (!visitor.visit(type)) {
            return true;
        } else if (type.isInterface()) {
            return !Bindings.visitInterfaces(type, visitor);
        } else if (Modifier.isAbstract(type.getModifiers())) {
            return !Bindings.visitHierarchy(type, visitor);
        } else {
            // it is not needed to visit interfaces if receiver is a concrete class
            return !Bindings.visitSuperclasses(type, visitor);
        }
    } else {
        ITypeBinding explicitCast = ASTNodes.getExplicitCast(returnExprs.get(0), (Expression) fTargetNode);
        return explicitCast != null;
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) 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) TypeBindingVisitor(org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 53 with Expression

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

the class CallInliner method replaceCall.

private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGroup textEditGroup) {
    // Inline empty body
    if (blocks.length == 0 && fTargetNode != null) {
        if (fNeedsStatement) {
            fRewrite.replace(fTargetNode, fTargetNode.getAST().newEmptyStatement(), textEditGroup);
        } else {
            fRewrite.remove(fTargetNode, textEditGroup);
        }
    } else {
        ASTNode node = null;
        for (int i = 0; i < blocks.length - 1; i++) {
            node = fRewrite.createStringPlaceholder(blocks[i], ASTNode.RETURN_STATEMENT);
            fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
        }
        String block = blocks[blocks.length - 1];
        // returned expression must be evaluated.
        if (fContext.callMode == ASTNode.EXPRESSION_STATEMENT && fSourceProvider.hasReturnValue()) {
            if (fSourceProvider.mustEvaluateReturnedExpression()) {
                if (fSourceProvider.returnValueNeedsLocalVariable()) {
                    IMethodBinding invocation = Invocations.resolveBinding(fInvocation);
                    node = createLocalDeclaration(invocation.getReturnType(), fInvocationScope.createName(fSourceProvider.getMethodName(), true), (Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                } else {
                    node = fRewrite.getAST().newExpressionStatement((Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                }
            } else {
                node = null;
            }
        } else if (fTargetNode instanceof Expression) {
            node = fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION);
            // fixes bug #24941
            if (needsExplicitCast(status)) {
                AST ast = node.getAST();
                CastExpression castExpression = ast.newCastExpression();
                Type returnType = fImportRewrite.addImport(fSourceProvider.getReturnType(), ast);
                castExpression.setType(returnType);
                if (NecessaryParenthesesChecker.needsParentheses(fSourceProvider.getReturnExpressions().get(0), castExpression, CastExpression.EXPRESSION_PROPERTY)) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
                castExpression.setExpression((Expression) node);
                node = castExpression;
                if (NecessaryParenthesesChecker.needsParentheses(castExpression, fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
            } else if (fSourceProvider.needsReturnedExpressionParenthesis(fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                ParenthesizedExpression pExp = fTargetNode.getAST().newParenthesizedExpression();
                pExp.setExpression((Expression) node);
                node = pExp;
            }
        } else {
            node = fRewrite.createStringPlaceholder(block, ASTNode.RETURN_STATEMENT);
        }
        // Now replace the target node with the source node
        if (node != null) {
            if (fTargetNode == null) {
                fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
            } else {
                fRewrite.replace(fTargetNode, node, textEditGroup);
            }
        } else {
            if (fTargetNode != null) {
                fRewrite.remove(fTargetNode, textEditGroup);
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) 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) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 54 with Expression

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

the class ConvertAnonymousToNestedRefactoring method copyArguments.

private void copyArguments(CompilationUnitRewrite rewrite, ClassInstanceCreation newClassCreation) {
    Iterator<Expression> iter = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).arguments().iterator();
    if (!iter.hasNext())
        return;
    IMethodBinding superConstructorBinding = getSuperConstructorBinding();
    ITypeBinding[] parameterTypes = superConstructorBinding.getParameterTypes();
    List<Expression> arguments = newClassCreation.arguments();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    int last = parameterTypes.length - 1;
    for (int i = 0; i < last; i++) {
        arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
    }
    if (superConstructorBinding.isVarargs()) {
        AST ast = astRewrite.getAST();
        ArrayCreation arrayCreation = ast.newArrayCreation();
        arrayCreation.setType((ArrayType) rewrite.getImportRewrite().addImport(parameterTypes[last], ast));
        ArrayInitializer initializer = ast.newArrayInitializer();
        arrayCreation.setInitializer(initializer);
        arguments.add(arrayCreation);
        arguments = initializer.expressions();
    }
    while (iter.hasNext()) {
        arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 55 with Expression

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

the class ConvertAnonymousToNestedRefactoring method updateAndMoveBodyDeclarations.

private void updateAndMoveBodyDeclarations(CompilationUnitRewrite rewriter, IVariableBinding[] bindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations, MethodDeclaration newConstructorDecl) {
    final ASTRewrite astRewrite = rewriter.getASTRewrite();
    final AST ast = astRewrite.getAST();
    final boolean useThisAccess = useThisForFieldAccess();
    int fieldInsertIndex = newConstructorDecl != null ? newBodyDeclarations.lastIndexOf(newConstructorDecl) : newBodyDeclarations.size();
    for (Iterator<BodyDeclaration> iterator = fAnonymousInnerClassNode.bodyDeclarations().iterator(); iterator.hasNext(); ) {
        BodyDeclaration body = iterator.next();
        for (int i = 0; i < bindings.length; i++) {
            SimpleName[] names = LinkedNodeFinder.findByBinding(body, bindings[i]);
            String fieldName = fieldNames[i];
            for (int k = 0; k < names.length; k++) {
                SimpleName newNode = ast.newSimpleName(fieldName);
                if (useThisAccess) {
                    FieldAccess access = ast.newFieldAccess();
                    access.setExpression(ast.newThisExpression());
                    access.setName(newNode);
                    astRewrite.replace(names[k], access, null);
                } else {
                    astRewrite.replace(names[k], newNode, null);
                }
                addLinkedPosition(KEY_FIELD_NAME_EXT + i, newNode, astRewrite, false);
            }
        }
        if (body instanceof Initializer || body instanceof FieldDeclaration) {
            newBodyDeclarations.add(fieldInsertIndex++, (BodyDeclaration) astRewrite.createMoveTarget(body));
        } else {
            newBodyDeclarations.add((BodyDeclaration) astRewrite.createMoveTarget(body));
        }
    }
    if (newConstructorDecl != null) {
        // move initialization of existing fields to constructor if an outer is referenced
        List<Statement> bodyStatements = newConstructorDecl.getBody().statements();
        List<VariableDeclarationFragment> fieldsToInitializeInConstructor = getFieldsToInitializeInConstructor();
        for (Iterator<VariableDeclarationFragment> iter = fieldsToInitializeInConstructor.iterator(); iter.hasNext(); ) {
            VariableDeclarationFragment fragment = iter.next();
            Expression initializer = fragment.getInitializer();
            Expression replacement = (Expression) astRewrite.get(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY);
            if (replacement == initializer) {
                replacement = (Expression) astRewrite.createMoveTarget(initializer);
            }
            astRewrite.remove(initializer, null);
            SimpleName fieldNameNode = ast.newSimpleName(fragment.getName().getIdentifier());
            bodyStatements.add(newFieldAssignment(ast, fieldNameNode, replacement, useThisAccess));
        }
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Statement(org.eclipse.jdt.core.dom.Statement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer) Initializer(org.eclipse.jdt.core.dom.Initializer) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Aggregations

Expression (org.eclipse.jdt.core.dom.Expression)552 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)263 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)213 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)187 CastExpression (org.eclipse.jdt.core.dom.CastExpression)185 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)135 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)126 ASTNode (org.eclipse.jdt.core.dom.ASTNode)125 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)122 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)121 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)112 AST (org.eclipse.jdt.core.dom.AST)101 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)95 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)88 SimpleName (org.eclipse.jdt.core.dom.SimpleName)83 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)76 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)73 Type (org.eclipse.jdt.core.dom.Type)70 ArrayList (java.util.ArrayList)69 Block (org.eclipse.jdt.core.dom.Block)63