Search in sources :

Example 1 with AssertionRemover

use of fr.inria.diversify.dspot.assertGenerator.AssertionRemover in project Ex2Amplifier by STAMP-project.

the class MainGenerator method generateMainMethodFromTestMethod.

public static CtMethod<?> generateMainMethodFromTestMethod(CtMethod<?> testMethod, CtType<?> testClass) {
    final Factory factory = testMethod.getFactory();
    final CtBlock<?> blockMain = new AssertionRemover().removeAssertion(testMethod).getBody().clone();
    final List<CtLiteral<?>> originalLiterals = blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER);
    final int[] count = new int[] { 1 };
    final List<CtLocalVariable<?>> localVariables = originalLiterals.stream().map(literal -> factory.createLocalVariable(Utils.getRealTypeOfLiteral(literal), "lit" + count[0]++, factory.createCodeSnippetExpression(createMakeRead(factory, literal)))).collect(Collectors.toList());
    final CtMethod<?> mainMethod = initMainMethod(factory);
    Collections.reverse(localVariables);
    localVariables.forEach(blockMain::insertBegin);
    Collections.reverse(localVariables);
    final Iterator<CtLocalVariable<?>> iterator = localVariables.iterator();
    blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).forEach(literal -> literal.replace(factory.createVariableRead(iterator.next().getReference(), false)));
    if (!testMethod.getThrownTypes().isEmpty()) {
        final CtTry largeTryCatchBlock = createLargeTryCatchBlock(testMethod.getThrownTypes(), factory);
        largeTryCatchBlock.setBody(blockMain);
        mainMethod.setBody(largeTryCatchBlock);
    } else {
        mainMethod.setBody(blockMain);
    }
    removeNonStaticElement(mainMethod, testClass);
    return mainMethod;
}
Also used : Ex2Amplifier(eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtThrow(spoon.reflect.code.CtThrow) Function(java.util.function.Function) CtStatement(spoon.reflect.code.CtStatement) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) CtType(spoon.reflect.declaration.CtType) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Iterator(java.util.Iterator) Utils(eu.stamp.project.ex2amplifier.Utils) CtTargetedExpression(spoon.reflect.code.CtTargetedExpression) Set(java.util.Set) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) ModifierKind(spoon.reflect.declaration.ModifierKind) Optional(java.util.Optional) CtBlock(spoon.reflect.code.CtBlock) CtTry(spoon.reflect.code.CtTry) CtParameter(spoon.reflect.declaration.CtParameter) CtLiteral(spoon.reflect.code.CtLiteral) Collections(java.util.Collections) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) CtTry(spoon.reflect.code.CtTry) CtLocalVariable(spoon.reflect.code.CtLocalVariable) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) CtLiteral(spoon.reflect.code.CtLiteral)

Example 2 with AssertionRemover

use of fr.inria.diversify.dspot.assertGenerator.AssertionRemover in project Ex2Amplifier by STAMP-project.

the class ArgumentsExtractor method performExtraction.

public static CtMethod<?> performExtraction(CtMethod<?> ctMethod) {
    final CtMethod<?> ctMethodWithoutAssertion = new AssertionRemover().removeAssertion(ctMethod);
    final Factory factory = ctMethodWithoutAssertion.getFactory();
    final CtMethod<?> extractedMethod = ctMethodWithoutAssertion.clone();
    extractedMethod.setSimpleName("extract_" + ctMethodWithoutAssertion.getSimpleName());
    new ArrayList<>(extractedMethod.getThrownTypes()).forEach(extractedMethod::removeThrownType);
    ctMethodWithoutAssertion.getAnnotations().forEach(extractedMethod::removeAnnotation);
    final int[] count = new int[1];
    final Map<CtAbstractInvocation<?>, List<CtVariableAccess>> parametersPerInvocation = new HashMap<>();
    extractedMethod.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).stream().filter(literal -> !(literal.getValue() instanceof String)).forEach(ctLiteral -> {
        final CtParameter parameter = factory.createParameter(extractedMethod, Utils.getRealTypeOfLiteral(ctLiteral), "lit" + count[0]++);
        final CtParameterReference<?> parameterReference = factory.createParameterReference();
        parameterReference.setSimpleName(parameter.getSimpleName());
        parameterReference.setType(parameter.getType());
        final CtVariableAccess<?> variableRead = factory.createVariableRead(parameterReference, false);
        final CtAbstractInvocation invocation = ctLiteral.getParent(CtAbstractInvocation.class);
        if (invocation != null) {
            if (!parametersPerInvocation.containsKey(invocation)) {
                parametersPerInvocation.put(invocation, new ArrayList<>(invocation.getArguments()));
            }
            if (ctLiteral.getParent() instanceof CtUnaryOperator) {
                ctLiteral.getParent().replace(variableRead);
            } else {
                ctLiteral.replace(variableRead);
            }
        } else {
            ctLiteral.replace(variableRead);
        }
    });
    extractedMethod.setThrownTypes(ctMethod.getThrownTypes());
    return extractedMethod;
}
Also used : Ex2Amplifier(eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier) Utils(eu.stamp.project.ex2amplifier.Utils) CtParameterReference(spoon.reflect.reference.CtParameterReference) HashMap(java.util.HashMap) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) ArrayList(java.util.ArrayList) List(java.util.List) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) Map(java.util.Map) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) HashMap(java.util.HashMap) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with AssertionRemover

use of fr.inria.diversify.dspot.assertGenerator.AssertionRemover in project Ex2Amplifier by STAMP-project.

the class CATGAmplifier method buildMethodFromValues.

private CtMethod<?> buildMethodFromValues(List<String> values, CtMethod originalTestMethod) {
    final Iterator<String> iteratorOnNewValues = values.iterator();
    final CtMethod<?> clone = new AssertionRemover().removeAssertion(AmplificationHelper.cloneTestMethodForAmp(originalTestMethod, "_Ex2_CATG"));
    final List<CtLiteral<?>> originalLiterals = clone.getBody().getElements(CT_LITERAL_TYPE_FILTER);
    originalLiterals.forEach(ctLiteral -> ctLiteral.replace(buildNewLiteralFromString(iteratorOnNewValues.next(), ctLiteral)));
    return clone;
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover)

Aggregations

AssertionRemover (fr.inria.diversify.dspot.assertGenerator.AssertionRemover)3 Utils (eu.stamp.project.ex2amplifier.Utils)2 Ex2Amplifier (eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier)2 List (java.util.List)2 CtLiteral (spoon.reflect.code.CtLiteral)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtParameter (spoon.reflect.declaration.CtParameter)2 Factory (spoon.reflect.factory.Factory)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 CtAbstractInvocation (spoon.reflect.code.CtAbstractInvocation)1 CtBlock (spoon.reflect.code.CtBlock)1 CtCatch (spoon.reflect.code.CtCatch)1