use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class FactoryTest method specificationCoreFactoryCreate.
@Test
public void specificationCoreFactoryCreate() throws Exception {
// contract: all concrete metamodel classes must be instantiable by CoreFactory.create
for (CtType<? extends CtElement> itf : SpoonTestHelpers.getAllInstantiableMetamodelInterfaces()) {
CtElement o = itf.getFactory().Core().create(itf.getActualClass());
assertNotNull(o);
assertTrue(itf.getActualClass().isInstance(o));
}
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class FieldFactoryTest method testCreateFromSource.
@Test
public void testCreateFromSource() throws Exception {
CtClass<?> target = build("spoon.test.testclasses", "SampleClass");
Factory factory = build(Foo.class, Bar.class, SuperClass.class);
final CtClass<Object> type = factory.Class().get(Foo.class);
CtField<?> source = type.getField("i");
FieldFactory ff = type.getFactory().Field();
TypeFactory tf = type.getFactory().Type();
ff.create(target, source);
CtField<?> field = target.getField("i");
Assert.assertEquals("i", field.getSimpleName());
CtTypeReference<?> tref = tf.createReference("int");
Assert.assertEquals(tref, field.getType());
CtElement parent = field.getParent();
Assert.assertTrue(parent instanceof CtClass<?>);
Assert.assertEquals("SampleClass", ((CtClass<?>) parent).getSimpleName());
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class FilterTest method testFilterChildrenWithoutFilterQueryStep.
@Test
public void testFilterChildrenWithoutFilterQueryStep() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
// Contract: the filterChildren(null) without Filter returns same results like filter which returns true for each input.
List<CtElement> list = launcher.getFactory().Package().getRootPackage().filterChildren(null).list();
Iterator<CtElement> iter = list.iterator();
launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
return true;
}).forEach(real -> {
// the elements produced by each query are same
CtElement expected = iter.next();
if (real != expected) {
assertEquals(expected, real);
}
});
assertTrue(list.size() > 0);
assertTrue(iter.hasNext() == false);
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class FilterTest method testEarlyTerminatingQuery.
@Test
public void testEarlyTerminatingQuery() throws Exception {
// contract: a method first evaluates query until first element is found and then terminates the query
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
boolean wasTerminated = false;
void failIfTerminated(String place) {
assertTrue("The " + place + " is called after query was terminated.", wasTerminated == false);
}
}
Context context = new Context();
CtMethod firstMethod = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
context.failIfTerminated("Filter#match of filterChildren");
return true;
}).map((CtElement e) -> {
context.failIfTerminated("Array returning CtFunction#apply of map");
// send result twice to check that second item is skipped
return new CtElement[] { e, e };
}).map((CtElement e) -> {
context.failIfTerminated("List returning CtFunction#apply of map");
// send result twice to check that second item is skipped
return Arrays.asList(new CtElement[] { e, e });
}).map((CtElement e, CtConsumer<Object> out) -> {
context.failIfTerminated("CtConsumableFunction#apply of map");
if (e instanceof CtMethod) {
// this call should pass and cause termination of the query
out.accept(e);
context.wasTerminated = true;
// let it call out.accept(e); once more to check that next step is not called after query is terminated
}
out.accept(e);
}).map(e -> {
context.failIfTerminated("CtFunction#apply of map after CtConsumableFunction");
return e;
}).first(CtMethod.class);
assertTrue(firstMethod != null);
assertTrue(context.wasTerminated);
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class FilterTest method unionOfTwoFilters.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void unionOfTwoFilters() throws Exception {
Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
TypeFilter<CtNewClass> newClassFilter = new TypeFilter<CtNewClass>(CtNewClass.class);
TypeFilter<CtMethod> methodFilter = new TypeFilter<CtMethod>(CtMethod.class);
CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.UNION, methodFilter, newClassFilter);
List filteredWithCompositeFilter = Query.getElements(factory, compositeFilter);
List<CtMethod> methods = Query.getElements(factory, methodFilter);
List<CtNewClass> newClasses = Query.getElements(factory, newClassFilter);
List<CtElement> union = new ArrayList<CtElement>();
union.addAll(methods);
union.addAll(newClasses);
assertEquals(methods.size() + newClasses.size(), union.size());
assertEquals(union.size(), filteredWithCompositeFilter.size());
assertTrue(filteredWithCompositeFilter.containsAll(union));
}
Aggregations