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());
}
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);
}
}
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());
}
Aggregations