use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class CtParameterReferenceImpl method lookupDynamically.
private CtParameter<T> lookupDynamically() {
CtElement element = this;
CtParameter optional = null;
String name = getSimpleName();
try {
do {
CtExecutable executable = element.getParent(CtExecutable.class);
if (executable == null) {
return null;
}
for (CtParameter parameter : (List<CtParameter>) executable.getParameters()) {
if (name.equals(parameter.getSimpleName())) {
optional = parameter;
}
}
element = executable;
} while (optional == null);
} catch (ParentNotInitializedException e) {
return null;
}
return optional;
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class InvocationTest method testIssue1753.
@Test
public void testIssue1753() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/noclasspath/elasticsearch1753");
final CtModel model = launcher.buildModel();
final List<CtExecutable> executables = model.getElements(new TypeFilter<>(CtExecutable.class)).stream().filter(i -> i.getPosition().getLine() == 190).collect(Collectors.toList());
assertEquals(1, executables.size());
final CtExecutable exe = executables.get(0);
assertNotNull(exe.getReference().getDeclaration());
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class MethodsRefactoringTest method testAllMethodsSameSignatureFunction.
@Test
public void testAllMethodsSameSignatureFunction() {
Factory factory = ModelUtils.build(new File("./src/test/java/spoon/test/refactoring/parameter/testclasses"));
// each executable in test classes is marked with a annotation TestHierarchy,
// which defines the name of the hierarchy where this executable belongs to.
// collect all executables which are marked that they belong to hierarchy A_method1
List<CtExecutable<?>> executablesOfHierarchyA = getExecutablesOfHierarchy(factory, "A_method1");
// check executables of this hierarchy
checkMethodHierarchies(executablesOfHierarchyA);
// collect all executables which are marked that they belong to hierarchy R_method1
List<CtExecutable<?>> executablesOfHierarchyR = getExecutablesOfHierarchy(factory, "R_method1");
// check executables of this hierarchy
checkMethodHierarchies(executablesOfHierarchyR);
// contract: CtConstructor has no other same signature
CtConstructor<?> constructorTypeA = factory.Class().get(TypeA.class).getConstructors().iterator().next();
CtExecutable<?> exec = constructorTypeA.map(new AllMethodsSameSignatureFunction()).first();
assertNull("Unexpected executable found by Constructor of TypeA " + exec, exec);
CtConstructor<?> constructorTypeB = factory.Class().get(TypeB.class).getConstructors().iterator().next();
exec = constructorTypeA.map(new AllMethodsSameSignatureFunction()).first();
assertNull("Unexpected executable found by Constructor of TypeA " + exec, exec);
// contract: constructor is returned if includingSelf == true
assertSame(constructorTypeA, constructorTypeA.map(new AllMethodsSameSignatureFunction().includingSelf(true)).first());
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class MethodsRefactoringTest method testExecutableReferenceFilter.
@Test
public void testExecutableReferenceFilter() {
Factory factory = ModelUtils.build(new File("./src/test/java/spoon/test/refactoring/parameter/testclasses"));
List<CtExecutable<?>> executables = factory.getModel().filterChildren((CtExecutable<?> e) -> true).list();
int nrExecRefsTotal = 0;
// contract check that ExecutableReferenceFilter found CtExecutableReferences of each executable individually
for (CtExecutable<?> executable : executables) {
nrExecRefsTotal += checkExecutableReferenceFilter(factory, Collections.singletonList(executable));
}
// contract check that ExecutableReferenceFilter found CtExecutableReferences of all executables together
int nrExecRefsTotal2 = checkExecutableReferenceFilter(factory, executables);
assertSame(nrExecRefsTotal, nrExecRefsTotal2);
// contract check that it found lambdas too
CtLambda lambda = factory.getModel().filterChildren((CtLambda<?> e) -> true).first();
assertNotNull(lambda);
// this test case is quite wild, because there is normally lambda reference in spoon model. So make one lambda reference here:
CtExecutableReference<?> lambdaRef = lambda.getReference();
List<CtExecutableReference<?>> refs = lambdaRef.filterChildren(null).select(new ExecutableReferenceFilter(lambda)).list();
assertEquals(1, refs.size());
assertSame(lambdaRef, refs.get(0));
}
use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.
the class Refactoring method copyMethod.
/**
* See doc in {@link CtMethod#copyMethod()}
*/
public static CtMethod<?> copyMethod(final CtMethod<?> method) {
CtMethod<?> clone = method.clone();
String tentativeTypeName = method.getSimpleName() + "Copy";
CtType parent = method.getParent(CtType.class);
while (parent.getMethodsByName(tentativeTypeName).size() > 0) {
tentativeTypeName += "X";
}
final String cloneMethodName = tentativeTypeName;
clone.setSimpleName(cloneMethodName);
parent.addMethod(clone);
new CtScanner() {
@Override
public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) {
CtExecutable<T> declaration = reference.getDeclaration();
if (declaration == null) {
return;
}
if (declaration == method) {
reference.setSimpleName(cloneMethodName);
}
if (reference.getDeclaration() != clone) {
throw new SpoonException("post condition broken " + reference);
}
super.visitCtExecutableReference(reference);
}
}.scan(clone);
return clone;
}
Aggregations