use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class MethodsRefactoringTest method testCtParameterRemoveRefactoringValidationCheck.
@Test
public void testCtParameterRemoveRefactoringValidationCheck() throws FileNotFoundException {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/" + testPackagePath)));
comp.build();
Factory factory = comp.getFactory();
CtType<?> typeR = factory.Class().get(TypeR.class);
CtMethod<?> methodTypeR_method1 = typeR.getMethodsByName("method1").get(0);
CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring().setTarget(methodTypeR_method1.getParameters().get(0));
refactor.setTarget(methodTypeR_method1.getParameters().get(0));
// check that each to be refactored method has one parameter
List<CtExecutable<?>> execs = refactor.getTargetExecutables();
execs.forEach(exec -> {
// check that each to be modified method has one parameter
assertEquals(1, exec.getParameters().size());
});
// try refactor
try {
refactor.refactor();
fail();
} catch (RefactoringException e) {
this.getClass();
}
}
use of spoon.reflect.declaration.CtExecutable 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.declaration.CtExecutable in project spoon by INRIA.
the class MethodsRefactoringTest method testCtParameterRemoveRefactoring.
@Test
public void testCtParameterRemoveRefactoring() throws FileNotFoundException {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/" + testPackagePath)));
comp.build();
Factory factory = comp.getFactory();
CtType<?> typeA = factory.Class().get(TypeA.class);
CtMethod<?> methodTypeA_method1 = typeA.getMethodsByName("method1").get(0);
CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring();
refactor.setTarget(methodTypeA_method1.getParameters().get(0));
// check that expected methods are targets of refactoring
List<CtExecutable<?>> execs = refactor.getTargetExecutables();
execs.forEach(exec -> {
// check that each to be modified method has one parameter
assertEquals(1, exec.getParameters().size());
});
refactor.refactor();
execs.forEach(exec -> {
// check that each to be modified method has no parameter after refactoring
assertEquals(0, exec.getParameters().size());
});
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt("./target/spooned/" + testPackagePath, 8);
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class MethodsRefactoringTest method checkMethodHierarchy.
private void checkMethodHierarchy(List<CtExecutable<?>> expectedExecutables, CtExecutable startExecutable) {
// contract: check that by default it does not includes self
// contract: check that by default it returns lambdas
{
final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction()).list();
assertFalse("Unexpected start executable " + startExecutable, containsSame(executables, startExecutable));
// check that some method was found
assertTrue(executables.size() > 0);
// check that expected methods were found and remove them
expectedExecutables.forEach(m -> {
boolean found = removeSame(executables, m);
if (startExecutable == m) {
// it is start method. It should not be there
assertFalse("The signature " + getQSignature(m) + " was returned too", found);
} else {
assertTrue("The signature " + getQSignature(m) + " not found", found);
}
});
// check that there is no unexpected executable
assertTrue("Unexpected executables: " + executables, executables.isEmpty());
}
// contract: check that includingSelf(true) returns startMethod too
// contract: check that by default it still returns lambdas
{
final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true)).list();
assertTrue("Missing start executable " + startExecutable, containsSame(executables, startExecutable));
// check that some method was found
assertTrue(executables.size() > 0);
// check that expected methods were found and remove them
expectedExecutables.forEach(m -> {
assertTrue("The signature " + getQSignature(m) + " not found", removeSame(executables, m));
});
// check that there is no unexpected executable
assertTrue("Unexpected executables: " + executables, executables.isEmpty());
}
// contract: check that includingLambdas(false) returns no lambda expressions
{
final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true).includingLambdas(false)).list();
if (startExecutable instanceof CtLambda) {
// lambda must not be returned even if it is first
assertFalse("Unexpected start executable " + startExecutable, containsSame(executables, startExecutable));
} else {
assertTrue("Missing start executable " + startExecutable, containsSame(executables, startExecutable));
}
// check that some method was found
assertTrue(executables.size() > 0);
// check that expected methods were found and remove them
expectedExecutables.forEach(m -> {
if (m instanceof CtLambda) {
// the lambdas are not expected. Do not ask for them
return;
}
assertTrue("The signature " + getQSignature(m) + " not found", removeSame(executables, m));
});
// check that there is no unexpected executable or lambda
assertTrue("Unexepcted executables " + executables, executables.isEmpty());
}
// contract: check early termination
// contract: check that first returned element is the startExecutable itself if includingSelf == true
CtExecutable<?> exec = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true)).first();
assertSame(startExecutable, exec);
// contract: check that first returned element is not the startExecutable itself if includingSelf == false, but some other executable from the expected
exec = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(false)).first();
assertNotSame(startExecutable, exec);
assertTrue(containsSame(expectedExecutables, exec));
}
use of spoon.reflect.declaration.CtExecutable 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());
}
Aggregations