use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.
private static boolean isReferringToLocalVariableFromFor(Expression expression) {
ASTNode current = expression;
ASTNode parent = current.getParent();
while (parent != null && !(parent instanceof BodyDeclaration)) {
if (parent instanceof ForStatement) {
ForStatement forStmt = (ForStatement) parent;
if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
List<Expression> initializers = forStmt.initializers();
if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
expression.accept(checker);
if (checker.isReferringToForVariable())
return true;
}
}
}
current = parent;
parent = current.getParent();
}
return false;
}
use of org.eclipse.jdt.core.dom.Expression in project AutoRefactor by JnRouvignac.
the class PrimitiveWrapperCreationRefactoring method replaceFloatInstanceWithValueOf.
private boolean replaceFloatInstanceWithValueOf(ClassInstanceCreation node, final ITypeBinding typeBinding, final List<Expression> args) {
final Expression arg0 = args.get(0);
if (isPrimitive(arg0, "double")) {
final ASTBuilder b = ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.invoke(typeBinding.getName(), "valueOf", b.cast(b.type("float"), b.copy(arg0))));
return DO_NOT_VISIT_SUBTREE;
} else if (hasType(arg0, "java.lang.Double")) {
final ASTBuilder b = ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.invoke(b.copy(arg0), "floatValue"));
return DO_NOT_VISIT_SUBTREE;
} else {
replaceWithValueOf(node, typeBinding);
return DO_NOT_VISIT_SUBTREE;
}
}
use of org.eclipse.jdt.core.dom.Expression in project AutoRefactor by JnRouvignac.
the class BooleanEqualsRatherThanNullCheckRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if (hasOperator(node, CONDITIONAL_AND) || hasOperator(node, CONDITIONAL_OR)) {
final Expression leftOperand = node.getLeftOperand();
final Expression rightOperand = node.getRightOperand();
final InfixExpression condition = as(leftOperand, InfixExpression.class);
final boolean isNullCheck = hasOperator(condition, EQUALS);
final boolean isAndExpr = hasOperator(node, CONDITIONAL_AND);
if (!node.hasExtendedOperands() && (isNullCheck ^ isAndExpr) && condition != null && (hasOperator(condition, EQUALS) || hasOperator(condition, NOT_EQUALS))) {
Expression firstExpr = null;
if (isNullLiteral(condition.getLeftOperand())) {
firstExpr = condition.getRightOperand();
} else if (isNullLiteral(condition.getRightOperand())) {
firstExpr = condition.getLeftOperand();
}
Expression secondExpr = null;
final PrefixExpression negateSecondExpr = as(rightOperand, PrefixExpression.class);
final boolean isPositiveExpr;
if (negateSecondExpr != null && hasOperator(negateSecondExpr, NOT)) {
secondExpr = negateSecondExpr.getOperand();
isPositiveExpr = false;
} else {
secondExpr = rightOperand;
isPositiveExpr = true;
}
if (firstExpr != null && hasType(firstExpr, "java.lang.Boolean") && isPassive(firstExpr) && match(new ASTMatcher(), firstExpr, secondExpr)) {
replaceNullCheck(node, firstExpr, isNullCheck, isAndExpr, isPositiveExpr);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Expression in project AutoRefactor by JnRouvignac.
the class BooleanEqualsRatherThanNullCheckRefactoring method replaceNullCheck.
private void replaceNullCheck(final InfixExpression node, final Expression firstExpr, final boolean isNullCheck, final boolean isAndExpr, final boolean isPositiveExpr) {
final ASTBuilder b = ctx.getASTBuilder();
final Name booleanConstant = b.name("Boolean", isAndExpr == isPositiveExpr ? "TRUE" : "FALSE");
final MethodInvocation equalsMethod = b.invoke(booleanConstant, "equals", b.copy(firstExpr));
Expression newExpr = null;
if (!isNullCheck || isAndExpr) {
newExpr = equalsMethod;
} else {
newExpr = b.not(equalsMethod);
}
ctx.getRefactorings().replace(node, newExpr);
}
use of org.eclipse.jdt.core.dom.Expression in project AutoRefactor by JnRouvignac.
the class BigDecimalRefactoring method visit.
@Override
public boolean visit(MethodInvocation node) {
if (node.getExpression() == null) {
return VISIT_SUBTREE;
}
if (getJavaMinorVersion() >= 5 && (isMethod(node, "java.math.BigDecimal", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "double"))) {
final ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
final Expression arg0 = arg0(node);
if (arg0 instanceof NumberLiteral) {
final NumberLiteral nb = (NumberLiteral) arg0;
if (nb.getToken().contains(".")) {
this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), nb.getToken()));
} else if (ZERO_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
replaceWithQualifiedName(node, typeBinding, "ZERO");
} else if (ONE_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
replaceWithQualifiedName(node, typeBinding, "ONE");
} else if (TEN_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
replaceWithQualifiedName(node, typeBinding, "TEN");
} else {
return VISIT_SUBTREE;
}
return DO_NOT_VISIT_SUBTREE;
}
} else if (!(node.getParent() instanceof PrefixExpression) || !hasOperator((PrefixExpression) node.getParent(), NOT)) {
return maybeReplaceEquals(true, node, node);
}
return VISIT_SUBTREE;
}
Aggregations