use of org.eclipse.jdt.core.dom.ArrayAccess in project eclipse.jdt.ls by eclipse.
the class NecessaryParenthesesChecker method needsParentheses.
/**
* Does the <code>expression</code> need parentheses when inserted into <code>parent</code> at
* <code>locationInParent</code> ?
*
* @param expression the expression
* @param parent the parent node
* @param locationInParent location of expression in the parent
* @param leftOperandType the type of the left operand in <code>parent</code> if
* <code>parent</code> is an infix expression with no bindings and
* <code>expression</code> is the right operand in it, <code>null</code> otherwise
* @return <code>true</code> if <code>expression</code> needs parentheses, <code>false</code>
* otherwise.
*
* @since 3.9
*/
private static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
if (!expressionTypeNeedsParentheses(expression)) {
return false;
}
if (!locationNeedsParentheses(locationInParent)) {
return false;
}
if (parent instanceof Expression) {
Expression parentExpression = (Expression) parent;
if (expression instanceof PrefixExpression) {
// see bug 405096
return needsParenthesesForPrefixExpression(parentExpression, ((PrefixExpression) expression).getOperator());
}
if (expression instanceof ArrayCreation) {
// see bug 394721
return parentExpression instanceof ArrayAccess && ((ArrayCreation) expression).getInitializer() == null;
}
int expressionPrecedence = OperatorPrecedence.getExpressionPrecedence(expression);
int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(parentExpression);
if (expressionPrecedence > parentPrecedence) {
// (opEx) opParent and opEx binds more -> parentheses not needed
return false;
}
if (expressionPrecedence < parentPrecedence) {
// (opEx) opParent and opEx binds less -> parentheses needed
return true;
}
if (parentExpression instanceof InfixExpression) {
return needsParenthesesInInfixExpression(expression, (InfixExpression) parentExpression, locationInParent, leftOperandType);
}
if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
return true;
}
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.ArrayAccess in project flux by eclipse.
the class ASTResolving method guessTypeForReference.
public static Type guessTypeForReference(AST ast, ASTNode node) {
ASTNode parent = node.getParent();
while (parent != null) {
switch(parent.getNodeType()) {
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
if (((VariableDeclarationFragment) parent).getInitializer() == node) {
return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
}
return null;
case ASTNode.SINGLE_VARIABLE_DECLARATION:
if (((VariableDeclarationFragment) parent).getInitializer() == node) {
return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
}
return null;
case ASTNode.ARRAY_ACCESS:
if (!((ArrayAccess) parent).getIndex().equals(node)) {
Type type = guessTypeForReference(ast, parent);
if (type != null) {
return ASTNodeFactory.newArrayType(type);
}
}
return null;
case ASTNode.FIELD_ACCESS:
if (node.equals(((FieldAccess) parent).getName())) {
node = parent;
parent = parent.getParent();
} else {
return null;
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
case ASTNode.PARENTHESIZED_EXPRESSION:
node = parent;
parent = parent.getParent();
break;
case ASTNode.QUALIFIED_NAME:
if (node.equals(((QualifiedName) parent).getName())) {
node = parent;
parent = parent.getParent();
} else {
return null;
}
break;
default:
return null;
}
}
return null;
}
use of org.eclipse.jdt.core.dom.ArrayAccess in project AutoRefactor by JnRouvignac.
the class ObsoleteAddAllRatherThanLoopCleanUp method maybeRefactorForStatement.
private boolean maybeRefactorForStatement(final ForStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ForLoopContent loopContent = ForLoops.iterateOverContainer(node);
MethodInvocation methodInvocation = ASTNodes.asExpression(node.getBody(), MethodInvocation.class);
if (loopContent != null && loopContent.getLoopVariable() != null && methodInvocation != null) {
Name loopVariable = loopContent.getLoopVariable();
IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
// As we replace only one, there should be no more than one occurrence
if (methodInvocation != null && methodInvocation.arguments().size() == 1 && getVariableUseCount(loopVariableName, node.getBody()) == 1 && (loopContent.isLoopingForward() || methodInvocation.resolveMethodBinding() != null && ASTNodes.hasType(methodInvocation.resolveMethodBinding().getDeclaringClass(), Set.class.getCanonicalName()))) {
Expression addArg0 = (Expression) methodInvocation.arguments().get(0);
switch(loopContent.getContainerType()) {
case COLLECTION:
MethodInvocation getMI = ASTNodes.as(addArg0, MethodInvocation.class);
if (getMI != null && getMI.arguments().size() == 1 && isSameVariable(loopContent, getMI)) {
return maybeReplaceForCollection(node, methodInvocation, getMI.getExpression());
}
break;
case ARRAY:
ArrayAccess arrayAccess = ASTNodes.as(addArg0, ArrayAccess.class);
if (isSameVariable(loopContent, arrayAccess)) {
return maybeReplaceForArray(node, classesToUseWithImport, importsToAdd, loopContent.getContainerVariable(), methodInvocation);
}
break;
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.ArrayAccess in project AutoRefactor by JnRouvignac.
the class ObsoleteFillRatherThanLoopCleanUp method maybeRefactorForStatement.
private boolean maybeRefactorForStatement(final ForStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ForLoopContent loopContent = ForLoops.iterateOverContainer(node);
Assignment assignment = ASTNodes.asExpression(node.getBody(), Assignment.class);
if (assignment != null && loopContent != null && loopContent.getLoopVariable() != null && loopContent.getContainerType() == ContainerType.ARRAY && ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN) && ASTNodes.isHardCoded(assignment.getRightHandSide()) && ASTNodes.isPassive(assignment.getRightHandSide())) {
ArrayAccess arrayAccess = ASTNodes.as(assignment.getLeftHandSide(), ArrayAccess.class);
if (arrayAccess != null && isSameVariable(loopContent, arrayAccess)) {
replaceWithArraysFill(node, classesToUseWithImport, importsToAdd, assignment, arrayAccess);
return false;
}
}
return true;
}
use of org.eclipse.jdt.core.dom.ArrayAccess in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final PostfixExpression node) {
final AST dummyAST = AST.newAST(node.getAST().apiLevel());
final PostfixExpression.Operator pfOperator = node.getOperator();
Expression _operand = node.getOperand();
if ((_operand instanceof ArrayAccess)) {
Expression _operand_1 = node.getOperand();
final ArrayAccess pfOperand = ((ArrayAccess) _operand_1);
if ((Objects.equal(pfOperator, PostfixExpression.Operator.INCREMENT) || Objects.equal(pfOperator, PostfixExpression.Operator.DECREMENT))) {
final String arrayName = this.computeArrayName(pfOperand);
StringConcatenation _builder = new StringConcatenation();
_builder.append("_postIndx_");
_builder.append(arrayName);
final String idxName = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("_postVal_");
_builder_1.append(arrayName);
final String tempVarName = _builder_1.toString();
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("{ var ");
_builder_2.append(idxName);
_builder_2.append("=");
this.appendToBuffer(_builder_2.toString());
pfOperand.getIndex().accept(this);
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append(" ");
_builder_3.append("var ");
this.appendToBuffer(_builder_3.toString());
final VariableDeclarationFragment varDeclaration = dummyAST.newVariableDeclarationFragment();
varDeclaration.setName(dummyAST.newSimpleName(tempVarName));
ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, pfOperand);
final ArrayAccess arrayAccess = ((ArrayAccess) _copySubtree);
arrayAccess.setIndex(dummyAST.newSimpleName(idxName));
varDeclaration.setInitializer(arrayAccess);
varDeclaration.accept(this);
final InfixExpression infixOp = dummyAST.newInfixExpression();
infixOp.setLeftOperand(dummyAST.newSimpleName(tempVarName));
PostfixExpression.Operator _operator = node.getOperator();
boolean _equals = Objects.equal(_operator, PostfixExpression.Operator.DECREMENT);
if (_equals) {
infixOp.setOperator(InfixExpression.Operator.MINUS);
} else {
infixOp.setOperator(InfixExpression.Operator.PLUS);
}
infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
final Assignment assigment = dummyAST.newAssignment();
ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, pfOperand);
final ArrayAccess writeArray = ((ArrayAccess) _copySubtree_1);
writeArray.setIndex(dummyAST.newSimpleName(idxName));
assigment.setLeftHandSide(writeArray);
ASTNode _copySubtree_2 = ASTNode.copySubtree(dummyAST, infixOp);
assigment.setRightHandSide(((Expression) _copySubtree_2));
assigment.accept(this);
StringConcatenation _builder_4 = new StringConcatenation();
String _xifexpression = null;
boolean _needsReturnValue = this._aSTFlattenerUtils.needsReturnValue(node);
if (_needsReturnValue) {
_xifexpression = tempVarName;
}
_builder_4.append(_xifexpression);
_builder_4.append(" }");
this.appendToBuffer(_builder_4.toString());
return false;
}
}
node.getOperand().accept(this);
this.appendToBuffer(pfOperator.toString());
return false;
}
Aggregations