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;
}
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;
}
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);
}
}
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;
}
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());
}
Aggregations