Search in sources :

Example 66 with CtMethod

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);
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 67 with CtMethod

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());
}
Also used : Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 68 with CtMethod

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);
    }
}
Also used : HashMap(java.util.HashMap) Factory(spoon.reflect.factory.Factory) List(java.util.List) ArrayList(java.util.ArrayList) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) CtMethod(spoon.reflect.declaration.CtMethod)

Example 69 with CtMethod

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 70 with CtMethod

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());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16