@Override public Set<CtMethod<?>> getMethodsAnnotatedWith(CtTypeReference<?>... annotationTypes) { Set<CtMethod<?>> result = new SignatureBasedSortedSet<>(); for (CtTypeMember typeMember : typeMembers) { if (!(typeMember instanceof CtMethod)) { continue; } CtMethod<?> m = (CtMethod<?>) typeMember; for (CtAnnotation<?> a : m.getAnnotations()) { if (Arrays.asList(annotationTypes).contains(a.getAnnotationType())) { result.add(m); } } } return result; }
public <T> void visitCtMethod(final spoon.reflect.declaration.CtMethod<T> m) { spoon.reflect.declaration.CtMethod other = ((spoon.reflect.declaration.CtMethod) (this.stack.peek())); enter(m); biScan(spoon.reflect.path.CtRole.ANNOTATION, m.getAnnotations(), other.getAnnotations()); biScan(spoon.reflect.path.CtRole.TYPE_PARAMETER, m.getFormalCtTypeParameters(), other.getFormalCtTypeParameters()); biScan(spoon.reflect.path.CtRole.TYPE, m.getType(), other.getType()); biScan(spoon.reflect.path.CtRole.PARAMETER, m.getParameters(), other.getParameters()); biScan(spoon.reflect.path.CtRole.THROWN, m.getThrownTypes(), other.getThrownTypes()); biScan(spoon.reflect.path.CtRole.BODY, m.getBody(), other.getBody()); biScan(spoon.reflect.path.CtRole.COMMENT, m.getComments(), other.getComments()); exit(m); }
public <T> void visitCtMethod(final CtMethod<T> m) { enter(m); scan(CtRole.ANNOTATION, m.getAnnotations()); scan(CtRole.TYPE_PARAMETER, m.getFormalCtTypeParameters()); scan(CtRole.TYPE, m.getType()); scan(CtRole.PARAMETER, m.getParameters()); scan(CtRole.THROWN, m.getThrownTypes()); scan(CtRole.BODY, m.getBody()); scan(CtRole.COMMENT, m.getComments()); exit(m); }
@java.lang.Override public <T> void visitCtMethod(final spoon.reflect.declaration.CtMethod<T> m) { replaceInListIfExist(m.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(m)); replaceInListIfExist(m.getFormalCtTypeParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtFormalTypeDeclarerFormalCtTypeParametersReplaceListener(m)); replaceElementIfExist(m.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(m)); replaceInListIfExist(m.getParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableParametersReplaceListener(m)); replaceInSetIfExist(m.getThrownTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableThrownTypesReplaceListener(m)); replaceElementIfExist(m.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableBodyReplaceListener(m)); replaceInListIfExist(m.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(m)); }
@Override public Set<CtMethod<?>> getMethodsAnnotatedWith(CtTypeReference<?>... annotationTypes) { Set<CtMethod<?>> result = new HashSet<CtMethod<?>>(); for (CtMethod<?> m : methods) { for (CtAnnotation<?> a : m.getAnnotations()) { if (Arrays.asList(annotationTypes).contains(a.getAnnotationType())) { result.add(m); } } } return result; }
/** * Creates a constructor into a target class by copying it from a source * method. * * @param target * the target class * @param source * the method to be copied * @return the new constructor */ @SuppressWarnings("unchecked") public <T> CtConstructor<T> create(CtClass<T> target, CtMethod<?> source) { CtMethod<T> method = (CtMethod<T>) source.clone(); CtConstructor<T> newConstructor = factory.Core().createConstructor(); newConstructor.setAnnotations(method.getAnnotations()); newConstructor.setBody(method.getBody()); newConstructor.setDocComment(method.getDocComment()); newConstructor.setFormalCtTypeParameters(method.getFormalCtTypeParameters()); newConstructor.setModifiers(method.getModifiers()); newConstructor.setParameters(method.getParameters()); target.addConstructor(newConstructor); return newConstructor; }
static <A extends Annotation> void removeAnnotation(CtMethod<?> method, Class<A> annClass) { CtAnnotation<?> toRemove = null; for (CtAnnotation<? extends Annotation> ctAnnotation : method.getAnnotations()) { if (annClass.isAssignableFrom(ctAnnotation.getActualAnnotation().getClass())) { toRemove = ctAnnotation; break; } } if (toRemove != null) method.removeAnnotation(toRemove); }
public <T> void visitCtMethod(final spoon.reflect.declaration.CtMethod<T> m) { spoon.reflect.declaration.CtMethod<T> aCtMethod = m.getFactory().Core().createMethod(); this.builder.copy(m, aCtMethod); aCtMethod.setAnnotations(this.cloneHelper.clone(m.getAnnotations())); aCtMethod.setFormalCtTypeParameters(this.cloneHelper.clone(m.getFormalCtTypeParameters())); aCtMethod.setType(this.cloneHelper.clone(m.getType())); aCtMethod.setParameters(this.cloneHelper.clone(m.getParameters())); aCtMethod.setThrownTypes(this.cloneHelper.clone(m.getThrownTypes())); aCtMethod.setBody(this.cloneHelper.clone(m.getBody())); aCtMethod.setComments(this.cloneHelper.clone(m.getComments())); this.cloneHelper.tailor(m, aCtMethod); this.other = aCtMethod; }
@Override public boolean matches(CtMethod element) { return element.getAnnotations().contains(factory.Annotation().get(AfterClass.class)); } }).first();
@Override public boolean matches(CtMethod element) { return element.getAnnotations().contains(factory.Annotation().get(AfterClass.class)); } }).first();
public void removeAnnotatedMethods(CtClass element){ List<CtMethod> removed = new ArrayList<CtMethod>(); Set<CtMethod<?>> methods = element.getMethods(); for(CtMethod method : methods){ List<CtAnnotation<? extends Annotation>> annotations = method.getAnnotations(); for(CtAnnotation annotation : annotations){ if(UNWATED_ANNOTATIONS.contains(annotation.getActualAnnotation().annotationType().getName())){ removed.add(method); break; } } } for(CtMethod method : removed){ element.removeMethod(method); } }
/** * Clones a method. * * @param method Method to be cloned * @param suffix Suffix for the cloned method's name * @return The cloned method */ public static CtMethod cloneMethod(CtMethod<?> method, String suffix) { CtMethod cloned_method = method.clone(); //rename the clone cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : suffix + cloneNumber)); cloneNumber++; CtAnnotation toRemove = cloned_method.getAnnotations().stream() .filter(annotation -> annotation.toString().contains("Override")) .findFirst().orElse(null); if (toRemove != null) { cloned_method.removeAnnotation(toRemove); } return cloned_method; }
@Override protected boolean isATest(CtMethod<?> candidate) { // checkEnum that the current test class inherit from TestCase final CtType<?> testClass = candidate.getParent(CtType.class); if (testClass == null) { return false; } final CtTypeReference<?> superclassReference = testClass.getSuperclass(); if (superclassReference == null) { return false; } return superclassReference.getQualifiedName().equals(this.qualifiedNameOfAssertClass) && candidate.getAnnotations().isEmpty() && candidate.getSimpleName().startsWith("test"); }
private boolean isTestCase(CtMethod<?> method) { boolean isNamedTest = method.getSimpleName().toLowerCase().endsWith("test"); // to detect TestCase under JUnit 3.x boolean hasTestAnnotation = false; // to detect TestCase under JUnit 4.x List<CtAnnotation<?>> listAnnotation = method.getAnnotations(); for (CtAnnotation<?> tmp : listAnnotation) { if (tmp.getType().equals(method.getFactory().Annotation().createReference(org.junit.Test.class))) { hasTestAnnotation = true; } } return isNamedTest || hasTestAnnotation; }
final Factory factory = InputConfiguration.get().getFactory(); CtAnnotation testAnnotation = clone .getAnnotations() .stream() .filter(annotation -> annotation.toString().contains("Test"))
@Override public boolean isToBeProcessed(CtMethod element){ boolean isTest = false; for(CtAnnotation<? extends Annotation> annotation : element.getAnnotations()){ if (annotation.getType().equals(annotation.getFactory().Annotation().createReference(org.junit.Test.class))) { isTest = true; } } if(!isTest){ return false; //keep this one } for(CtMethod<?> method : keptMethods){ if(element.getSignature().equals(method.getSignature())){ return false; //keep this one } } return super.isToBeProcessed(element); }
@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); }
/** * this method analyze all java class in generatedTestDir and return a list of all Junit method * @param generatedTestFolder * @param classPath * @return List<CtMethod> list of methods */ public static List<CtMethod<?>> getNewTestsMethods(String generatedTestFolder, String classPath){ List<CtMethod<?>> testsMethods = new ArrayList<CtMethod<?>>(); logger.debug("--------------------------------------------------"); logger.debug(" ##### Search tests methods ##### "); Launcher spoon = new Launcher(); spoon.getEnvironment().setAutoImports(true); spoon.addInputResource(generatedTestFolder); spoon.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator)); spoon.buildModel(); //getannotatedMethod.. could be better for(CtType<?> clazz : spoon.getFactory().Class().getAll()){ methodLoop: for(CtMethod<?> method : clazz.getAllMethods()){ for(CtAnnotation<? extends Annotation> annotation : method.getAnnotations()){ if (annotation.getType().equals(method.getFactory().Annotation().createReference(org.junit.Test.class))) { logger.debug("[FOUND] "+method.getSignature()); testsMethods.add(method); continue methodLoop; } } } } return testsMethods; }
public <T> void visitCtMethod(CtMethod<T> m) { enter(m); scan(m.getAnnotations()); scan(m.getType()); scan(m.getParameters()); scanReferences(m.getThrownTypes()); scanReferences(m.getFormalTypeParameters()); scan(m.getBody()); exit(m); }
/** * Creates a constructor into a target class by copying it from a source * method. * * @param target * the target class * @param source * the method to be copied * @return the new constructor */ @SuppressWarnings("unchecked") public <T> CtConstructor<T> create(CtClass<T> target, CtMethod<?> source) { CtMethod<T> method = factory.Core().clone((CtMethod<T>) source); CtConstructor<T> newConstructor = factory.Core().createConstructor(); newConstructor.setAnnotations(method.getAnnotations()); newConstructor.setBody(method.getBody()); newConstructor.setDocComment(method.getDocComment()); newConstructor.setFormalTypeParameters(method.getFormalTypeParameters()); newConstructor.setModifiers(method.getModifiers()); newConstructor.setParameters(method.getParameters()); target.addConstructor(newConstructor); return newConstructor; }