use of spoon.reflect.code.CtNewClass in project spoon by INRIA.
the class NewClassTest method setUp.
@Before
public void setUp() throws Exception {
final Factory build = build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
newClasses = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return true;
}
});
}
use of spoon.reflect.code.CtNewClass in project spoon by INRIA.
the class NewClassTest method testMoreThan9NewClass.
@Test
public void testMoreThan9NewClass() throws Exception {
final Factory build = build(Foo2.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo2.class);
List<CtNewClass<?>> elements = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return true;
}
});
assertEquals(13, elements.size());
assertEquals(Foo2.class.getCanonicalName() + "$12", elements.get(11).getAnonymousClass().getQualifiedName());
assertEquals(Foo2.class.getCanonicalName() + "$12$1", elements.get(12).getAnonymousClass().getQualifiedName());
}
use of spoon.reflect.code.CtNewClass in project spoon by INRIA.
the class CtClassTest method testCloneAnonymousClassInvocationWithAutoimports.
@Test
public void testCloneAnonymousClassInvocationWithAutoimports() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when using autoimport
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(true);
launcher.buildModel();
CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();
CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
// The test stops failing if we set the parent below
// newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());
assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
use of spoon.reflect.code.CtNewClass in project spoon by INRIA.
the class CtClassTest method testCloneAnonymousClassInvocation.
@Test
public void testCloneAnonymousClassInvocation() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when not using autoimport
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(false);
launcher.buildModel();
CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();
CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
// The test stops failing if we set the parent below
// newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());
assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
use of spoon.reflect.code.CtNewClass 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