Search in sources :

Example 6 with ParenthesizedExpression

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

the class GetterSetterUtil method createNarrowCastIfNessecary.

/**
	 * Checks if the assignment needs a downcast and inserts it if necessary
	 *
	 * @param expression the right hand-side
	 * @param expressionType the type of the right hand-side. Can be null
	 * @param ast the AST
	 * @param variableType the Type of the variable the expression will be assigned to
	 * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
	 * @return the casted expression if necessary
	 */
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
    PrimitiveType castTo = null;
    if (variableType.isEqualTo(expressionType))
        //no cast for same type
        return expression;
    if (is50OrHigher) {
        if (//$NON-NLS-1$
        ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType))
            castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
        if (//$NON-NLS-1$
        ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType))
            castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
        if (//$NON-NLS-1$
        ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType))
            castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
    }
    if (//$NON-NLS-1$
    ast.resolveWellKnownType("char").isEqualTo(variableType))
        castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
    if (//$NON-NLS-1$
    ast.resolveWellKnownType("byte").isEqualTo(variableType))
        castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
    if (//$NON-NLS-1$
    ast.resolveWellKnownType("short").isEqualTo(variableType))
        castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
    if (castTo != null) {
        CastExpression cast = ast.newCastExpression();
        if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
            ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
            parenthesized.setExpression(expression);
            cast.setExpression(parenthesized);
        } else
            cast.setExpression(expression);
        cast.setType(castTo);
        return cast;
    }
    return expression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 7 with ParenthesizedExpression

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

the class GetterSetterUtil method getAssignedValue.

/**
	 * Converts an assignment, postfix expression or prefix expression into an assignable equivalent expression using the getter.
	 *
	 * @param node the assignment/prefix/postfix node
	 * @param astRewrite the astRewrite to use
	 * @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist
	 * @param variableType the type of the variable that the result will be assigned to
	 * @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used
	 * @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter
	 */
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) {
    InfixExpression.Operator op = null;
    AST ast = astRewrite.getAST();
    if (isNotInBlock(node))
        return null;
    if (node.getNodeType() == ASTNode.ASSIGNMENT) {
        Assignment assignment = ((Assignment) node);
        Expression rightHandSide = assignment.getRightHandSide();
        Expression copiedRightOp = (Expression) astRewrite.createCopyTarget(rightHandSide);
        if (assignment.getOperator() == Operator.ASSIGN) {
            ITypeBinding rightHandSideType = rightHandSide.resolveTypeBinding();
            copiedRightOp = createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
            return copiedRightOp;
        }
        if (getterExpression != null) {
            InfixExpression infix = ast.newInfixExpression();
            infix.setLeftOperand(getterExpression);
            infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
            ITypeBinding infixType = infix.resolveTypeBinding();
            if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) {
                ParenthesizedExpression p = ast.newParenthesizedExpression();
                p.setExpression(copiedRightOp);
                copiedRightOp = p;
            }
            infix.setRightOperand(copiedRightOp);
            return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
        }
    } else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
        PostfixExpression po = (PostfixExpression) node;
        if (po.getOperator() == PostfixExpression.Operator.INCREMENT)
            op = InfixExpression.Operator.PLUS;
        if (po.getOperator() == PostfixExpression.Operator.DECREMENT)
            op = InfixExpression.Operator.MINUS;
    } else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
        PrefixExpression pe = (PrefixExpression) node;
        if (pe.getOperator() == PrefixExpression.Operator.INCREMENT)
            op = InfixExpression.Operator.PLUS;
        if (pe.getOperator() == PrefixExpression.Operator.DECREMENT)
            op = InfixExpression.Operator.MINUS;
    }
    if (op != null && getterExpression != null) {
        return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher);
    }
    return null;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Example 8 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression 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 9 with ParenthesizedExpression

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

the class ASTNodes method getTargetType.

/**
	 * Derives the target type defined at the location of the given expression if the target context
	 * supports poly expressions.
	 * 
	 * @param expression the expression at whose location the target type is required
	 * @return the type binding of the target type defined at the location of the given expression
	 *         if the target context supports poly expressions, or <code>null</code> if the target
	 *         type could not be derived
	 * 
	 * @since 3.10
	 */
public static ITypeBinding getTargetType(Expression expression) {
    ASTNode parent = expression.getParent();
    StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
        return ((VariableDeclaration) parent).getName().resolveTypeBinding();
    } else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        return getTargetTypeForReturnStmt((ReturnStatement) parent);
    } else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
        return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
    } else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) parent;
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        if (methodBinding != null) {
            return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
        }
    } else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
        SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
        IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
        if (superMethodBinding != null) {
            return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
        }
    } else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
        ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
        IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
        if (constructorBinding != null) {
            return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
        }
    } else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
        SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
        IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
        if (superConstructorBinding != null) {
            return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
        }
    } else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
        ClassInstanceCreation creation = (ClassInstanceCreation) parent;
        IMethodBinding creationBinding = creation.resolveConstructorBinding();
        if (creationBinding != null) {
            return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
        }
    } else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
        EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
        IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
        if (enumConstructorBinding != null) {
            return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
        }
    } else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
        IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
        if (methodBinding != null) {
            return methodBinding.getReturnType();
        }
    } else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        return getTargetType((ConditionalExpression) parent);
    } else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
        return ((CastExpression) parent).getType().resolveBinding();
    } else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return getTargetType((ParenthesizedExpression) parent);
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 10 with ParenthesizedExpression

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

the class InferTypeArgumentsRefactoring method rewriteCastVariable.

private static ASTNode rewriteCastVariable(CastVariable2 castCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel) {
    //, List positionGroups) {
    ASTNode node = castCv.getRange().getNode(rewrite.getRoot());
    ConstraintVariable2 expressionVariable = castCv.getExpressionVariable();
    ConstraintVariable2 methodReceiverCv = tCModel.getMethodReceiverCv(expressionVariable);
    if (methodReceiverCv != null) {
        TType chosenReceiverType = InferTypeArgumentsConstraintsSolver.getChosenType(methodReceiverCv);
        if (chosenReceiverType == null)
            return null;
        else if (!InferTypeArgumentsTCModel.isAGenericType(chosenReceiverType))
            return null;
        else if (hasUnboundElement(methodReceiverCv, tCModel))
            return null;
    }
    CastExpression castExpression = (CastExpression) node;
    Expression expression = castExpression.getExpression();
    ASTNode nodeToReplace;
    if (castExpression.getParent() instanceof ParenthesizedExpression)
        nodeToReplace = castExpression.getParent();
    else
        nodeToReplace = castExpression;
    Expression newExpression = (Expression) rewrite.getASTRewrite().createMoveTarget(expression);
    rewrite.getASTRewrite().replace(nodeToReplace, newExpression, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_removeCast));
    rewrite.getImportRemover().registerRemovedNode(nodeToReplace);
    return newExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Aggregations

ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 Expression (org.eclipse.jdt.core.dom.Expression)18 AST (org.eclipse.jdt.core.dom.AST)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)13 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)12 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7 Assignment (org.eclipse.jdt.core.dom.Assignment)6 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)4 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3