Search in sources :

Example 1 with CastCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.CastCorrectionProposal in project che by eclipse.

the class TypeMismatchSubProcessor method createCastProposal.

public static ASTRewriteCorrectionProposal createCastProposal(IInvocationContext context, ITypeBinding castTypeBinding, Expression nodeToCast, int relevance) {
    ICompilationUnit cu = context.getCompilationUnit();
    String label;
    String castType = BindingLabelProvider.getBindingLabel(castTypeBinding, JavaElementLabels.ALL_DEFAULT);
    if (nodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) {
        label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changecast_description, castType);
    } else {
        label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_addcast_description, castType);
    }
    return new CastCorrectionProposal(label, cu, nodeToCast, castTypeBinding, relevance);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CastCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CastCorrectionProposal)

Example 2 with CastCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.CastCorrectionProposal 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)

Aggregations

CastCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CastCorrectionProposal)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 Name (org.eclipse.jdt.core.dom.Name)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1