Search in sources :

Example 41 with Expression

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

the class AssignToVariableAssistProposal method doAddLocal.

private ASTRewrite doAddLocal() {
    Expression expression = ((ExpressionStatement) fNodeToAssign).getExpression();
    AST ast = fNodeToAssign.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
    String[] varNames = suggestLocalVariableNames(fTypeBinding, expression);
    for (int i = 0; i < varNames.length; i++) {
        addLinkedPositionProposal(KEY_NAME, varNames[i], null);
    }
    VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
    newDeclFrag.setName(ast.newSimpleName(varNames[0]));
    newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression));
    Type type = evaluateType(ast);
    if (ASTNodes.isControlStatementBody(fNodeToAssign.getLocationInParent())) {
        Block block = ast.newBlock();
        block.statements().add(rewrite.createMoveTarget(fNodeToAssign));
        rewrite.replace(fNodeToAssign, block, null);
    }
    if (needsSemicolon(expression)) {
        VariableDeclarationStatement varStatement = ast.newVariableDeclarationStatement(newDeclFrag);
        varStatement.setType(type);
        rewrite.replace(expression, varStatement, null);
    } else {
        // trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement
        VariableDeclarationExpression varExpression = ast.newVariableDeclarationExpression(newDeclFrag);
        varExpression.setType(type);
        rewrite.replace(expression, varExpression, null);
    }
    addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME);
    addLinkedPosition(rewrite.track(type), false, KEY_TYPE);
    // set cursor after expression statement
    setEndPosition(rewrite.track(fNodeToAssign));
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 42 with Expression

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

the class ASTNodes method getReceiverTypeBinding.

/**
	 * Returns the receiver's type binding of the given method invocation.
	 *
	 * @param invocation method invocation to resolve type of
	 * @return the type binding of the receiver
	 */
public static ITypeBinding getReceiverTypeBinding(MethodInvocation invocation) {
    ITypeBinding result = null;
    Expression exp = invocation.getExpression();
    if (exp != null) {
        return exp.resolveTypeBinding();
    } else {
        AbstractTypeDeclaration type = (AbstractTypeDeclaration) getParent(invocation, AbstractTypeDeclaration.class);
        if (type != null)
            return type.resolveBinding();
    }
    return result;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 43 with Expression

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

the class CodeFormatterUtil method format2.

/**
	 * Creates edits that describe how to format the given string.
	 * The given node is used to infer the kind to use to format the string.
	 * Consider to use {@link #format2(int, String, int, String, Map)} if the kind is already known.
	 * Returns <code>null</code> if the code could not be formatted for the given kind.
	 *
	 * @param node
	 *        Use to infer the kind of the code snippet to format.
	 * @param source
	 *        The source to format
	 * @param indentationLevel
	 *        The initial indentation level, used to shift left/right the entire source fragment.
	 *        An initial indentation level of zero or below has no effect.
	 * @param lineSeparator
	 *        The line separator to use in formatted source,
	 *        if set to <code>null</code>, then the platform default one will be used.
	 * @param options
	 *        The options map to use for formatting with the default code formatter.
	 *        Recognized options are documented on {@link JavaCore#getDefaultOptions()}.
	 *        If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}.
	 * @return an TextEdit describing the changes required to format source
	 * @throws IllegalArgumentException
	 *         If the offset and length are not inside the string, a IllegalArgumentException is thrown.
	 */
public static TextEdit format2(ASTNode node, String source, int indentationLevel, String lineSeparator, Map<String, String> options) {
    int code;
    //$NON-NLS-1$
    String prefix = "";
    //$NON-NLS-1$
    String suffix = "";
    if (node instanceof Statement) {
        code = CodeFormatter.K_STATEMENTS;
        if (node.getNodeType() == ASTNode.SWITCH_CASE) {
            //$NON-NLS-1$
            prefix = "switch(1) {";
            //$NON-NLS-1$
            suffix = "}";
            code = CodeFormatter.K_STATEMENTS;
        }
    } else if (node instanceof Expression && node.getNodeType() != ASTNode.VARIABLE_DECLARATION_EXPRESSION) {
        code = CodeFormatter.K_EXPRESSION;
    } else if (node instanceof BodyDeclaration) {
        code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
    } else {
        switch(node.getNodeType()) {
            case ASTNode.ARRAY_TYPE:
            case ASTNode.PARAMETERIZED_TYPE:
            case ASTNode.PRIMITIVE_TYPE:
            case ASTNode.QUALIFIED_TYPE:
            case ASTNode.SIMPLE_TYPE:
                //$NON-NLS-1$
                suffix = " x;";
                code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
                break;
            case ASTNode.WILDCARD_TYPE:
                //$NON-NLS-1$
                prefix = "A<";
                //$NON-NLS-1$
                suffix = "> x;";
                code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
                break;
            case ASTNode.COMPILATION_UNIT:
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
            case ASTNode.SINGLE_VARIABLE_DECLARATION:
                //$NON-NLS-1$
                suffix = ";";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
                //$NON-NLS-1$
                prefix = "A ";
                //$NON-NLS-1$
                suffix = ";";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.PACKAGE_DECLARATION:
            case ASTNode.IMPORT_DECLARATION:
                //$NON-NLS-1$
                suffix = "\nclass A {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.JAVADOC:
                //$NON-NLS-1$
                suffix = "void foo();";
                code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
                break;
            case ASTNode.CATCH_CLAUSE:
                //$NON-NLS-1$
                prefix = "try {}";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.ANONYMOUS_CLASS_DECLARATION:
                //$NON-NLS-1$
                prefix = "new A()";
                //$NON-NLS-1$
                suffix = ";";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.MEMBER_VALUE_PAIR:
                //$NON-NLS-1$
                prefix = "@Author(";
                //$NON-NLS-1$
                suffix = ") class x {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.MODIFIER:
                //$NON-NLS-1$
                suffix = " class x {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.TYPE_PARAMETER:
                //$NON-NLS-1$
                prefix = "class X<";
                //$NON-NLS-1$
                suffix = "> {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.MEMBER_REF:
            case ASTNode.METHOD_REF:
            case ASTNode.METHOD_REF_PARAMETER:
            case ASTNode.TAG_ELEMENT:
            case ASTNode.TEXT_ELEMENT:
                // Javadoc formatting not yet supported:
                return null;
            default:
                //Assert.isTrue(false, "Node type not covered: " + node.getClass().getName()); //$NON-NLS-1$
                return null;
        }
    }
    String concatStr = prefix + source + suffix;
    TextEdit edit = format2(code, concatStr, prefix.length(), source.length(), indentationLevel, lineSeparator, options);
    if (edit != null && prefix.length() > 0) {
        edit.moveTree(-prefix.length());
    }
    return edit;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) Statement(org.eclipse.jdt.core.dom.Statement) TextEdit(org.eclipse.text.edits.TextEdit) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 44 with Expression

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

the class NecessaryParenthesesChecker method isAllOperandsHaveSameType.

/*
	 * Do all operands in expression have same type
	 */
private static boolean isAllOperandsHaveSameType(InfixExpression expression, ITypeBinding leftOperandType, ITypeBinding rightOperandType) {
    ITypeBinding binding = leftOperandType;
    if (binding == null)
        return false;
    ITypeBinding current = rightOperandType;
    if (binding != current)
        return false;
    for (Iterator<Expression> iterator = expression.extendedOperands().iterator(); iterator.hasNext(); ) {
        Expression operand = iterator.next();
        current = operand.resolveTypeBinding();
        if (binding != current)
            return false;
    }
    return true;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 45 with Expression

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

the class AdvancedQuickAssistProcessor method getBooleanExpression.

private static Expression getBooleanExpression(ASTNode node) {
    if (!(node instanceof Expression)) {
        return null;
    }
    // check if the node is a location where it can be negated
    StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
    if (locationInParent == QualifiedName.NAME_PROPERTY) {
        node = node.getParent();
        locationInParent = node.getLocationInParent();
    }
    while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        node = node.getParent();
        locationInParent = node.getLocationInParent();
    }
    Expression expression = (Expression) node;
    if (!isBoolean(expression)) {
        return null;
    }
    if (expression.getParent() instanceof InfixExpression) {
        return expression;
    }
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY || locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
        return expression;
    }
    return null;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

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