Search in sources :

Example 1 with CtParameterReference

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());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtParameterReference(spoon.reflect.reference.CtParameterReference) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtVariableRead(spoon.reflect.code.CtVariableRead) Test(org.junit.Test)

Example 2 with CtParameterReference

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));
}
Also used : CtParameterReference(spoon.reflect.reference.CtParameterReference) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 3 with CtParameterReference

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);
    }
}
Also used : CtParameterReference(spoon.reflect.reference.CtParameterReference) Launcher(spoon.Launcher) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 4 with CtParameterReference

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());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtParameterReference(spoon.reflect.reference.CtParameterReference) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtVariableRead(spoon.reflect.code.CtVariableRead) Test(org.junit.Test)

Example 5 with CtParameterReference

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);
        }
    });
}
Also used : Launcher(spoon.Launcher) CtParameterReference(spoon.reflect.reference.CtParameterReference) Launcher(spoon.Launcher) List(java.util.List) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Aggregations

CtParameterReference (spoon.reflect.reference.CtParameterReference)6 Test (org.junit.Test)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)5 CtParameter (spoon.reflect.declaration.CtParameter)4 Factory (spoon.reflect.factory.Factory)3 List (java.util.List)2 Launcher (spoon.Launcher)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtVariableRead (spoon.reflect.code.CtVariableRead)2 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)2 Tacos (spoon.test.replace.testclasses.Tacos)2 Utils (eu.stamp.project.ex2amplifier.Utils)1 Ex2Amplifier (eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier)1 AssertionRemover (fr.inria.diversify.dspot.assertGenerator.AssertionRemover)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CtAbstractInvocation (spoon.reflect.code.CtAbstractInvocation)1 CtUnaryOperator (spoon.reflect.code.CtUnaryOperator)1 CtVariableAccess (spoon.reflect.code.CtVariableAccess)1