/** * Creates a cast correction proposal. * * @param label the display name of the proposal * @param targetCU the compilation unit that is modified * @param nodeToCast the node to cast * @param castType the type to cast to, may be <code>null</code> * @param relevance the relevance of this proposal */ public CastCorrectionProposal(String label, ICompilationUnit targetCU, Expression nodeToCast, ITypeBinding castType, int relevance) { super(label, targetCU, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST)); fNodeToCast= nodeToCast; fCastType= castType; setCommandId(ADD_CAST_ID); }
@Override protected ASTRewrite getRewrite() throws CoreException { AST ast= fNodeToCast.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ImportRewrite importRewrite= createImportRewrite((CompilationUnit) fNodeToCast.getRoot()); Type newTypeNode= getNewCastTypeNode(rewrite, importRewrite); if (fNodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) { CastExpression expression= (CastExpression) fNodeToCast; rewrite.replace(expression.getType(), newTypeNode, null); } else { Expression expressionCopy= (Expression) rewrite.createCopyTarget(fNodeToCast); if (needsInnerParantheses(fNodeToCast)) { ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(expressionCopy); expressionCopy= parenthesizedExpression; } CastExpression castExpression= ast.newCastExpression(); castExpression.setExpression(expressionCopy); castExpression.setType(newTypeNode); ASTNode replacingNode= castExpression; if (needsOuterParantheses(fNodeToCast)) { ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(castExpression); replacingNode= parenthesizedExpression; } rewrite.replace(fNodeToCast, replacingNode, null); } return rewrite; }
ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext); if (bindings.length > 0) { ITypeBinding first= getCastFavorite(bindings, fNodeToCast.resolveTypeBinding()); addLinkedPosition(rewrite.track(newTypeNode), true, "casttype"); //$NON-NLS-1$ for (int i= 0; i < bindings.length; i++) { addLinkedPositionProposal("casttype", bindings[i]); //$NON-NLS-1$ addLinkedPosition(rewrite.track(newCastType), true, "casttype"); //$NON-NLS-1$ return newCastType;
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); }
@Override protected ASTRewrite getRewrite() throws CoreException { AST ast= fNodeToCast.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ImportRewrite importRewrite= createImportRewrite((CompilationUnit) fNodeToCast.getRoot()); Type newTypeNode= getNewCastTypeNode(rewrite, importRewrite); if (fNodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) { CastExpression expression= (CastExpression) fNodeToCast; rewrite.replace(expression.getType(), newTypeNode, null); } else { Expression expressionCopy= (Expression) rewrite.createCopyTarget(fNodeToCast); if (needsInnerParantheses(fNodeToCast)) { ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(expressionCopy); expressionCopy= parenthesizedExpression; } CastExpression castExpression= ast.newCastExpression(); castExpression.setExpression(expressionCopy); castExpression.setType(newTypeNode); ASTNode replacingNode= castExpression; if (needsOuterParantheses(fNodeToCast)) { ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression(castExpression); replacingNode= parenthesizedExpression; } rewrite.replace(fNodeToCast, replacingNode, null); } return rewrite; }
ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext); if (bindings.length > 0) { ITypeBinding first= getCastFavorite(bindings, fNodeToCast.resolveTypeBinding()); addLinkedPosition(rewrite.track(newTypeNode), true, "casttype"); //$NON-NLS-1$ for (int i= 0; i < bindings.length; i++) { addLinkedPositionProposal("casttype", bindings[i]); //$NON-NLS-1$ addLinkedPosition(rewrite.track(newCastType), true, "casttype"); //$NON-NLS-1$ return newCastType;
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); }
proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));
/** * Creates a cast correction proposal. * * @param label the display name of the proposal * @param targetCU the compilation unit that is modified * @param nodeToCast the node to cast * @param castType the type to cast to, may be <code>null</code> * @param relevance the relevance of this proposal */ public CastCorrectionProposal(String label, ICompilationUnit targetCU, Expression nodeToCast, ITypeBinding castType, int relevance) { super(label, targetCU, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST)); fNodeToCast= nodeToCast; fCastType= castType; setCommandId(ADD_CAST_ID); }
proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));