@Override void insert(CtBlock<?> block, CtStatementList statementsToBeInserted) { block.insertEnd(statementsToBeInserted); }
private void addAtCorrectPlace(String id, CtStatement lastStmt, CtStatement assertStatement, CtStatement statementToBeAsserted) { if (id.endsWith("end")) { statementToBeAsserted.getParent(CtBlock.class).insertEnd(assertStatement); } else { lastStmt.insertAfter(assertStatement); } } }
public static void insertAfterUnderSameParent(CtStatement toBeInserted, CtStatement insertionPoint) { CtElement parent; if (isBlock(insertionPoint)) { CtBlock<?> block = (CtBlock<?>) insertionPoint; block.insertEnd(toBeInserted); parent = block; } else { insertionPoint.insertAfter(toBeInserted); parent = insertionPoint.getParent(); } setParent(parent, toBeInserted); }
@Override public void generateAfterClassToSaveObservations(CtType<?> testClass, List<CtMethod<?>> testsToRun) { // get AfterClassMethod is exist otherwise use initAfterClassMethod final Factory factory = testClass.getFactory(); final CtMethod<?> afterClassMethod = testClass.getMethods() .stream() .filter(method -> method.getAnnotations() .stream() .anyMatch(ctAnnotation -> this.getFullQualifiedNameOfAnnotationAfterClass().equals(ctAnnotation.getAnnotationType().getQualifiedName()) ) ).findFirst() .orElse(initAfterClassMethod(factory)); final CtTypeReference<?> ctTypeReference = factory.createCtTypeReference(ObjectLog.class); final CtExecutableReference<?> reference = ctTypeReference .getTypeDeclaration() .getMethodsByName("save") .get(0) .getReference(); afterClassMethod.getBody().insertEnd( factory.createInvocation(factory.createTypeAccess(ctTypeReference), reference) ); testClass.addMethod(afterClassMethod); }
CtInvocation newInvocation = factory.Code().createInvocation(targetClone, methodToInvokeToAdd.getReference(), arguments); body.insertEnd(newInvocation); return methodClone;
@SuppressWarnings("unchecked") private static CtElement addSaveStatementInTearDownAfterClass(CtClass<?> testClass) { final Factory factory = testClass.getFactory(); CtMethod<?> testDownAfterClass = testClass.filterChildren(new TypeFilter<CtMethod>(CtMethod.class) { @Override public boolean matches(CtMethod element) { return element.getAnnotations().contains(factory.Annotation().get(AfterClass.class)); } }).first(); boolean methodCreated = false; if (testDownAfterClass == null) { methodCreated = true; testDownAfterClass = initializeTestDownAfterClassMethod(factory, testClass); } final CtType<?> loggerType = factory.Type().get(Logger.class); final CtMethod<?> save = loggerType.getMethodsByName("save").get(0); CtInvocation invocation = factory.createInvocation(factory.Code().createTypeAccess(loggerType.getReference()), save.getReference()); testDownAfterClass.getBody().insertEnd( invocation ); if (methodCreated) { return testDownAfterClass; } else { return invocation; } }
ifInit.setThenStatement(closeDependants); initStageMethodBody.insertEnd(ifInit); }); });
stmt.getParent(CtBlock.class).insertEnd(invocationToObjectLogAtTheEnd);