Search in sources :

Example 1 with CtNewClass

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;
        }
    });
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtNewClass(spoon.reflect.code.CtNewClass) Foo(spoon.test.constructorcallnewclass.testclasses.Foo) Factory(spoon.reflect.factory.Factory) Before(org.junit.Before)

Example 2 with CtNewClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Factory(spoon.reflect.factory.Factory) Foo2(spoon.test.constructorcallnewclass.testclasses.Foo2) Test(org.junit.Test)

Example 3 with CtNewClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 4 with CtNewClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 5 with CtNewClass

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));
}
Also used : CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtNewClass(spoon.reflect.code.CtNewClass) List(java.util.List) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtNewClass (spoon.reflect.code.CtNewClass)12 Test (org.junit.Test)10 CtClass (spoon.reflect.declaration.CtClass)8 Launcher (spoon.Launcher)6 Factory (spoon.reflect.factory.Factory)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 MainTest (spoon.test.main.MainTest)3 CtModel (spoon.reflect.CtModel)2 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Before (org.junit.Before)1 SpoonModelBuilder (spoon.SpoonModelBuilder)1 CtReturn (spoon.reflect.code.CtReturn)1 CtElement (spoon.reflect.declaration.CtElement)1 CtInterface (spoon.reflect.declaration.CtInterface)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)1 CompositeFilter (spoon.reflect.visitor.filter.CompositeFilter)1