Search in sources :

Example 1 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.

the class AssertionRemover method removeAssertion.

/**
 * Replaces an invocation with its arguments.
 *
 * @param invocation Invocation
 */
public void removeAssertion(CtInvocation<?> invocation) {
    final Factory factory = invocation.getFactory();
    invocation.getArguments().forEach(argument -> {
        CtExpression clone = ((CtExpression) argument).clone();
        if (clone instanceof CtUnaryOperator) {
            clone = ((CtUnaryOperator) clone).getOperand();
        }
        if (clone instanceof CtStatement) {
            clone.getTypeCasts().clear();
            invocation.insertBefore((CtStatement) clone);
        } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
            CtTypeReference<?> typeOfParameter = clone.getType();
            if (clone.getType().equals(factory.Type().NULL_TYPE)) {
                typeOfParameter = factory.Type().createReference(Object.class);
            }
            final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
            invocation.insertBefore(localVariable);
        }
    });
    CtElement currentParent = invocation;
    while (!(currentParent.getParent() instanceof CtStatementList)) {
        currentParent = currentParent.getParent();
    }
    ((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtStatementList(spoon.reflect.code.CtStatementList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtVariableRead(spoon.reflect.code.CtVariableRead)

Example 2 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.

the class MethodsAssertGenerator method makeFailureTest.

/**
 * Adds surrounding try/catch/fail in a failing test.
 *
 * @param test Failing test method to amplify
 * @param failure Test's failure description
 * @return New amplified test
 */
protected CtMethod<?> makeFailureTest(CtMethod<?> test, Failure failure) {
    CtMethod cloneMethodTest = AmplificationHelper.cloneTestMethodForAmp(test, "");
    cloneMethodTest.setSimpleName(test.getSimpleName());
    Factory factory = cloneMethodTest.getFactory();
    Throwable exception = failure.getException();
    if (// TestTimedOutException means infinite loop
    exception instanceof TestTimedOutException || exception instanceof AssertionError) {
        // AssertionError means that some assertion remained in the test: TODO
        return null;
    }
    Class exceptionClass;
    if (exception == null) {
        exceptionClass = Exception.class;
    } else {
        exceptionClass = exception.getClass();
    }
    CtTry tryBlock = factory.Core().createTry();
    tryBlock.setBody(cloneMethodTest.getBody());
    String snippet = "org.junit.Assert.fail(\"" + test.getSimpleName() + " should have thrown " + exceptionClass.getSimpleName() + "\")";
    tryBlock.getBody().addStatement(factory.Code().createCodeSnippetStatement(snippet));
    DSpotUtils.addComment(tryBlock, "AssertGenerator generate try/catch block with fail statement", CtComment.CommentType.INLINE);
    CtCatch ctCatch = factory.Core().createCatch();
    CtTypeReference exceptionType = factory.Type().createReference(exceptionClass);
    ctCatch.setParameter(factory.Code().createCatchVariable(exceptionType, "eee"));
    ctCatch.setBody(factory.Core().createBlock());
    List<CtCatch> catchers = new ArrayList<>(1);
    catchers.add(ctCatch);
    tryBlock.setCatchers(catchers);
    CtBlock body = factory.Core().createBlock();
    body.addStatement(tryBlock);
    cloneMethodTest.setBody(body);
    cloneMethodTest.setSimpleName(cloneMethodTest.getSimpleName() + "_failAssert" + (numberOfFail++));
    Counter.updateAssertionOf(cloneMethodTest, 1);
    return cloneMethodTest;
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Factory(spoon.reflect.factory.Factory) CtTypeReference(spoon.reflect.reference.CtTypeReference) TestTimedOutException(org.junit.runners.model.TestTimedOutException) CtMethod(spoon.reflect.declaration.CtMethod)

Example 3 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.

the class CollectionCreator method generateEmptyCollection.

static CtExpression<?> generateEmptyCollection(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) {
    final Factory factory = type.getFactory();
    final CtType<?> collectionsType = factory.Type().get(Collections.class);
    final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference());
    final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0);
    final CtExecutableReference<?> executableReference = factory.Core().createExecutableReference();
    executableReference.setStatic(true);
    executableReference.setSimpleName(singletonListMethod.getSimpleName());
    executableReference.setDeclaringType(collectionsType.getReference());
    executableReference.setType(factory.createCtTypeReference(typeOfCollection));
    if (type.getActualTypeArguments().isEmpty()) {
        // supporting Collections.<type>emptyList()
        executableReference.addActualTypeArgument(type);
    } else if (type.getActualTypeArguments().stream().noneMatch(reference -> reference instanceof CtWildcardReference)) {
        // in case type is a list, we copy the Actual arguments
        executableReference.setActualTypeArguments(type.getActualTypeArguments());
    }
    return factory.createInvocation(accessToCollections, executableReference);
}
Also used : CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) CtType(spoon.reflect.declaration.CtType) CtExpression(spoon.reflect.code.CtExpression) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Factory(spoon.reflect.factory.Factory) CtWildcardReference(spoon.reflect.reference.CtWildcardReference) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) CtMethod(spoon.reflect.declaration.CtMethod) CtWildcardReference(spoon.reflect.reference.CtWildcardReference) Factory(spoon.reflect.factory.Factory)

Example 4 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.

the class ValueCreator method generateArray.

private static CtExpression generateArray(CtTypeReference type) {
    CtArrayTypeReference arrayType = (CtArrayTypeReference) type;
    CtTypeReference typeComponent = arrayType.getComponentType();
    CtNewArray<?> newArray = type.getFactory().createNewArray();
    final int size = AmplificationHelper.getRandom().nextInt(MAX_ARRAY_SIZE);
    newArray.setType(arrayType);
    if (size == 0) {
        newArray.setDimensionExpressions(Collections.singletonList(type.getFactory().createLiteral(size)));
    } else if (ValueCreatorHelper.canGenerateAValueForType(typeComponent)) {
        IntStream.range(0, size).mapToObj(i -> generateRandomValue(typeComponent)).forEach(newArray::addElement);
    }
    return newArray;
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 5 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.

the class AssertGeneratorHelper method isStmtToLog.

private static boolean isStmtToLog(String filter, CtStatement statement) {
    if (!(statement.getParent() instanceof CtBlock)) {
        return false;
    }
    // contract: for now, we do not log values inside loop
    if (statement.getParent(CtLoop.class) != null) {
        return false;
    }
    if (statement instanceof CtInvocation) {
        CtInvocation invocation = (CtInvocation) statement;
        // type tested by the test class
        String targetType = "";
        if (invocation.getTarget() != null && invocation.getTarget().getType() != null) {
            targetType = invocation.getTarget().getType().getQualifiedName();
        }
        if (targetType.startsWith(filter)) {
            return (!isVoidReturn(invocation) && !isGetter(invocation));
        } else {
            return !isVoidReturn(invocation);
        }
    }
    if (statement instanceof CtLocalVariable || statement instanceof CtAssignment || statement instanceof CtVariableWrite) {
        if (statement instanceof CtNamedElement) {
            if (((CtNamedElement) statement).getSimpleName().startsWith("__DSPOT_")) {
                return false;
            }
        }
        final CtTypeReference type = ((CtTypedElement) statement).getType();
        if (type.getQualifiedName().startsWith(filter)) {
            return true;
        } else {
            try {
                return type.getActualClass() == String.class;
            } catch (SpoonClassNotFoundException e) {
                return false;
            }
        }
    } else {
        return false;
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtNamedElement(spoon.reflect.declaration.CtNamedElement)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9