use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.
the class CastCorrectionProposal method getRewrite.
@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;
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.
the class CastCorrectionProposal method getNewCastTypeNode.
private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) {
AST ast = rewrite.getAST();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite);
if (fCastType != null) {
return importRewrite.addImport(fCastType, ast, context);
}
ASTNode node = fNodeToCast;
ASTNode parent = node.getParent();
if (parent instanceof CastExpression) {
node = parent;
parent = parent.getParent();
}
while (parent instanceof ParenthesizedExpression) {
node = parent;
parent = parent.getParent();
}
if (parent instanceof MethodInvocation) {
MethodInvocation invocation = (MethodInvocation) node.getParent();
if (invocation.getExpression() == node) {
IBinding targetContext = ASTResolving.getParentMethodOrTypeBinding(node);
ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext);
if (bindings.length > 0) {
ITypeBinding first = getCastFavorite(bindings, fNodeToCast.resolveTypeBinding());
Type newTypeNode = importRewrite.addImport(first, ast, context);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(newTypeNode), true, "casttype");
for (int i = 0; i < bindings.length; i++) {
//$NON-NLS-1$
addLinkedPositionProposal("casttype", bindings[i]);
}
return newTypeNode;
}
}
}
//$NON-NLS-1$
Type newCastType = ast.newSimpleType(ast.newSimpleName("Object"));
//$NON-NLS-1$
addLinkedPosition(rewrite.track(newCastType), true, "casttype");
return newCastType;
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project AutoRefactor by JnRouvignac.
the class ASTBuilder method parenthesize.
/**
* Builds a new {@link ParenthesizedExpression} instance.
*
* @param expression the expression to wrap with parentheses
* @return a new parenthesized expression
*/
public ParenthesizedExpression parenthesize(Expression expression) {
final ParenthesizedExpression pe = ast.newParenthesizedExpression();
pe.setExpression(expression);
return pe;
}
Aggregations