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
}
}
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;
}
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);
}
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());
}
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)");
}
Aggregations