Search in sources :

Example 11 with ArrayAccess

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;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 12 with ArrayAccess

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;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 13 with ArrayAccess

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;
}
Also used : ForLoopContent(org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) HashSet(java.util.HashSet) Set(java.util.Set) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name)

Example 14 with ArrayAccess

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;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ForLoopContent(org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess)

Example 15 with ArrayAccess

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;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) AST(org.eclipse.jdt.core.dom.AST) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Aggregations

ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)19 Expression (org.eclipse.jdt.core.dom.Expression)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 ArrayType (org.eclipse.jdt.core.dom.ArrayType)6 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)6 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)6 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)6 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)5 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)5 AST (org.eclipse.jdt.core.dom.AST)4