use of spoon.reflect.reference.CtParameterReference in project spoon by INRIA.
the class ReplaceTest method testReplaceAParameterReferenceToFieldReference.
@Test
public void testReplaceAParameterReferenceToFieldReference() throws Exception {
// contract: replace a parameter reference to a field reference.
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 CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
final CtFieldReference<?> aFieldReference = aTacos.getField("field").getReference();
assertEquals(aParameterReference, variableRead.getVariable());
assertEquals("java.lang.System.err.println(param)", inv.toString());
aParameterReference.replace(aFieldReference);
assertEquals(aFieldReference, variableRead.getVariable());
assertEquals("java.lang.System.err.println(field)", inv.toString());
}
use of spoon.reflect.reference.CtParameterReference in project spoon by INRIA.
the class LambdaTest method testEqualsLambdaParameterRef.
@Test
public void testEqualsLambdaParameterRef() throws Exception {
CtLambda<?> lambda = getLambdaInFooByNumber(8);
CtParameter<?> param = (CtParameter<?>) lambda.getParameters().get(0);
CtParameterReference paramRef1 = param.getReference();
CtParameterReference paramRef2 = lambda.filterChildren(new TypeFilter<>(CtParameterReference.class)).first();
assertTrue(paramRef1.equals(paramRef2));
}
use of spoon.reflect.reference.CtParameterReference in project spoon by INRIA.
the class ParameterTest method testGetParameterReferenceInLambdaNoClasspath.
@Test
public void testGetParameterReferenceInLambdaNoClasspath() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/Tacos.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
CtMethod<?> ctMethod = launcher.getFactory().Type().get("Tacos").getMethodsByName("setStarRatings").get(0);
CtParameter ctParameter = ctMethod.getBody().getStatement(0).getElements(new TypeFilter<CtParameter>(CtParameter.class) {
@Override
public boolean matches(CtParameter element) {
return "entryPair".equals(element.getSimpleName()) && super.matches(element);
}
}).get(0);
assertNotNull(ctParameter.getReference());
List<CtParameterReference> elements = ctMethod.getBody().getStatement(0).getElements(new TypeFilter<CtParameterReference>(CtParameterReference.class) {
@Override
public boolean matches(CtParameterReference element) {
return "entryPair".equals(element.getSimpleName()) && super.matches(element);
}
});
assertEquals(2, elements.size());
for (CtParameterReference element : elements) {
assertEquals(ctParameter, element.getDeclaration());
assertEquals(ctParameter.getReference(), element);
}
}
use of spoon.reflect.reference.CtParameterReference in project spoon by INRIA.
the class ParentTest method testParentOfCtVariableReference.
@Test
public void testParentOfCtVariableReference() throws Exception {
// contract: parent of a variable reference is the element which call getVariable().
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 CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
assertNotNull(aParameterReference.getParent());
assertEquals(variableRead, aParameterReference.getParent());
}
use of spoon.reflect.reference.CtParameterReference in project spoon by INRIA.
the class VariableAccessTest method testDeclaringTypeOfALambdaReferencedByParameterReference.
@Test
public void testDeclaringTypeOfALambdaReferencedByParameterReference() {
final spoon.Launcher launcher = new spoon.Launcher();
launcher.addInputResource("src/test/resources/noclasspath/Foo3.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setComplianceLevel(8);
launcher.buildModel();
launcher.getModel().getElements(new TypeFilter<CtExecutable<?>>(CtExecutable.class) {
@Override
public boolean matches(CtExecutable<?> exec) {
final List<CtParameterReference<?>> guiParams = exec.getParameters().stream().map(CtParameter::getReference).collect(Collectors.toList());
if (guiParams.size() != 1) {
return false;
}
final CtParameterReference<?> param = guiParams.get(0);
exec.getBody().getElements(new TypeFilter<CtParameterReference<?>>(CtParameterReference.class) {
@Override
public boolean matches(CtParameterReference<?> p) {
assertEquals(p.getSimpleName(), param.getSimpleName());
return super.matches(p);
}
});
return super.matches(exec);
}
});
}
Aggregations