use of org.eclipse.jdt.core.dom.InfixExpression in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final PrefixExpression node) {
final Expression operand = node.getOperand();
PrefixExpression.Operator _operator = node.getOperator();
boolean _matched = false;
if (Objects.equal(_operator, PrefixExpression.Operator.DECREMENT)) {
_matched = true;
}
if (!_matched) {
if (Objects.equal(_operator, PrefixExpression.Operator.INCREMENT)) {
_matched = true;
}
}
if (_matched) {
if ((operand instanceof ArrayAccess)) {
final String arrayName = this.computeArrayName(((ArrayAccess) operand));
StringConcatenation _builder = new StringConcatenation();
_builder.append("_tPreInx_");
_builder.append(arrayName);
final String idxName = _builder.toString();
String op = "-";
PrefixExpression.Operator _operator_1 = node.getOperator();
boolean _equals = Objects.equal(_operator_1, PrefixExpression.Operator.INCREMENT);
if (_equals) {
op = "+";
}
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("{val ");
_builder_1.append(idxName);
_builder_1.append("=");
this.appendToBuffer(_builder_1.toString());
((ArrayAccess) operand).getIndex().accept(this);
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(" ");
_builder_2.append("val ");
_builder_2.append(idxName, " ");
_builder_2.append("_res=");
_builder_2.append(arrayName, " ");
_builder_2.append(".get(");
_builder_2.append(idxName, " ");
_builder_2.append(")");
_builder_2.append(op, " ");
_builder_2.append("1");
this.appendToBuffer(_builder_2.toString());
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append(" ");
_builder_3.append(arrayName, " ");
_builder_3.append(".set(");
_builder_3.append(idxName, " ");
_builder_3.append(", ");
_builder_3.append(idxName, " ");
_builder_3.append("_res) ");
_builder_3.append(idxName, " ");
_builder_3.append("_res}");
this.appendToBuffer(_builder_3.toString());
return false;
} else {
final AST dummyAST = AST.newAST(node.getAST().apiLevel());
final Assignment assigment = dummyAST.newAssignment();
final InfixExpression infixOp = dummyAST.newInfixExpression();
ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, operand);
infixOp.setLeftOperand(((Expression) _copySubtree));
PrefixExpression.Operator _operator_2 = node.getOperator();
boolean _equals_1 = Objects.equal(_operator_2, PrefixExpression.Operator.DECREMENT);
if (_equals_1) {
infixOp.setOperator(InfixExpression.Operator.MINUS);
} else {
infixOp.setOperator(InfixExpression.Operator.PLUS);
}
infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, operand);
final Expression leftSide = ((Expression) _copySubtree_1);
assigment.setLeftHandSide(leftSide);
assigment.setRightHandSide(infixOp);
this.appendToBuffer("{");
Type type = null;
if ((operand instanceof SimpleName)) {
type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) operand));
}
this.handleAssignment(assigment, leftSide, type);
this.appendToBuffer("}");
return false;
}
}
if (!_matched) {
if (Objects.equal(_operator, PrefixExpression.Operator.COMPLEMENT)) {
_matched = true;
node.getOperand().accept(this);
this.appendToBuffer(".bitwiseNot");
}
}
if (!_matched) {
{
this.appendToBuffer(node.getOperator().toString());
node.getOperand().accept(this);
}
}
return false;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final InfixExpression node) {
final boolean useRichString = this._aSTFlattenerUtils.canConvertToRichText(node);
if (useRichString) {
ASTNode _parent = node.getParent();
final boolean firstEntrance = (!(_parent instanceof InfixExpression));
if (firstEntrance) {
this.appendToBuffer("\'\'\'");
}
this.appendAsRichString(node.getLeftOperand());
this.appendAsRichString(node.getRightOperand());
final Function2<Expression, Expression, Expression> _function = (Expression prevExpr, Expression currExpr) -> {
this.appendAsRichString(currExpr);
return currExpr;
};
IterableExtensions.<Expression, Expression>fold(node.extendedOperands(), node.getRightOperand(), _function);
if (firstEntrance) {
this.appendToBuffer("\'\'\'");
if (this.fallBackStrategy) {
this.appendToBuffer(".toString");
}
}
} else {
node.getLeftOperand().accept(this);
final InfixExpression.Operator operator = node.getOperator();
this.handleInfixRightSide(node, operator, node.getRightOperand());
final List extendedOperands = node.extendedOperands();
int _size = extendedOperands.size();
boolean _notEquals = (_size != 0);
if (_notEquals) {
final Consumer<Expression> _function_1 = (Expression e) -> {
this.handleInfixRightSide(node, operator, e);
};
extendedOperands.forEach(_function_1);
}
}
return false;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method extractVariableAndValuesFromInfixExpression.
private Variable extractVariableAndValuesFromInfixExpression(InfixExpression infixExpr) {
final Operator op = infixExpr.getOperator();
final Expression leftOp = infixExpr.getLeftOperand();
final Expression rightOp = infixExpr.getRightOperand();
if (extendedOperands(infixExpr).isEmpty() && (CONDITIONAL_OR.equals(op) || OR.equals(op) || XOR.equals(op))) {
final Variable leftVar = extractVariableAndValues(leftOp);
final Variable rightVar = extractVariableAndValues(rightOp);
if (leftVar != null && leftVar.isSameVariable(rightVar)) {
return leftVar.mergeValues(rightVar);
}
} else if (EQUALS.equals(op)) {
Variable variable = extractVariableWithConstantValue(leftOp, rightOp);
return variable != null ? variable : extractVariableWithConstantValue(rightOp, leftOp);
}
return null;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
final Expression lhs = node.getLeftOperand();
final Expression rhs = node.getRightOperand();
if (hasOperator(node, CONDITIONAL_OR)) {
final List<Expression> remainingOperands = removeUselessOperands(node, true, false);
if (!remainingOperands.equals(allOperands(node))) {
return replaceWithNewInfixExpr(node, remainingOperands);
}
} else if (hasOperator(node, CONDITIONAL_AND)) {
final List<Expression> remainingOperands = removeUselessOperands(node, false, true);
if (!remainingOperands.equals(allOperands(node))) {
return replaceWithNewInfixExpr(node, remainingOperands);
} else {
// FIXME this should actually check anywhere in the infix expression,
// not only for left and right operands,
// said otherwise: handle extended operands
final Expression nullCheckedExpressionLHS = getNullCheckedExpression(lhs);
final Expression nullCheckedExpressionRHS = getNullCheckedExpression(rhs);
if (nullCheckedExpressionLHS != null) {
if (isNullCheckRedundant(rhs, nullCheckedExpressionLHS)) {
checkNoExtendedOperands(node);
return replaceBy(node, rhs);
}
} else if (isNullCheckRedundant(lhs, nullCheckedExpressionRHS)) {
return replaceBy(node, lhs);
}
}
} else if ((hasOperator(node, EQUALS) || hasOperator(node, NOT_EQUALS) || hasOperator(node, XOR)) && !node.hasExtendedOperands()) {
if (maybeReduceBooleanExpression(node, lhs, rhs) == DO_NOT_VISIT_SUBTREE) {
return DO_NOT_VISIT_SUBTREE;
}
}
if (shouldHaveParentheses(node)) {
addParentheses(node);
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method getExpressionWithoutParentheses.
private Expression getExpressionWithoutParentheses(ParenthesizedExpression node) {
final ASTNode parent = node.getParent();
final Expression innerExpr = node.getExpression();
if (innerExpr instanceof ParenthesizedExpression) {
return innerExpr;
}
if (parent instanceof InfixExpression) {
final InfixExpression parentInfixExpr = (InfixExpression) parent;
if (innerExpr instanceof InfixExpression) {
final Operator innerOp = ((InfixExpression) innerExpr).getOperator();
if (innerOp == parentInfixExpr.getOperator() && OperatorEnum.isAssociative(innerOp) && // to other if statements in this method.
equalNotNull(innerExpr.resolveTypeBinding(), parentInfixExpr.resolveTypeBinding())) {
return innerExpr;
}
}
}
// Infix, prefix or postfix without parenthesis is not readable
if ((parent instanceof InfixExpression && (InfixExpression.Operator.PLUS.equals(((InfixExpression) parent).getOperator()) || InfixExpression.Operator.MINUS.equals(((InfixExpression) parent).getOperator()))) || (parent instanceof PrefixExpression && (PLUS.equals(((PrefixExpression) parent).getOperator()) || MINUS.equals(((PrefixExpression) parent).getOperator())))) {
if (innerExpr instanceof PrefixExpression && (DECREMENT.equals(((PrefixExpression) innerExpr).getOperator()) || INCREMENT.equals(((PrefixExpression) innerExpr).getOperator()) || PLUS.equals(((PrefixExpression) innerExpr).getOperator()) || MINUS.equals(((PrefixExpression) innerExpr).getOperator()))) {
return node;
}
if (innerExpr instanceof PostfixExpression && (PostfixExpression.Operator.DECREMENT.equals(((PostfixExpression) innerExpr).getOperator()) || PostfixExpression.Operator.INCREMENT.equals(((PostfixExpression) innerExpr).getOperator()))) {
return node;
}
}
if (isInnerExprHardToRead(innerExpr, parent)) {
// return (bla != null) ? bla.getSomething() : null;
return node;
}
if (isUselessParenthesesInStatement(parent, node)) {
return innerExpr;
}
final int compareTo = OperatorEnum.compareTo(innerExpr, parent);
if (compareTo < 0) {
return node;
} else if (compareTo > 0) {
return innerExpr;
}
if (// some like it like that
innerExpr instanceof InfixExpression || // or if it can be removed.
innerExpr instanceof CastExpression || // infix and prefix or postfix without parenthesis is not readable
((parent instanceof InfixExpression || parent instanceof PrefixExpression || parent instanceof PostfixExpression) && (innerExpr instanceof PrefixExpression || innerExpr instanceof PostfixExpression))) {
return node;
}
return innerExpr;
}
Aggregations