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 che by eclipse.
the class AccessAnalyzer method visit.
@Override
public boolean visit(Assignment node) {
Expression leftHandSide = node.getLeftHandSide();
if (!considerBinding(resolveBinding(leftHandSide), leftHandSide))
return true;
checkParent(node);
Expression rightHandSide = node.getRightHandSide();
if (!fIsFieldFinal) {
// Write access.
AST ast = node.getAST();
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(fSetter));
fReferencingSetter = true;
Expression receiver = getReceiver(leftHandSide);
if (receiver != null)
invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
List<Expression> arguments = invocation.arguments();
if (node.getOperator() == Assignment.Operator.ASSIGN) {
arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide));
} else {
// This is the compound assignment case: field+= 10;
InfixExpression exp = ast.newInfixExpression();
exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
MethodInvocation getter = ast.newMethodInvocation();
getter.setName(ast.newSimpleName(fGetter));
fReferencingGetter = true;
if (receiver != null)
getter.setExpression((Expression) fRewriter.createCopyTarget(receiver));
exp.setLeftOperand(getter);
Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) {
ParenthesizedExpression p = ast.newParenthesizedExpression();
p.setExpression(rhs);
rhs = p;
}
exp.setRightOperand(rhs);
arguments.add(exp);
}
fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
}
rightHandSide.accept(this);
return false;
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.
the class ExpressionsFix method createRemoveUnnecessaryParenthesisFix.
public static ExpressionsFix createRemoveUnnecessaryParenthesisFix(CompilationUnit compilationUnit, ASTNode[] nodes) {
// check sub-expressions in fully covered nodes
final ArrayList<ParenthesizedExpression> changedNodes = new ArrayList<ParenthesizedExpression>();
for (int i = 0; i < nodes.length; i++) {
ASTNode covered = nodes[i];
if (covered instanceof ParenthesizedExpression || covered instanceof InfixExpression)
covered.accept(new UnnecessaryParenthesisVisitor(changedNodes));
}
if (changedNodes.isEmpty())
return null;
HashSet<ParenthesizedExpression> expressions = new HashSet<ParenthesizedExpression>(changedNodes);
RemoveParenthesisOperation op = new RemoveParenthesisOperation(expressions);
return new ExpressionsFix(FixMessages.ExpressionsFix_removeUnnecessaryParentheses_description, compilationUnit, new CompilationUnitRewriteOperation[] { op });
}
use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.
the class UnusedCodeFix method replaceCast.
private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) {
boolean castEnclosedInNecessaryParentheses = castExpression.getParent() instanceof ParenthesizedExpression && NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent());
ASTNode toReplace = castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression;
ASTNode move;
if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
if (replacement.getParent() instanceof ParenthesizedExpression) {
move = rewrite.createMoveTarget(replacement.getParent());
} else if (castEnclosedInNecessaryParentheses) {
toReplace = castExpression;
move = rewrite.createMoveTarget(replacement);
} else {
ParenthesizedExpression parentheses = replacement.getAST().newParenthesizedExpression();
parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement));
move = parentheses;
}
} else {
move = rewrite.createMoveTarget(replacement);
}
rewrite.replace(toReplace, move, group);
}
Aggregations