Search in sources :

Example 41 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class LambdaTest method testLambdaExpressionInIfConditional.

@Test
public void testLambdaExpressionInIfConditional() throws Exception {
    final CtLambda<?> lambda = getLambdaInFooByNumber(7);
    assertTypedBy(Predicate.class, lambda.getType());
    assertParametersSizeIs(1, lambda.getParameters());
    final CtParameter<?> parameter = (CtParameter<?>) lambda.getParameters().get(0);
    assertParameterTypedBy(Foo.Person.class, parameter);
    assertParameterIsNamedBy("p", parameter);
    assertHasExpressionBody(lambda);
    final CtMethod<?> method = foo.getElements(new NamedElementFilter<>(CtMethod.class, "m8")).get(0);
    final CtIf condition = method.getElements(new AbstractFilter<CtIf>(CtIf.class) {

        @Override
        public boolean matches(CtIf element) {
            return true;
        }
    }).get(0);
    final String expected = "if (((java.util.function.Predicate<spoon.test.lambda.testclasses.Foo.Person>) (( p) -> (p.age) > 18)).test(new spoon.test.lambda.testclasses.Foo.Person(10))) {" + System.lineSeparator() + "    java.lang.System.err.println(\"Enjoy, you have more than 18.\");" + System.lineSeparator() + "}";
    assertEquals("Condition must be well printed", expected, condition.toString());
}
Also used : AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Foo(spoon.test.lambda.testclasses.Foo) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtParameter(spoon.reflect.declaration.CtParameter) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 42 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class ParameterTest method testMultiParameterLambdaTypeReference.

@Test
@SuppressWarnings("unchecked")
public void testMultiParameterLambdaTypeReference() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/lambdas/MultiParameterLambda.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    List<CtParameter> parameters;
    // test string parameters
    parameters = launcher.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "stringLambda")).get(0).getElements(new TypeFilter<>(CtParameter.class));
    assertEquals(2, parameters.size());
    for (final CtParameter param : parameters) {
        CtTypeReference refType = (CtTypeReference) param.getReference().getType();
        assertEquals(launcher.getFactory().Type().STRING, refType);
    }
    // test integer parameters
    parameters = launcher.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "integerLambda")).get(0).getElements(new TypeFilter<>(CtParameter.class));
    assertEquals(2, parameters.size());
    for (final CtParameter param : parameters) {
        CtTypeReference refType = (CtTypeReference) param.getReference().getType();
        assertEquals(launcher.getFactory().Type().INTEGER, refType);
    }
    // test unknown parameters
    parameters = launcher.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "unknownLambda")).get(0).getElements(new TypeFilter<>(CtParameter.class));
    assertEquals(2, parameters.size());
    for (final CtParameter param : parameters) {
        CtTypeReference refType = (CtTypeReference) param.getReference().getType();
        // unknown parameters have no type
        assertEquals(null, refType);
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 43 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class MethodReferenceTest method testNoClasspathSuperExecutable.

@Test
public void testNoClasspathSuperExecutable() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/noclasspath/superclass/UnknownSuperClass.java");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    final CtTypeReference overrideRef = launcher.getFactory().Annotation().createReference(Override.class);
    // call `getSuperClass()` indirectly using `getOverridingExecutable()`
    // some consistency checks...
    assertEquals(1, model.getElements(new NamedElementFilter<>(CtMethod.class, "a")).size());
    assertEquals(1, model.getElements(new NamedElementFilter<>(CtMethod.class, "b")).size());
    assertEquals(1, model.getElements(new NamedElementFilter<>(CtMethod.class, "toString")).size());
    // get super method of a class not available in classpath
    final CtMethod bMethod = model.getElements(new NamedElementFilter<>(CtMethod.class, "b")).get(0);
    assertNotNull(bMethod.getAnnotation(overrideRef));
    assertNull(bMethod.getReference().getOverridingExecutable());
    // get super method of a class available in classpath (Object)
    final CtMethod toStringMethod = model.getElements(new NamedElementFilter<>(CtMethod.class, "toString")).get(0);
    assertNotNull(toStringMethod.getAnnotation(overrideRef));
    assertNotNull(toStringMethod.getReference().getOverridingExecutable());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)43 Test (org.junit.Test)42 Launcher (spoon.Launcher)19 Factory (spoon.reflect.factory.Factory)17 CtClass (spoon.reflect.declaration.CtClass)10 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)10 CtMethod (spoon.reflect.declaration.CtMethod)8 MainTest (spoon.test.main.MainTest)8 CtIf (spoon.reflect.code.CtIf)7 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)7 CtBlock (spoon.reflect.code.CtBlock)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)6 File (java.io.File)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtElement (spoon.reflect.declaration.CtElement)4 Annotation (java.lang.annotation.Annotation)3 CtLiteral (spoon.reflect.code.CtLiteral)3 CtParameter (spoon.reflect.declaration.CtParameter)3