public Set<ModifierKind> getModifiers() { CtExecutable<T> e = getDeclaration(); if (e != null) { if (e instanceof CtMethod) { return ((CtMethod<T>) e).getModifiers(); } else if (e instanceof CtConstructor) { return ((CtConstructor<T>) e).getModifiers(); } return CtElementImpl.emptySet(); } Method m = getActualMethod(); if (m != null) { return RtHelper.getModifiers(m.getModifiers()); } Constructor<?> c = getActualConstructor(); if (c != null) { return RtHelper.getModifiers(c.getModifiers()); } return Collections.emptySet(); }
/** * Gets all the main methods stored in this factory. */ public Collection<CtMethod<Void>> getMainMethods() { Collection<CtMethod<Void>> methods = new ArrayList<>(); for (CtType<?> t : factory.Type().getAll()) { if (t instanceof CtClass) { CtMethod<Void> m = ((CtClass<?>) t).getMethod(factory.Type().createReference(void.class), "main", factory.Type().createArrayReference(factory.Type().createReference(String.class))); if ((m != null) && m.getModifiers().contains(ModifierKind.STATIC)) { methods.add(m); } } } return methods; }
public <T> void visitCtMethod(spoon.reflect.declaration.CtMethod<T> e) { ((spoon.reflect.declaration.CtMethod<T>) (other)).setDefaultMethod(e.isDefaultMethod()); ((spoon.reflect.declaration.CtMethod<T>) (other)).setModifiers(e.getModifiers()); ((spoon.reflect.declaration.CtMethod<T>) (other)).setShadow(e.isShadow()); super.visitCtMethod(e); }
@Override public void addMethod(CtMethod ctMethod) { final CtAnnotationMethod<Object> field = factory.Core().createAnnotationMethod(); field.setSimpleName(ctMethod.getSimpleName()); field.setModifiers(ctMethod.getModifiers()); field.setType(ctMethod.getType()); field.setShadow(true); ctAnnotationType.addMethod(field); } });
/** * 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; }
@Override public boolean matches(CtMethod<?> element) { return element.getModifiers().contains(ModifierKind.STATIC) && Arrays.stream(NAME_OF_FACTORY_METHOD) .anyMatch(element.getSimpleName().toLowerCase()::contains) && element.getType().equals(type) && element.getParameters().stream() .map(CtParameter::getType) .allMatch(ValueCreatorHelper::canGenerateAValueForType); } }
adaptedMethod.setModifiers(method.getModifiers()); adaptedMethod.setSimpleName(method.getSimpleName());
public static List<CtMethod<?>> findMethodsWithTargetType(CtTypeReference<?> type) { if (type == null || type.getTypeDeclaration() == null) { return Collections.emptyList(); } else { return type.getTypeDeclaration().getMethods().stream() .filter(method -> method.getModifiers().contains(ModifierKind.PUBLIC)) // TODO checks this predicate // TODO we could also access to method with default or protected modifiers .filter(method -> !method.getModifiers().contains(ModifierKind.STATIC)) // TODO checks this predicate // TODO we can't amplify test on full static classes with this predicate .filter(method -> !method.getModifiers().contains(ModifierKind.ABSTRACT)) // TODO checks this predicate // TODO maybe we would like to call of abstract method, since the abstract would be implemented // TODO inherited classes. However, the semantic of the test to be amplified may be to test the abstract class .filter(method -> method.getParameters() .stream() .map(CtParameter::getType) .allMatch(ValueCreatorHelper::canGenerateAValueForType) ).collect(Collectors.toList()); } }
public Set<ModifierKind> getModifiers() { CtExecutable<T> e = getDeclaration(); if (e != null) { if (e instanceof CtMethod) { return ((CtMethod<T>) e).getModifiers(); } else if (e instanceof CtConstructor) { return ((CtConstructor<T>) e).getModifiers(); } return CtElementImpl.emptySet(); } Method m = getActualMethod(); if (m != null) { return RtHelper.getModifiers(m.getModifiers()); } Constructor<?> c = getActualConstructor(); if (c != null) { return RtHelper.getModifiers(c.getModifiers()); } return new TreeSet<ModifierKind>(); }
/** * Gets all the main methods stored in this factory. */ public Collection<CtMethod<Void>> getMainMethods() { Collection<CtMethod<Void>> methods = new ArrayList<CtMethod<Void>>(); for (CtType<?> t : factory.Type().getAll()) { if (t instanceof CtClass) { CtMethod<Void> m = ((CtClass<?>) t).getMethod(factory.Type().createReference(void.class), "main", factory.Type().createArrayReference(factory.Type().createReference(String.class))); if ((m != null) && m.getModifiers().contains(ModifierKind.STATIC)) { methods.add(m); } } } return methods; }
if (parentMethod != null) { if (parentMethod.getModifiers().contains(ModifierKind.STATIC)) { modifiers.add(ModifierKind.STATIC); invocationTarget = MutationSupporter.getFactory().createTypeAccess(target.getReference());
/** * 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; }