Search in sources :

Example 1 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project che by eclipse.

the class ConvertForLoopOperation method convertBody.

private void convertBody(Statement body, final IBinding indexBinding, final IBinding arrayBinding, final String parameterName, final ASTRewrite rewrite, final TextEditGroup editGroup, final LinkedProposalPositionGroup pg) {
    final AST ast = body.getAST();
    body.accept(new GenericVisitor() {

        @Override
        public boolean visit(ArrayAccess node) {
            IBinding binding = getBinding(node.getArray());
            if (arrayBinding.equals(binding)) {
                IBinding index = getBinding(node.getIndex());
                if (indexBinding.equals(index)) {
                    replaceAccess(node);
                }
            }
            return super.visit(node);
        }

        private void replaceAccess(ASTNode node) {
            if (fElementDeclaration != null && node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getParent();
                IBinding targetBinding = fragment.getName().resolveBinding();
                if (targetBinding != null) {
                    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                    if (statement.fragments().size() == 1) {
                        rewrite.remove(statement, editGroup);
                    } else {
                        ListRewrite listRewrite = rewrite.getListRewrite(statement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
                        listRewrite.remove(fragment, editGroup);
                    }
                } else {
                    SimpleName name = ast.newSimpleName(parameterName);
                    rewrite.replace(node, name, editGroup);
                    pg.addPosition(rewrite.track(name), true);
                }
            } else {
                SimpleName name = ast.newSimpleName(parameterName);
                rewrite.replace(node, name, editGroup);
                pg.addPosition(rewrite.track(name), true);
            }
        }
    });
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 2 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project AutoRefactor by JnRouvignac.

the class HotSpotIntrinsicedAPIsRefactoring method visit.

@Override
public boolean visit(ForStatement node) {
    final SystemArrayCopyParams params = new SystemArrayCopyParams();
    collectUniqueIndex(node, params);
    final IVariableBinding incrementedIdx = getUniqueIncrementedVariable(node);
    final List<Statement> stmts = asList(node.getBody());
    if (equalNotNull(params.indexVarBinding, incrementedIdx) && stmts.size() == 1) {
        collectLength(node.getExpression(), incrementedIdx, params);
        final Assignment as = asExpression(stmts.get(0), Assignment.class);
        if (hasOperator(as, ASSIGN)) {
            final Expression lhs = as.getLeftHandSide();
            final Expression rhs = as.getRightHandSide();
            if (lhs instanceof ArrayAccess && rhs instanceof ArrayAccess) {
                final ArrayAccess aaLHS = (ArrayAccess) lhs;
                final ArrayAccess aaRHS = (ArrayAccess) rhs;
                params.destArrayExpr = aaLHS.getArray();
                params.srcArrayExpr = aaRHS.getArray();
                if (haveSameType(params.srcArrayExpr, params.destArrayExpr)) {
                    params.destPos = calcIndex(aaLHS.getIndex(), params);
                    params.srcPos = calcIndex(aaRHS.getIndex(), params);
                    return replaceWithSystemArrayCopyCloneAll(node, params);
                }
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 3 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(ArrayAccess arrayAccess) {
    Expression expr = arrayAccess.getArray();
    List<Integer> indices = new ArrayList<Integer>();
    // TODO This is a shortcut
    // we need a variable reference for the index value
    indices.add(getArrayIndex(arrayAccess.getIndex()));
    while (expr instanceof ArrayAccess) {
        ArrayAccess current = (ArrayAccess) expr;
        expr = current.getArray();
        indices.add(getArrayIndex(current.getIndex()));
    }
    Collections.reverse(indices);
    VariableReference varRef = retrieveVariableReference(expr, null);
    ArrayReference arrayReference = (ArrayReference) varRef;
    assert indices.size() == arrayReference.getArrayDimensions();
    ArrayIndex arrayIndex = new ArrayIndex(testCase.getReference(), arrayReference, indices);
    return arrayIndex;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ArrayList(java.util.ArrayList) ArrayIndex(org.evosuite.testcase.ArrayIndex)

Example 4 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveVariableReference.

/**
 * <p>
 * retrieveVariableReference
 * </p>
 *
 * @param argument
 *            a {@link java.lang.Object} object.
 * @param varType
 *            a {@link java.lang.Class} object.
 * @return a {@link org.evosuite.testcase.VariableReference} object.
 */
protected VariableReference retrieveVariableReference(Object argument, Class<?> varType) {
    if (argument instanceof ClassInstanceCreation) {
        return retrieveVariableReference((ClassInstanceCreation) argument, varType);
    }
    if (argument instanceof VariableDeclarationFragment) {
        return retrieveVariableReference((VariableDeclarationFragment) argument);
    }
    if (argument instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) argument;
        lineNumber = testReader.getLineNumber(simpleName.getStartPosition());
        return retrieveVariableReference(simpleName.resolveBinding(), varType);
    }
    if (argument instanceof IVariableBinding) {
        return retrieveVariableReference((IVariableBinding) argument, varType);
    }
    if (argument instanceof PrefixExpression) {
        return retrieveVariableReference((PrefixExpression) argument);
    }
    if (argument instanceof InfixExpression) {
        return retrieveVariableReference((InfixExpression) argument, varType);
    }
    if (argument instanceof ExpressionStatement) {
        ExpressionStatement exprStmt = (ExpressionStatement) argument;
        Expression expression = exprStmt.getExpression();
        return retrieveVariableReference(expression, varType);
    }
    if (argument instanceof NullLiteral) {
        return retrieveVariableReference((NullLiteral) argument, varType);
    }
    if (argument instanceof StringLiteral) {
        return retrieveVariableReference((StringLiteral) argument);
    }
    if (argument instanceof NumberLiteral) {
        return retrieveVariableReference((NumberLiteral) argument);
    }
    if (argument instanceof CharacterLiteral) {
        return retrieveVariableReference((CharacterLiteral) argument);
    }
    if (argument instanceof BooleanLiteral) {
        return retrieveVariableReference((BooleanLiteral) argument);
    }
    if (argument instanceof ITypeBinding) {
        if (varType != null) {
            return new ValidVariableReference(testCase.getReference(), varType);
        }
        return new ValidVariableReference(testCase.getReference(), retrieveTypeClass(argument));
    }
    if (argument instanceof QualifiedName) {
        return retrieveVariableReference((QualifiedName) argument);
    }
    if (argument instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) argument;
        VariableReference result = retrieveResultReference(methodInvocation);
        nestedCallResults.push(result);
        return result;
    }
    if (argument instanceof ArrayCreation) {
        return retrieveVariableReference((ArrayCreation) argument);
    }
    if (argument instanceof VariableDeclaration) {
        return retrieveVariableReference((VariableDeclaration) argument);
    }
    if (argument instanceof ArrayAccess) {
        // argument).getArray(), null);
        return retrieveVariableReference((ArrayAccess) argument);
    }
    if (argument instanceof Assignment) {
        return retrieveVariableReference(((Assignment) argument).getLeftHandSide(), null);
    }
    if (argument instanceof CastExpression) {
        CastExpression castExpression = (CastExpression) argument;
        VariableReference result = retrieveVariableReference(castExpression.getExpression(), null);
        Class<?> castClass = retrieveTypeClass(castExpression.resolveTypeBinding());
        assert castClass.isAssignableFrom(toClass(result.getType()));
        result.setType(castClass);
        return result;
    }
    throw new UnsupportedOperationException("Argument type " + argument.getClass() + " not implemented!");
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 5 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project evosuite by EvoSuite.

the class JUnitCodeGenerator method createArrayInitStmt.

@Override
public void createArrayInitStmt(final CaptureLog log, final int logRecNo) {
    final int oid = log.objectIds.get(logRecNo);
    final Object[] params = log.params.get(logRecNo);
    final String arrTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid));
    final Class<?> arrType = getClassForName(arrTypeName);
    // --- create array instance creation e.g. int[] var = new int[10];
    final ArrayType arrAstType = (ArrayType) createAstArrayType(arrTypeName, ast);
    final ArrayCreation arrCreationExpr = ast.newArrayCreation();
    arrCreationExpr.setType(arrAstType);
    arrCreationExpr.dimensions().add(ast.newNumberLiteral(String.valueOf(params.length)));
    final String arrVarName = this.createNewVarName(oid, arrTypeName);
    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    final SimpleName arrVarNameExpr = ast.newSimpleName(arrVarName);
    vd.setName(arrVarNameExpr);
    vd.setInitializer(arrCreationExpr);
    final VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(vd);
    varDeclStmt.setType(this.createAstType(arrTypeName, ast));
    methodBlock.statements().add(varDeclStmt);
    // create array access statements var[0] = var1;
    Integer paramOID;
    Assignment assign;
    ArrayAccess arrAccessExpr;
    for (int i = 0; i < params.length; i++) {
        assign = ast.newAssignment();
        arrAccessExpr = ast.newArrayAccess();
        arrAccessExpr.setIndex(ast.newNumberLiteral(String.valueOf(i)));
        arrAccessExpr.setArray(arrVarNameExpr);
        assign.setLeftHandSide(arrAccessExpr);
        paramOID = (Integer) params[i];
        if (paramOID == null) {
            assign.setRightHandSide(ast.newNullLiteral());
        } else {
            assign.setRightHandSide(ast.newSimpleName(this.oidToVarMapping.get(paramOID)));
        }
        methodBlock.statements().add(assign);
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

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