Search in sources :

Example 16 with ArrayCreation

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

the class ASTNodeFactory method newArrayCreation.

/**
 * Builds a new {@link ArrayCreation} instance.
 *
 * @param arrayType        the array type
 * @param arrayInitializer the array initializer
 * @return a new array creation instance
 */
public ArrayCreation newArrayCreation(final ArrayType arrayType, final ArrayInitializer arrayInitializer) {
    ArrayCreation ac = ast.newArrayCreation();
    ac.setType(arrayType);
    ac.setInitializer(arrayInitializer);
    return ac;
}
Also used : ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation)

Example 17 with ArrayCreation

use of org.eclipse.jdt.core.dom.ArrayCreation in project xtext-xtend by eclipse.

the class JavaASTFlattener method handleRightHandSide.

public StringBuffer handleRightHandSide(final Assignment a, final Type type) {
    StringBuffer _xifexpression = null;
    if ((this._aSTFlattenerUtils.needPrimitiveCast(type) && (!(a.getRightHandSide() instanceof ArrayCreation)))) {
        StringBuffer _xblockexpression = null;
        {
            this.appendToBuffer("(");
            a.getRightHandSide().accept(this);
            StringConcatenation _builder = new StringConcatenation();
            _builder.append(") as ");
            _builder.append(type);
            _xblockexpression = this.appendToBuffer(_builder.toString());
        }
        _xifexpression = _xblockexpression;
    } else {
        a.getRightHandSide().accept(this);
    }
    return _xifexpression;
}
Also used : ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 18 with ArrayCreation

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

the class CodeGenerator method createCallMethodOrNewInstanceCallStmt.

@SuppressWarnings("unchecked")
private MethodInvocation createCallMethodOrNewInstanceCallStmt(final boolean isConstructorCall, final AST ast, final String varName, final String targetTypeName, final String methodName, final Object[] methodArgs, final org.objectweb.asm.Type[] paramTypes) {
    // -- construct getField() call
    final MethodInvocation callMethodCall = ast.newMethodInvocation();
    if (isConstructorCall) {
        callMethodCall.setName(ast.newSimpleName("newInstance"));
    } else {
        callMethodCall.setName(ast.newSimpleName("callMethod"));
    }
    StringLiteral stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue(targetTypeName);
    // class name
    callMethodCall.arguments().add(stringLiteral);
    if (!isConstructorCall) {
        stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(methodName);
        // method name
        callMethodCall.arguments().add(stringLiteral);
        if (varName == null) {
            // static call -> no receiver
            callMethodCall.arguments().add(ast.newNullLiteral());
        } else {
            // receiver
            callMethodCall.arguments().add(ast.newSimpleName(varName));
        }
    }
    // method arguments
    ArrayCreation arrCreation = ast.newArrayCreation();
    arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
    ArrayInitializer arrInit = ast.newArrayInitializer();
    arrCreation.setInitializer(arrInit);
    callMethodCall.arguments().add(arrCreation);
    // is either an oid or null
    Integer arg;
    for (int i = 0; i < methodArgs.length; i++) {
        arg = (Integer) methodArgs[i];
        if (arg == null) {
            arrInit.expressions().add(ast.newNullLiteral());
        } else {
            arrInit.expressions().add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
        }
    }
    // paramTypes
    arrCreation = ast.newArrayCreation();
    arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
    arrInit = ast.newArrayInitializer();
    arrCreation.setInitializer(arrInit);
    callMethodCall.arguments().add(arrCreation);
    org.objectweb.asm.Type type;
    for (int i = 0; i < paramTypes.length; i++) {
        type = paramTypes[i];
        if (type.equals(org.objectweb.asm.Type.BOOLEAN_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Boolean"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.BYTE_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Byte"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.CHAR_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Character"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.DOUBLE_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Double"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.FLOAT_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Float"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.INT_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Integer"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.LONG_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Long"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.SHORT_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Short"));
            arrInit.expressions().add(facc);
        } else {
            final TypeLiteral clazz = ast.newTypeLiteral();
            clazz.setType(ast.newSimpleType(ast.newName(type.getClassName())));
            arrInit.expressions().add(clazz);
        }
    }
    return callMethodCall;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 19 with ArrayCreation

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

the class JUnitCodeGenerator method createCallMethodOrNewInstanceCallStmt.

@SuppressWarnings("unchecked")
private MethodInvocation createCallMethodOrNewInstanceCallStmt(final boolean isConstructorCall, final AST ast, final String varName, final String targetTypeName, final String methodName, final Object[] methodArgs, final org.objectweb.asm.Type[] paramTypes) {
    // -- construct getField() call
    final MethodInvocation callMethodCall = ast.newMethodInvocation();
    if (isConstructorCall) {
        callMethodCall.setName(ast.newSimpleName("newInstance"));
    } else {
        callMethodCall.setName(ast.newSimpleName("callMethod"));
    }
    StringLiteral stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue(targetTypeName);
    // class name
    callMethodCall.arguments().add(stringLiteral);
    if (!isConstructorCall) {
        stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(methodName);
        // method name
        callMethodCall.arguments().add(stringLiteral);
        if (varName == null) {
            // static call -> no receiver
            callMethodCall.arguments().add(ast.newNullLiteral());
        } else {
            // receiver
            callMethodCall.arguments().add(ast.newSimpleName(varName));
        }
    }
    // method arguments
    ArrayCreation arrCreation = ast.newArrayCreation();
    arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
    ArrayInitializer arrInit = ast.newArrayInitializer();
    arrCreation.setInitializer(arrInit);
    callMethodCall.arguments().add(arrCreation);
    // is either an oid or null
    Integer arg;
    for (int i = 0; i < methodArgs.length; i++) {
        arg = (Integer) methodArgs[i];
        if (arg == null) {
            arrInit.expressions().add(ast.newNullLiteral());
        } else {
            arrInit.expressions().add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
        }
    }
    // paramTypes
    arrCreation = ast.newArrayCreation();
    arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
    arrInit = ast.newArrayInitializer();
    arrCreation.setInitializer(arrInit);
    callMethodCall.arguments().add(arrCreation);
    org.objectweb.asm.Type type;
    for (int i = 0; i < paramTypes.length; i++) {
        type = paramTypes[i];
        if (type.equals(org.objectweb.asm.Type.BOOLEAN_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Boolean"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.BYTE_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Byte"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.CHAR_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Character"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.DOUBLE_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Double"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.FLOAT_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Float"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.INT_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Integer"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.LONG_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Long"));
            arrInit.expressions().add(facc);
        } else if (type.equals(org.objectweb.asm.Type.SHORT_TYPE)) {
            FieldAccess facc = ast.newFieldAccess();
            facc.setName(ast.newSimpleName("TYPE"));
            facc.setExpression(ast.newSimpleName("Short"));
            arrInit.expressions().add(facc);
        } else {
            final TypeLiteral clazz = ast.newTypeLiteral();
            clazz.setType(ast.newSimpleType(ast.newName(type.getClassName())));
            arrInit.expressions().add(clazz);
        }
    }
    return callMethodCall;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 20 with ArrayCreation

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

the class TestExtractingVisitor method visit.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public boolean visit(VariableDeclarationStatement variableDeclStmt) {
    Class<?> varType = retrieveTypeClass(variableDeclStmt.getType());
    if (varType.isPrimitive() || PRIMITIVE_CLASSES.contains(varType)) {
        // Can only happen to primitive types and String,
        // otherwise it is a constructor call which is handled elsewhere
        logger.debug("Variable has not been treated elsewhere...");
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
        Expression expression = varDeclFrgmnt.getInitializer();
        VariableReference varRef = retrieveVariableReference(expression, null);
        varRef.setOriginalCode(variableDeclStmt.toString());
        // TODO Use the name here as well?
        // String name = varDeclFrgmt.getName().getIdentifier();
        // new BoundVariableReferenceImpl(testCase, varType, name);
        testCase.addVariable(varDeclFrgmnt.resolveBinding(), varRef);
        return true;
    }
    if (varType.isArray()) {
        // if (varType.getComponentType().isPrimitive() ||
        // varType.getComponentType().equals(String.class)) {
        // ... or to primitive and string arrays
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
        Expression expression = varDeclFrgmnt.getInitializer();
        if (expression instanceof ArrayInitializer) {
            ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
            ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
            arrayReference.setOriginalCode(variableDeclStmt.toString());
            testCase.addStatement(arrayStatement);
            testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayReference);
            ArrayInitializer arrayInitializer = (ArrayInitializer) expression;
            for (int idx = 0; idx < arrayInitializer.expressions().size(); idx++) {
                Expression expr = (Expression) arrayInitializer.expressions().get(idx);
                VariableReference valueRef;
                if (expr instanceof NumberLiteral) {
                    valueRef = retrieveVariableReference((NumberLiteral) expr, varType.getComponentType());
                } else if (expr instanceof PrefixExpression) {
                    valueRef = retrieveVariableReference((PrefixExpression) expr, varType.getComponentType());
                } else {
                    valueRef = retrieveVariableReference(expr, null);
                }
                valueRef.setOriginalCode(expr.toString());
                VariableReference arrayElementRef = new ArrayIndex(testCase.getReference(), arrayReference, idx);
                arrayElementRef.setOriginalCode(expr.toString());
                arrayStatement.getVariableReferences().add(arrayElementRef);
                AssignmentStatement arrayAssignment = new AssignmentStatement(testCase.getReference(), arrayElementRef, valueRef);
                testCase.addStatement(arrayAssignment);
            }
            // }
            return true;
        }
        if (expression instanceof ArrayCreation) {
            ArrayCreation arrayCreation = ((ArrayCreation) expression);
            List paramTypes = new ArrayList();
            for (int idx = 0; idx < arrayCreation.dimensions().size(); idx++) {
                paramTypes.add(int.class);
            }
            List<VariableReference> lengthsVarRefs = convertParams(arrayCreation.dimensions(), paramTypes);
            ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
            arrayReference.setOriginalCode(variableDeclStmt.toString());
            ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
            arrayStatement.setLengths(getLengths(variableDeclStmt, lengthsVarRefs));
            testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayStatement.getReturnValue());
            testCase.addStatement(arrayStatement);
        }
    }
    return true;
}
Also used : ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) ArrayList(java.util.ArrayList) ArrayIndex(org.evosuite.testcase.ArrayIndex) 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) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ArrayStatement(org.evosuite.testcase.ArrayStatement) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) List(java.util.List) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Aggregations

ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)21 Expression (org.eclipse.jdt.core.dom.Expression)11 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)8 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 AST (org.eclipse.jdt.core.dom.AST)7 ArrayType (org.eclipse.jdt.core.dom.ArrayType)7 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)7 Assignment (org.eclipse.jdt.core.dom.Assignment)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)6 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)6 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5