use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.
the class MethodTest method testGetAllMethodsAdaptingType.
@Test
public void testGetAllMethodsAdaptingType() throws Exception {
// contract: AbstractTypingContext should not enter in recursive calls when resolving autoreferenced bounding type
// such as T extends Comparable<? super T>
Launcher l = new Launcher();
l.getEnvironment().setNoClasspath(true);
l.addInputResource("src/test/resources/noclasspath/spring/PropertyComparator.java");
l.buildModel();
CtType<?> propertyComparator = l.getModel().getElements(new NamedElementFilter<CtType>(CtType.class, "PropertyComparator")).get(0);
Set<CtMethod<?>> allMethods = propertyComparator.getAllMethods();
boolean compareFound = false;
for (CtMethod<?> method : allMethods) {
if (method.getSimpleName().equals("compare")) {
assertEquals("compare(T,T)", method.getSignature());
compareFound = true;
}
}
assertTrue(compareFound);
}
use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.
the class MethodTest method testGetAllMethods.
@Test
public void testGetAllMethods() throws Exception {
/* getAllMethods must not throw Exception in no classpath mode */
Launcher l = new Launcher();
l.getEnvironment().setNoClasspath(true);
l.addInputResource("src/test/resources/noclasspath/A3.java");
l.buildModel();
Set<CtMethod<?>> methods = l.getFactory().Class().get("A3").getAllMethods();
assertEquals(1, methods.stream().filter(method -> "foo".equals(method.getSimpleName())).count());
}
use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.
the class MethodOverriddingTest method checkMethodOverride.
private void checkMethodOverride(BiFunction<CtMethod<?>, CtMethod<?>, Boolean> isOverriding) {
Factory factory = ModelUtils.build(new File("src/test/java/spoon/test/method_overriding/testclasses").listFiles());
Map<String, List<CtMethod>> methodsByName = new HashMap<>();
factory.getModel().filterChildren(new TypeFilter<>(CtMethod.class)).forEach((CtMethod m) -> {
List<CtMethod> methods = methodsByName.get(m.getSimpleName());
if (methods == null) {
methods = new ArrayList<>();
methodsByName.put(m.getSimpleName(), methods);
}
methods.add(m);
});
assertTrue(methodsByName.size() > 0);
for (Map.Entry<String, List<CtMethod>> e : methodsByName.entrySet()) {
combine(e.getValue(), 0, isOverriding);
}
}
use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.
the class InsertMethodsTest method testInsertAfter.
@Test
public void testInsertAfter() throws Exception {
CtMethod<Void> foo = (CtMethod<Void>) assignmentClass.getMethods().toArray()[0];
CtBlock<?> body = foo.getBody();
assertEquals(3, body.getStatements().size());
CtStatement s = body.getStatements().get(2);
assertEquals("int z = x + y", s.toString());
// adding a new statement;
CtCodeSnippetStatement stmt = factory.Core().createCodeSnippetStatement();
stmt.setValue("System.out.println(x);");
s.insertAfter(stmt);
assertEquals(4, body.getStatements().size());
assertSame(stmt, body.getStatements().get(3));
assertEquals(body, stmt.getParent());
}
use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.
the class MethodReferenceTest method testGetGenericMethodFromReference.
@Test
public void testGetGenericMethodFromReference() throws Exception {
CtType<?> classCloud = ModelUtils.buildClass(Cloud.class);
CtMethod<?> ctMethod = classCloud.getMethodsByName("method").get(0);
CtExecutableReference<?> execRef = ctMethod.getReference();
Method method = execRef.getActualMethod();
assertNotNull(method);
assertEquals("method", method.getName());
CtClass<?> classSun = classCloud.getFactory().Class().get("spoon.test.methodreference.testclasses.Sun");
// CtExecutableReference<?> execRef2 = classSun.filterChildren(new TypeFilter<>(CtExecutableReference.class)).select(new NameFilter<>("method")).first();
CtExecutableReference<?> execRef2 = classSun.filterChildren(new TypeFilter<>(CtInvocation.class)).select(((CtInvocation i) -> i.getExecutable().getSimpleName().equals("method"))).map((CtInvocation i) -> i.getExecutable()).first();
assertNotNull(execRef2);
Method method2 = execRef2.getActualMethod();
assertNotNull(method2);
assertEquals("method", method2.getName());
}
Aggregations