Search in sources :

Example 1 with CastExpression

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

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

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

the class InlineTempRefactoring method getModifiedInitializerSource.

private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
    VariableDeclaration varDecl = getVariableDeclaration();
    Expression initializer = varDecl.getInitializer();
    ASTNode referenceContext = reference.getParent();
    if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
        if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
            ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
            if (typeArguments != null) {
                String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
                return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
            }
        }
    }
    Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
    AST ast = rewrite.getAST();
    if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
        ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
        parenthesized.setExpression(copy);
        copy = parenthesized;
    }
    ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
    if (explicitCast != null) {
        CastExpression cast = ast.newCastExpression();
        if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
            ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
            parenthesized.setExpression(copy);
            copy = parenthesized;
        }
        cast.setExpression(copy);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
        cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
        copy = cast;
    } else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
        ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
        ArrayCreation newArrayCreation = ast.newArrayCreation();
        newArrayCreation.setType(newType);
        newArrayCreation.setInitializer((ArrayInitializer) copy);
        return newArrayCreation;
    }
    return copy;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayType(org.eclipse.jdt.core.dom.ArrayType) 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) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 4 with CastExpression

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

the class ExtractTempRefactoring method createTempType.

private Type createTempType() throws CoreException {
    Expression expression = getSelectedExpression().getAssociatedExpression();
    Type resultingType = null;
    ITypeBinding typeBinding = expression.resolveTypeBinding();
    ASTRewrite rewrite = fCURewrite.getASTRewrite();
    AST ast = rewrite.getAST();
    if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
        resultingType = (Type) rewrite.createCopyTarget(((ClassInstanceCreation) expression).getType());
    } else if (expression instanceof CastExpression) {
        resultingType = (Type) rewrite.createCopyTarget(((CastExpression) expression).getType());
    } else {
        if (typeBinding == null) {
            typeBinding = ASTResolving.guessBindingForReference(expression);
        }
        if (typeBinding != null) {
            typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
            ImportRewrite importRewrite = fCURewrite.getImportRewrite();
            ImportRewriteContext context = new ContextSensitiveImportRewriteContext(expression, importRewrite);
            resultingType = importRewrite.addImport(typeBinding, ast, context);
        } else {
            //$NON-NLS-1$
            resultingType = ast.newSimpleType(ast.newSimpleName("Object"));
        }
    }
    if (fLinkedProposalModel != null) {
        LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
        typeGroup.addPosition(rewrite.track(resultingType), false);
        if (typeBinding != null) {
            ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
            for (int i = 0; i < relaxingTypes.length; i++) {
                typeGroup.addProposal(relaxingTypes[i], fCURewrite.getCu(), relaxingTypes.length - i);
            }
        }
    }
    return resultingType;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Type(org.eclipse.jdt.core.dom.Type) 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) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 5 with CastExpression

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

CastExpression (org.eclipse.jdt.core.dom.CastExpression)24 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)18 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 Expression (org.eclipse.jdt.core.dom.Expression)16 AST (org.eclipse.jdt.core.dom.AST)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 Type (org.eclipse.jdt.core.dom.Type)10 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 Assignment (org.eclipse.jdt.core.dom.Assignment)6 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)6 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)6 IBinding (org.eclipse.jdt.core.dom.IBinding)4