Search in sources :

Example 31 with Expression

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

the class MissingReturnTypeCorrectionProposal method evaluateReturnExpressions.

/*
	 * Evaluates possible return expressions. The favourite expression is returned.
	 */
private Expression evaluateReturnExpressions(AST ast, ITypeBinding returnBinding, int returnOffset) {
    CompilationUnit root = getCU();
    Expression result = null;
    if (returnBinding != null) {
        result = computeProposals(ast, returnBinding, returnOffset, root, result);
    }
    Expression defaultExpression = createDefaultExpression(ast);
    addLinkedPositionProposal(RETURN_EXPRESSION_KEY, ASTNodes.asString(defaultExpression), null);
    if (result == null) {
        return defaultExpression;
    }
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Expression(org.eclipse.jdt.core.dom.Expression)

Example 32 with Expression

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

the class MissingReturnTypeCorrectionProposal method getRewrite.

/*(non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
	 */
@Override
protected ASTRewrite getRewrite() {
    AST ast = getAST();
    ITypeBinding returnBinding = getReturnTypeBinding();
    if (fExistingReturn != null) {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        Expression expression = evaluateReturnExpressions(ast, returnBinding, fExistingReturn.getStartPosition());
        if (expression != null) {
            rewrite.set(fExistingReturn, ReturnStatement.EXPRESSION_PROPERTY, expression, null);
            addLinkedPosition(rewrite.track(expression), true, RETURN_EXPRESSION_KEY);
        }
        return rewrite;
    } else {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ASTNode body = getBody();
        // For lambda the body can be a block or an expression.
        if (body instanceof Block) {
            Block block = (Block) body;
            List<Statement> statements = block.statements();
            int nStatements = statements.size();
            ASTNode lastStatement = null;
            if (nStatements > 0) {
                lastStatement = statements.get(nStatements - 1);
            }
            if (returnBinding != null && lastStatement instanceof ExpressionStatement && lastStatement.getNodeType() != ASTNode.ASSIGNMENT) {
                Expression expression = ((ExpressionStatement) lastStatement).getExpression();
                ITypeBinding binding = expression.resolveTypeBinding();
                if (binding != null && binding.isAssignmentCompatible(returnBinding)) {
                    Expression placeHolder = (Expression) rewrite.createMoveTarget(expression);
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(placeHolder);
                    rewrite.replace(lastStatement, returnStatement, null);
                    return rewrite;
                }
            }
            int offset;
            if (lastStatement == null) {
                offset = block.getStartPosition() + 1;
            } else {
                offset = lastStatement.getStartPosition() + lastStatement.getLength();
            }
            ReturnStatement returnStatement = ast.newReturnStatement();
            Expression expression = evaluateReturnExpressions(ast, returnBinding, offset);
            returnStatement.setExpression(expression);
            rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(returnStatement, null);
            addLinkedPosition(rewrite.track(returnStatement.getExpression()), true, RETURN_EXPRESSION_KEY);
        }
        return rewrite;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 33 with Expression

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

the class NewAnnotationMemberProposal method getNewType.

private Type getNewType(ASTRewrite rewrite) {
    AST ast = rewrite.getAST();
    Type newTypeNode = null;
    ITypeBinding binding = null;
    if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
        Expression value = ((MemberValuePair) fInvocationNode.getParent()).getValue();
        binding = value.resolveTypeBinding();
    } else if (fInvocationNode instanceof Expression) {
        binding = ((Expression) fInvocationNode).resolveTypeBinding();
    }
    if (binding != null) {
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(fInvocationNode, getImportRewrite());
        newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
    }
    if (newTypeNode == null) {
        //$NON-NLS-1$
        newTypeNode = ast.newSimpleType(ast.newSimpleName("String"));
    }
    addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
    return newTypeNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 34 with Expression

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

the class NewMethodCorrectionProposal method evaluateModifiers.

private int evaluateModifiers(ASTNode targetTypeDecl) {
    if (getSenderBinding().isAnnotation()) {
        return 0;
    }
    if (getSenderBinding().isInterface()) {
        // for interface and annotation members copy the modifiers from an existing field
        MethodDeclaration[] methodDecls = ((TypeDeclaration) targetTypeDecl).getMethods();
        if (methodDecls.length > 0) {
            return methodDecls[0].getModifiers();
        }
        return 0;
    }
    ASTNode invocationNode = getInvocationNode();
    if (invocationNode instanceof MethodInvocation) {
        int modifiers = 0;
        Expression expression = ((MethodInvocation) invocationNode).getExpression();
        if (expression != null) {
            if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
                modifiers |= Modifier.STATIC;
            }
        } else if (ASTResolving.isInStaticContext(invocationNode)) {
            modifiers |= Modifier.STATIC;
        }
        ASTNode node = ASTResolving.findParentType(invocationNode);
        if (targetTypeDecl.equals(node)) {
            modifiers |= Modifier.PRIVATE;
        } else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
            modifiers |= Modifier.PROTECTED;
            if (ASTResolving.isInStaticContext(node) && expression == null) {
                modifiers |= Modifier.STATIC;
            }
        } else {
            modifiers |= Modifier.PUBLIC;
        }
        return modifiers;
    }
    return Modifier.PUBLIC;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 35 with Expression

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

the class NewMethodCorrectionProposal method addNewParameters.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
	 */
@Override
protected void addNewParameters(ASTRewrite rewrite, List<String> takenNames, List<SingleVariableDeclaration> params) throws CoreException {
    AST ast = rewrite.getAST();
    List<Expression> arguments = fArguments;
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(getInvocationNode()), getImportRewrite());
    for (int i = 0; i < arguments.size(); i++) {
        Expression elem = arguments.get(i);
        SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
        // argument type
        //$NON-NLS-1$
        String argTypeKey = "arg_type_" + i;
        Type type = evaluateParameterType(ast, elem, argTypeKey, context);
        param.setType(type);
        // argument name
        //$NON-NLS-1$
        String argNameKey = "arg_name_" + i;
        String name = evaluateParameterName(takenNames, elem, type, argNameKey);
        param.setName(ast.newSimpleName(name));
        params.add(param);
        addLinkedPosition(rewrite.track(param.getType()), false, argTypeKey);
        addLinkedPosition(rewrite.track(param.getName()), false, argNameKey);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Expression(org.eclipse.jdt.core.dom.Expression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Aggregations

Expression (org.eclipse.jdt.core.dom.Expression)304 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)137 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)137 CastExpression (org.eclipse.jdt.core.dom.CastExpression)119 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)111 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)94 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)92 ASTNode (org.eclipse.jdt.core.dom.ASTNode)81 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)79 AST (org.eclipse.jdt.core.dom.AST)77 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)69 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)65 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)61 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)54 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)53 SimpleName (org.eclipse.jdt.core.dom.SimpleName)53 Type (org.eclipse.jdt.core.dom.Type)47 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)46 Block (org.eclipse.jdt.core.dom.Block)39 Statement (org.eclipse.jdt.core.dom.Statement)38