Search in sources :

Example 1 with Factory

use of spoon.reflect.factory.Factory 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 Factory

use of spoon.reflect.factory.Factory in project Ex2Amplifier by STAMP-project.

the class MainGenerator method wrapInTryCatch.

private static CtTry wrapInTryCatch(CtStatement statementToBeWrapped, CtTypeReference exceptionType) {
    final Factory factory = statementToBeWrapped.getFactory();
    final CtTry aTry = factory.createTry();
    aTry.setBody(statementToBeWrapped);
    addCatchGivenExceptionToTry(exceptionType, factory, aTry, "");
    return aTry;
}
Also used : Factory(spoon.reflect.factory.Factory) CtTry(spoon.reflect.code.CtTry)

Example 3 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class SnippetCompilationHelper method compileAndReplaceSnippetsIn.

public static void compileAndReplaceSnippetsIn(CtType<?> c) {
    Factory f = c.getFactory();
    CtType<?> workCopy = c;
    Set<ModifierKind> backup = EnumSet.noneOf(ModifierKind.class);
    backup.addAll(workCopy.getModifiers());
    workCopy.removeModifier(ModifierKind.PUBLIC);
    try {
        build(f, workCopy.toString());
    } finally {
        // restore modifiers
        c.setModifiers(backup);
    }
}
Also used : ModifierKind(spoon.reflect.declaration.ModifierKind) Factory(spoon.reflect.factory.Factory)

Example 4 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class SnippetCompilationHelper method internalCompileStatement.

private static CtStatement internalCompileStatement(CtElement st, CtTypeReference returnType) {
    Factory f = st.getFactory();
    String contents = createWrapperContent(st, f, returnType);
    build(f, contents);
    CtType<?> c = f.Type().get(WRAPPER_CLASS_NAME);
    // Get the part we want
    CtMethod<?> wrapper = c.getMethod(WRAPPER_METHOD_NAME);
    List<CtStatement> statements = wrapper.getBody().getStatements();
    CtStatement ret = statements.get(statements.size() - 1);
    // Clean up
    c.getPackage().removeType(c);
    if (ret instanceof CtClass) {
        CtClass klass = (CtClass) ret;
        ret.getFactory().Package().getRootPackage().addType(klass);
        klass.setSimpleName(klass.getSimpleName().replaceAll("^[0-9]*", ""));
    }
    return ret;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory)

Example 5 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class CommentTest method testCommentsInComment1And2.

@Test
public void testCommentsInComment1And2() {
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(Comment1.class);
    List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
    assertEquals(4, comments.size());
    type = (CtClass<?>) f.Type().get(Comment2.class);
    comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
    assertEquals(2, comments.size());
    CtComment commentD = comments.get(1);
    assertEquals("D", commentD.getContent());
}
Also used : Comment1(spoon.test.comment.testclasses.Comment1) CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19