/** See doc in {@link CtMethod#copyMethod()} */ public static CtMethod<?> copyMethod(final CtMethod<?> method) { CtMethod<?> clone = method.clone(); StringBuilder tentativeTypeName = new StringBuilder(method.getSimpleName() + "Copy"); CtType parent = method.getParent(CtType.class); while (!parent.getMethodsByName(tentativeTypeName.toString()).isEmpty()) { tentativeTypeName.append("X"); } final String cloneMethodName = tentativeTypeName.toString(); clone.setSimpleName(cloneMethodName); parent.addMethod(clone); new CtScanner() { @Override public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) { CtExecutable<T> declaration = reference.getDeclaration(); if (declaration == null) { return; } if (declaration == method) { reference.setSimpleName(cloneMethodName); } if (reference.getDeclaration() != clone) { throw new SpoonException("post condition broken " + reference); } super.visitCtExecutableReference(reference); } }.scan(clone); return clone; }
.byTemplateParameterReference(paramField); templateType.getMethodsByName(stringMarker).forEach(m -> { parameter(parameterName).byInvocation(m); }); } else if (paramType.isSubtypeOf(ctStatementRef)) { templateType.getMethodsByName(stringMarker).forEach(m -> { parameter(parameterName).setContainerKind(ContainerKind.LIST).byInvocation(m); });
public CtMethod getMethod(CtType<?> ctClass) { if ("none".equals(this.simpleNameMethod)) { return null; } else { if (this.testCase == null) { List<CtMethod<?>> methodsByName = ctClass.getMethodsByName(this.simpleNameMethod); if (methodsByName.isEmpty()) { if (ctClass.getSuperclass() != null) { return getMethod(ctClass.getSuperclass().getDeclaration()); } else { return null; } } this.testCase = methodsByName.get(0); } return this.testCase; } } }
final List<CtMethod<?>> methodsToRemoveInOriginalModel = methodsToRemove.stream() .map(CtMethod::getSimpleName) .map(methodName -> (CtMethod<?>) testClassToBeCompiled.getMethodsByName(methodName).get(0)) .collect(Collectors.toList());
@SuppressWarnings("unchecked") private static List<CtInvocation<?>> buildSnippetAssertCollection(Factory factory, CtMethod<?> testMethod, String expression, Collection value) { final CtVariableAccess variableRead = factory.createVariableRead( factory.createLocalVariableReference().setSimpleName(expression), false ); final CtExecutableReference contains = factory.Type().get(Collection.class).getMethodsByName("contains").get(0).getReference(); return (List<CtInvocation<?>>) value.stream() .limit(Math.min(value.size(), MAX_NUMBER_OF_CHECKED_ELEMENT_IN_LIST)) .map(factory::createLiteral) .map(o -> TestFramework.get().buildInvocationToAssertion( testMethod, AssertEnum.ASSERT_TRUE, Collections.singletonList(factory.createInvocation(variableRead, contains, (CtLiteral) o ) ) ) ) .collect(Collectors.toList()); }
public CtExpression<?> buildInvocationFromString(String invocationAsString) { final CtExpression<?> invocation = this.buildInvocationFromString(invocationAsString, null); if (invocation instanceof CtInvocation<?> && "isEmpty".equals(((CtInvocation) invocation).getExecutable().getSimpleName())) { return invocation; } if (invocationAsString.endsWith("isEmpty()")) { final CtType<?> listCtType = factory.Type() .get(java.util.List.class); final CtMethod<?> isEmpty = listCtType.getMethodsByName("isEmpty").get(0); return factory.createInvocation( invocation, isEmpty.getReference() ); } else { return invocation; } }
@SuppressWarnings("unchecked") private static List<CtInvocation<?>> buildSnippetAssertMap(Factory factory, CtMethod<?> testMethod, String expression, Map value) { final CtVariableAccess variableRead = factory.createVariableRead( factory.createLocalVariableReference().setSimpleName(expression), false ); final CtExecutableReference containsKey = factory.Type().get(Map.class).getMethodsByName("containsKey").get(0).getReference(); final CtExecutableReference get = factory.Type().get(Map.class).getMethodsByName("get").get(0).getReference(); return (List<CtInvocation<?>>) value.keySet().stream() .flatMap(key -> Arrays.stream(new CtInvocation<?>[]{ TestFramework.get().buildInvocationToAssertion(testMethod, AssertEnum.ASSERT_TRUE, Collections.singletonList(factory.createInvocation(variableRead, containsKey, factory.createLiteral(key) ) ) ), TestFramework.get().buildInvocationToAssertion(testMethod, AssertEnum.ASSERT_EQUALS, Arrays.asList(factory.createLiteral(value.get(key)), factory.createInvocation(variableRead, get, factory.createLiteral(key)) ) ) } ) ).collect(Collectors.toList()); }
@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); }
private static CtMethod<?> initializeTestDownAfterClassMethod(Factory factory, CtClass<?> testClass) { final CtType<?> loggerType = factory.Type().get(Logger.class); final CtMethod<?> save = loggerType.getMethodsByName("save").get(0); final CtMethod tearDownAfterClass = factory.createMethod( testClass, new HashSet<>(Arrays.asList(ModifierKind.PUBLIC, ModifierKind.STATIC)), factory.Type().VOID_PRIMITIVE, "tearDownAfterClass", Collections.emptyList(), Collections.emptySet(), factory.createCtBlock( factory.createInvocation(factory.Code().createTypeAccess(loggerType.getReference()), save.getReference())) ); final CtAnnotation annotation = factory.createAnnotation(); final CtTypeReference reference = factory.Type().createReference(AfterClass.class); annotation.setAnnotationType(reference); tearDownAfterClass.addAnnotation(annotation); return tearDownAfterClass; } }
@SuppressWarnings("unchecked") static CtExpression<?> generateSingletonList(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) { final Factory factory = type.getFactory(); final CtType<?> collectionsType = factory.Type().get(Collections.class); final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference()); final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0); final CtExecutableReference executableReference = factory.Core().createExecutableReference(); executableReference.setStatic(true); executableReference.setSimpleName(singletonListMethod.getSimpleName()); executableReference.setDeclaringType(collectionsType.getReference()); executableReference.setType(factory.createCtTypeReference(typeOfCollection)); if (!type.getActualTypeArguments().isEmpty() && type.getActualTypeArguments().stream().allMatch(ValueCreatorHelper::canGenerateAValueForType)) { executableReference.setParameters(type.getActualTypeArguments()); List<CtExpression<?>> parameters = type.getActualTypeArguments().stream() .map(reference -> ValueCreator.generateRandomValue(reference, 0)).collect(Collectors.toList()); return factory.createInvocation(accessToCollections, executableReference, parameters); } else { return factory.createInvocation(accessToCollections, executableReference, factory.createConstructorCall(factory.Type().createReference(Object.class)) ); } }
if (valueCollection.isEmpty()) { final CtInvocation<?> isEmpty = factory.createInvocation(variableRead, factory.Type().get(Collection.class).getMethodsByName("isEmpty").get(0).getReference() ); invocations.add( if (valueCollection.isEmpty()) { final CtInvocation<?> isEmpty = factory.createInvocation(variableRead, factory.Type().get(Map.class).getMethodsByName("isEmpty").get(0).getReference() ); invocations.add(TestFramework.get().buildInvocationToAssertion(
static CtExpression<?> generateEmptyCollection(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) { final Factory factory = type.getFactory(); final CtType<?> collectionsType = factory.Type().get(Collections.class); final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference()); final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0); final CtExecutableReference<?> executableReference = factory.Core().createExecutableReference(); executableReference.setStatic(true); executableReference.setSimpleName(singletonListMethod.getSimpleName()); executableReference.setDeclaringType(collectionsType.getReference()); executableReference.setType(factory.createCtTypeReference(typeOfCollection)); if (type.getActualTypeArguments().isEmpty()) { // supporting Collections.<type>emptyList() executableReference.addActualTypeArgument(type); } else if (type.getActualTypeArguments() .stream() .noneMatch(reference -> reference instanceof CtWildcardReference)){// in case type is a list, we copy the Actual arguments executableReference.setActualTypeArguments(type.getActualTypeArguments()); } return factory.createInvocation(accessToCollections, executableReference); }
private void generateFinalAccessors() { List<CtField<?>> finalFields = root.getMergedClass() .getElements((CtField<?> field) -> field.hasModifier(FINAL)); finalFields.forEach(f -> { CtType<?> declaringType = f.getDeclaringType(); if (declaringType.getMethodsByName(f.getSimpleName()).isEmpty()) { Factory factory = f.getFactory(); CtMethod access = factory.Method().create( declaringType, EnumSet.of(PUBLIC), f.getType(), f.getSimpleName(), emptyList(), emptySet()); access.setParent(declaringType); access.setBody((CtBlock) factory.Core().createBlock()); CtReturn fReturn = factory.Core().createReturn(); fReturn.setReturnedExpression( factory.Code().createVariableRead(f.getReference(), false)); access.getBody().addStatement(fReturn); access.setPosition(new LinedSourcePosition( ((LinedSourcePosition) f.getPosition()).delegate, ((LinedSourcePosition) f.getPosition()).line)); } }); }
@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; } }