Search in sources :

Example 1 with CtMethod

use of spoon.reflect.declaration.CtMethod in project Ex2Amplifier by STAMP-project.

the class Ex2AmplifierTest method testUsingJBSE2.

@Test
public void testUsingJBSE2() throws Exception {
    final Ex2Amplifier ex2Amplifier = Ex2Amplifier.getEx2Amplifier(Ex2Amplifier.Ex2Amplifier_Mode.JBSE);
    ex2Amplifier.init(this.configuration);
    final CtClass<?> testClass = this.launcher.getFactory().Class().get("fr.inria.stamp.MainTest");
    ex2Amplifier.reset(testClass);
    final CtMethod<?> test = testClass.getMethodsByName("test2").get(0);
    final List<CtMethod> apply = ex2Amplifier.apply(test);
    System.out.println(apply);
}
Also used : Ex2Amplifier(eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(eu.stamp.project.ex2amplifier.AbstractTest)

Example 2 with CtMethod

use of spoon.reflect.declaration.CtMethod in project Ex2Amplifier by STAMP-project.

the class Ex2AmplifierTest method test.

@Test
public void test() throws Exception {
    /*
            Test that the Ex2Amplifier returns a List of CtMethod build thank to CATG.
            Amplified CtMethod has the same "structural" inputs of the test, but with
            different test data input, i.e. literals has been modified.
         */
    final Ex2Amplifier ex2Amplifier = Ex2Amplifier.getEx2Amplifier(Ex2Amplifier.Ex2Amplifier_Mode.CATG);
    ex2Amplifier.init(this.configuration);
    final CtClass<?> testClass = this.launcher.getFactory().Class().get("fr.inria.stamp.MainTest");
    ex2Amplifier.reset(testClass);
    final CtMethod<?> test = testClass.getMethodsByName("test").get(0);
    final List<CtMethod> apply = ex2Amplifier.apply(test);
    assertEquals(3, apply.size());
    try (BufferedReader buffer = new BufferedReader(new FileReader(this.configuration.getOutputDirectory() + "/" + testClass.getQualifiedName() + "/test_values.csv"))) {
        assertEquals("\"\\\"bar\\\"\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "\"NEW\\nLINE\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "true,false,false,false" + AmplificationHelper.LINE_SEPARATOR + "'<','0','0','0'" + AmplificationHelper.LINE_SEPARATOR + "'\\'','0','0','0'" + AmplificationHelper.LINE_SEPARATOR + "3,0,0,0,0,0,0,0,0,0,0,0,0" + AmplificationHelper.LINE_SEPARATOR + "16,0,0,0" + AmplificationHelper.LINE_SEPARATOR + "100,0,0,0" + AmplificationHelper.LINE_SEPARATOR + "\"Potion\",\"\\uffff\",\"\\u0000\",\"\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "5,0,0,1" + AmplificationHelper.LINE_SEPARATOR + "\"Timoleon\",\"\",\"\",\"\"" + AmplificationHelper.LINE_SEPARATOR + "1000,0,0,0", buffer.lines().collect(Collectors.joining(AmplificationHelper.LINE_SEPARATOR)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    System.out.println(apply);
}
Also used : Ex2Amplifier(eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(eu.stamp.project.ex2amplifier.AbstractTest)

Example 3 with CtMethod

use of spoon.reflect.declaration.CtMethod in project Ex2Amplifier by STAMP-project.

the class JBSEAmplifier method generateNewTestMethod.

private CtMethod<?> generateNewTestMethod(CtMethod<?> testMethod, Map<String, List<String>> conditionForParameter) {
    final CtMethod clone = AmplificationHelper.cloneTestMethodForAmp(testMethod, "_Ex2_JBSE");
    final List<?> solutions = SMTSolver.solve(conditionForParameter);
    final Iterator<?> iterator = solutions.iterator();
    final List<CtLiteral> originalLiterals = clone.getElements(new TypeFilter<>(CtLiteral.class));
    conditionForParameter.keySet().forEach(s -> {
        try {
            final int indexOfLit = Integer.parseInt(s.substring("param".length()));
            final CtLiteral literalToBeReplaced = originalLiterals.get(indexOfLit);
            final CtLiteral<?> newLiteral = testMethod.getFactory().createLiteral(iterator.next());
            if (!this.intermediateAmplification.containsKey(literalToBeReplaced)) {
                this.intermediateAmplification.put(literalToBeReplaced, new ArrayList<>());
            }
            this.intermediateAmplification.get(literalToBeReplaced).add(newLiteral);
            if (literalToBeReplaced.getParent() instanceof CtUnaryOperator) {
                literalToBeReplaced.getParent().replace(newLiteral);
            } else {
                literalToBeReplaced.replace(newLiteral);
            }
        } catch (Exception e) {
            LOGGER.warn("Error when trying to generate a value for {}", s);
        }
    });
    return clone;
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtMethod(spoon.reflect.declaration.CtMethod)

Example 4 with CtMethod

use of spoon.reflect.declaration.CtMethod 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 5 with CtMethod

use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.

the class AnnotationFactory method annotate.

/**
 * Creates/updates an element's annotation value.
 *
 * @param element
 * 		the program element to annotate
 * @param annotationType
 * 		the annotation type
 * @param annotationElementName
 * 		the annotation element name
 * @param value
 * 		the value of the annotation element
 * @return the created/updated annotation
 */
public <A extends Annotation> CtAnnotation<A> annotate(CtElement element, CtTypeReference<A> annotationType, String annotationElementName, Object value) {
    final CtAnnotation<A> annotation = annotate(element, annotationType);
    boolean isArray;
    // try with CT reflection
    CtAnnotationType<A> ctAnnotationType = ((CtAnnotationType<A>) annotation.getAnnotationType().getDeclaration());
    boolean newValue = annotation.getValue(annotationElementName) == null;
    if (ctAnnotationType != null) {
        CtMethod<?> e = ctAnnotationType.getMethod(annotationElementName);
        isArray = (e.getType() instanceof CtArrayTypeReference);
    } else {
        Method m;
        try {
            m = annotation.getAnnotationType().getActualClass().getMethod(annotationElementName, new Class[0]);
        } catch (Exception ex) {
            annotation.addValue(annotationElementName, value);
            return annotation;
        }
        isArray = m.getReturnType().isArray();
    }
    if (isArray || newValue) {
        annotation.addValue(annotationElementName, value);
    } else {
        throw new SpoonException("cannot assign an array to a non-array annotation element");
    }
    return annotation;
}
Also used : SpoonException(spoon.SpoonException) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) SpoonException(spoon.SpoonException)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16