Search in sources :

Example 16 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class ReplaceTest method testReplaceExecutableReferenceByAnotherOne.

@Test
public void testReplaceExecutableReferenceByAnotherOne() throws Exception {
    // contract: replace an executable reference to another one in an invocation.
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    final CtExecutableReference oldExecutable = inv.getExecutable();
    final CtExecutableReference<Object> newExecutable = factory.Executable().createReference("void java.io.PrintStream#print(java.lang.String)");
    assertSame(oldExecutable, inv.getExecutable());
    oldExecutable.replace(newExecutable);
    assertSame(newExecutable, inv.getExecutable());
    assertEquals("print(java.lang.String)", inv.getExecutable().toString());
    assertEquals("java.io.PrintStream", inv.getExecutable().getDeclaringType().toString());
    // contract: replace of single value by multiple values in single value field must fail
    try {
        newExecutable.replace(Arrays.asList(oldExecutable, null));
        fail();
    } catch (InvalidReplaceException e) {
    // OK
    }
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Tacos(spoon.test.replace.testclasses.Tacos) InvalidReplaceException(spoon.support.visitor.replace.InvalidReplaceException) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 17 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class MethodsRefactoringTest method checkExecutableReferenceFilter.

private int checkExecutableReferenceFilter(Factory factory, List<CtExecutable<?>> executables) {
    assertTrue(executables.size() > 0);
    ExecutableReferenceFilter execRefFilter = new ExecutableReferenceFilter();
    executables.forEach((CtExecutable<?> e) -> execRefFilter.addExecutable(e));
    final List<CtExecutableReference<?>> refs = new ArrayList<>(factory.getModel().filterChildren(execRefFilter).list());
    int nrExecRefs = refs.size();
    // use different (slower, but straight forward) algorithm to search for all executable references to check if ExecutableReferenceFilter returns correct results
    factory.getModel().filterChildren((CtExecutableReference er) -> {
        return containsSame(executables, er.getDeclaration());
    }).forEach((CtExecutableReference er) -> {
        // check that each expected reference was found by ExecutableReferenceFilter and remove it from that list
        assertTrue("Executable reference: " + er + " not found.", refs.remove(er));
    });
    // check that no other reference was found by ExecutableReferenceFilter
    assertSame(0, refs.size());
    return nrExecRefs;
}
Also used : ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 18 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class ElasticsearchStackoverflowTest method testStackOverflow.

@Test
public void testStackOverflow() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/elasticsearch-stackoverflow");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    CtModel model = launcher.getModel();
    Scanner scanner = new Scanner();
    scanner.scan(model.getRootPackage());
    List<CtExecutableReference> executables = launcher.getModel().getElements(new TypeFilter<CtExecutableReference>(CtExecutableReference.class));
    assertFalse(executables.isEmpty());
    boolean result = false;
    for (CtExecutableReference execRef : executables) {
        if (execRef.getSimpleName().equals("setParentTask")) {
            CtTypeReference typeRef = execRef.getDeclaringType();
            assertTrue(typeRef instanceof CtTypeParameterReference);
            assertEquals("ShardRequest", typeRef.getSimpleName());
            CtType typeRefDecl = typeRef.getDeclaration();
            assertEquals("BroadcastShardRequest", typeRefDecl.getSuperclass().getSimpleName());
            assertNull(execRef.getDeclaration());
            result = true;
        }
    }
    assertTrue(result);
}
Also used : CtScanner(spoon.reflect.visitor.CtScanner) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 19 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class ExecutableReferenceGenericTest method testMultiReferenceBetweenMethodsWithGenericInSameClass.

@Test
public void testMultiReferenceBetweenMethodsWithGenericInSameClass() throws Exception {
    final CtClass<?> clazz = getCtClassByName("MyClass");
    CtMethod<?> method2 = getCtMethodByNameFromCtClass(clazz, "method2");
    List<CtExecutableReference<?>> refsMethod2 = getReferencesOfAMethod(method2);
    CtMethod<?> expectedMethod1 = getCtMethodByNameFromCtClass(clazz, "method1");
    CtMethod<?> expectedMethod5 = getCtMethodByNameFromCtClass(clazz, "method5");
    assertEquals(3, refsMethod2.size());
    CtExecutable execRefsMethods2 = refsMethod2.get(0).getDeclaration();
    // T has more information in the invocation than its declaration because of the argument type
    // assertEquals(expectedMethod1, refsMethod2.get(0).getDeclaration());
    assertEquals("method1(T extends java.lang.String)", execRefsMethods2.getSignature());
    assertEquals(expectedMethod1, refsMethod2.get(1).getDeclaration());
    assertEquals(expectedMethod5, refsMethod2.get(2).getDeclaration());
}
Also used : CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Example 20 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class ExecutableReferenceGenericTest method testOneReferenceWithGenericMethodOutOfTheClass.

@Test
public void testOneReferenceWithGenericMethodOutOfTheClass() throws Exception {
    final CtClass<?> clazz = getCtClassByName("MyClass");
    final CtClass<?> clazz2 = getCtClassByName("MyClass2");
    CtMethod<?> methodA = getCtMethodByNameFromCtClass(clazz2, "methodA");
    List<CtExecutableReference<?>> refsMethodA = getReferencesOfAMethod(methodA);
    CtMethod<?> expectedMethod1 = getCtMethodByNameFromCtClass(clazz, "method1");
    assertEquals(1, refsMethodA.size());
    CtExecutable execRefsMethods2 = refsMethodA.get(0).getDeclaration();
    // T has more information in the invocation than its declaration because of the argument type
    // assertEquals(expectedMethod1, refsMethodA.get(0).getDeclaration());
    assertEquals(execRefsMethods2.getSignature(), "method1(T extends java.lang.String)");
}
Also used : CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Aggregations

CtExecutableReference (spoon.reflect.reference.CtExecutableReference)27 Test (org.junit.Test)13 CtInvocation (spoon.reflect.code.CtInvocation)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)9 Factory (spoon.reflect.factory.Factory)8 Launcher (spoon.Launcher)7 CtMethod (spoon.reflect.declaration.CtMethod)7 ArrayList (java.util.ArrayList)5 CtElement (spoon.reflect.declaration.CtElement)5 CtExecutable (spoon.reflect.declaration.CtExecutable)5 CtExpression (spoon.reflect.code.CtExpression)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 List (java.util.List)3 CtType (spoon.reflect.declaration.CtType)3 CtReference (spoon.reflect.reference.CtReference)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 HashSet (java.util.HashSet)2 CtFieldAccess (spoon.reflect.code.CtFieldAccess)2 CtLiteral (spoon.reflect.code.CtLiteral)2 CtStatement (spoon.reflect.code.CtStatement)2