use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method combineOperands.
private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) {
if (existing == null && removeParentheses) {
while (originalNode instanceof ParenthesizedExpression) {
originalNode = ((ParenthesizedExpression) originalNode).getExpression();
}
}
Expression newRight = (Expression) rewrite.createMoveTarget(originalNode);
if (originalNode instanceof InfixExpression) {
((InfixExpression) newRight).setOperator(((InfixExpression) originalNode).getOperator());
}
if (existing == null) {
return newRight;
}
AST ast = rewrite.getAST();
InfixExpression infix = ast.newInfixExpression();
infix.setOperator(operator);
infix.setLeftOperand(existing);
infix.setRightOperand(newRight);
return infix;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getCombineStringProposals.
private static boolean getCombineStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
// we work with InfixExpressions
InfixExpression infixExpression;
if (node instanceof InfixExpression) {
infixExpression = (InfixExpression) node;
} else if (node.getParent() instanceof InfixExpression) {
infixExpression = (InfixExpression) node.getParent();
} else {
return false;
}
// only + is valid for combining strings
if (!(infixExpression.getOperator().equals(InfixExpression.Operator.PLUS))) {
return false;
}
// all expressions must be strings
Expression leftOperand = infixExpression.getLeftOperand();
Expression rightOperand = infixExpression.getRightOperand();
if (!(leftOperand instanceof StringLiteral && rightOperand instanceof StringLiteral)) {
return false;
}
StringLiteral leftString = (StringLiteral) leftOperand;
StringLiteral rightString = (StringLiteral) rightOperand;
if (resultingCollections == null) {
return true;
}
// begin building combined string
StringBuilder stringBuilder = new StringBuilder(leftString.getLiteralValue());
stringBuilder.append(rightString.getLiteralValue());
// append extended string literals
for (Object operand : infixExpression.extendedOperands()) {
if (!(operand instanceof StringLiteral))
return false;
StringLiteral stringLiteral = (StringLiteral) operand;
stringBuilder.append(stringLiteral.getLiteralValue());
}
// prepare new string literal
AST ast = node.getAST();
StringLiteral combinedStringLiteral = ast.newStringLiteral();
combinedStringLiteral.setLiteralValue(stringBuilder.toString());
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(infixExpression, combinedStringLiteral, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_combineSelectedStrings;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.COMBINE_STRINGS);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class NecessaryParenthesesChecker method needsParenthesesInInfixExpression.
private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
InfixExpression.Operator parentInfixOperator = parentInfix.getOperator();
ITypeBinding rightOperandType;
ITypeBinding parentInfixExprType;
if (leftOperandType == null) {
// parentInfix has bindings
leftOperandType = parentInfix.getLeftOperand().resolveTypeBinding();
rightOperandType = parentInfix.getRightOperand().resolveTypeBinding();
parentInfixExprType = parentInfix.resolveTypeBinding();
} else {
rightOperandType = expression.resolveTypeBinding();
parentInfixExprType = getInfixExpressionType(parentInfixOperator, leftOperandType, rightOperandType);
}
boolean isAllOperandsHaveSameType = isAllOperandsHaveSameType(parentInfix, leftOperandType, rightOperandType);
if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
// infix expressions are evaluated from left to right -> parentheses not needed
return false;
} else if (isAssociative(parentInfixOperator, parentInfixExprType, isAllOperandsHaveSameType)) {
// left op (right) == (right) op left == right op left
if (expression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) expression;
Operator operator = infixExpression.getOperator();
if (isStringType(parentInfixExprType)) {
if (parentInfixOperator == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isStringType(infixExpression.resolveTypeBinding())) {
// "" + (2 + "") == "" + 2 + ""
return !isStringType(infixExpression.getLeftOperand().resolveTypeBinding()) && !isStringType(leftOperandType);
}
// "" + (1 + 2), "" + (1 - 2) etc
return true;
}
if (parentInfixOperator != InfixExpression.Operator.TIMES)
return false;
if (operator == InfixExpression.Operator.TIMES)
// x * (y * z) == x * y * z
return false;
if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
// x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
return true;
return false;
}
return false;
} else {
return true;
}
}
use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.
the class NecessaryParenthesesChecker method isAllOperandsHaveSameType.
/*
* Do all operands in expression have same type
*/
private static boolean isAllOperandsHaveSameType(InfixExpression expression, ITypeBinding leftOperandType, ITypeBinding rightOperandType) {
ITypeBinding binding = leftOperandType;
if (binding == null)
return false;
ITypeBinding current = rightOperandType;
if (binding != current)
return false;
for (Iterator<Expression> iterator = expression.extendedOperands().iterator(); iterator.hasNext(); ) {
Expression operand = iterator.next();
current = operand.resolveTypeBinding();
if (binding != current)
return false;
}
return true;
}
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;
}
Aggregations