use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationInNewInstance.
@Test
public void testUsageOfTypeAnnotationInNewInstance() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtConstructorCall<?> ctConstructorCall = ctClass.getElements(new AbstractFilter<CtConstructorCall<?>>(CtConstructorCall.class) {
@Override
public boolean matches(CtConstructorCall<?> element) {
return "String".equals(element.getType().getSimpleName());
}
}).get(0);
final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctConstructorCall.getType().getAnnotations();
assertEquals("Type of the new class must use an annotation", 1, typeAnnotations.size());
assertEquals("Type of the new class is typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
assertEquals("New class with an type annotation must be well printed", "new java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String()", ctConstructorCall.toString());
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class ConstructorCallTest method setUp.
@Before
public void setUp() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/" + Foo.class.getCanonicalName().replace(".", "/") + ".java");
launcher.addInputResource("./src/test/java/" + Panini.class.getCanonicalName().replace(".", "/") + ".java");
launcher.setSourceOutputDirectory("./target/spooned");
launcher.run();
final Factory factory = launcher.getFactory();
final CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
TreeSet ts = new TreeSet(new DeepRepresentationComparator());
ts.addAll(foo.getElements(new AbstractFilter<CtConstructorCall<?>>(CtConstructorCall.class) {
@Override
public boolean matches(CtConstructorCall<?> element) {
return true;
}
}));
constructorCalls = new ArrayList(ts);
final CtType<Panini> panini = factory.Type().get(Panini.class);
constructorCallsPanini = panini.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class));
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class TryCatchTest method testMultiTryCatchWithCustomExceptions.
@Test
public void testMultiTryCatchWithCustomExceptions() throws Exception {
final Launcher launcher = new Launcher();
final SpoonModelBuilder compiler = launcher.createCompiler();
compiler.addInputSource(new File("./src/test/java/spoon/test/trycatch/testclasses/"));
compiler.build();
Factory factory = compiler.getFactory();
final CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
final CtCatch ctCatch = foo.getElements(new AbstractFilter<CtCatch>(CtCatch.class) {
@Override
public boolean matches(CtCatch element) {
return true;
}
}).get(0);
final String expected = "catch (spoon.test.trycatch.testclasses.internal.MyException | spoon.test.trycatch.testclasses.internal.MyException2 ignore) {" + System.lineSeparator() + "}";
assertEquals(expected, ctCatch.toString());
}
use of spoon.reflect.visitor.filter.AbstractFilter 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.AbstractFilter in project spoon by INRIA.
the class ParentTest method testGetParentWithFilter.
@Test
public void testGetParentWithFilter() throws Exception {
// addType should set Parent
CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
CtMethod<Object> m = clazz.getMethod("m");
// get three = "" in one = two = three = "";
CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return element.getParent() instanceof CtStatementList && super.matches(element);
}
});
// the filter has to return one = two = three = ""
assertEquals(m.getBody().getStatement(3), ctStatement);
m = clazz.getMethod("internalClass");
CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return element instanceof CtLocalVariable && super.matches(element);
}
}).get(0);
// get the top class
ctStatement1.getParent(CtType.class);
CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {
@Override
public boolean matches(CtType element) {
return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
}
});
assertEquals(clazz, parent);
assertNotEquals(ctStatement1.getParent(CtType.class), parent);
// not present element
CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
assertEquals(null, ctWhile);
CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return true;
}
});
// getParent must not return the current element
assertNotEquals(statement, statementParent);
}
Aggregations