Search in sources :

Example 11 with CtNewClass

use of spoon.reflect.code.CtNewClass in project spoon by INRIA.

the class TypeReferenceTest method testSubTypeAnonymous.

@Test
public void testSubTypeAnonymous() throws Exception {
    CtType<Panini> paniniCtType = buildClass(Panini.class);
    CtClass anonymousClass = ((CtNewClass) ((CtReturn) paniniCtType.getMethod("entryIterator").getBody().getStatement(0)).getReturnedExpression()).getAnonymousClass();
    assertTrue(anonymousClass.getReference().isSubtypeOf(paniniCtType.getFactory().Type().createReference("spoon.test.reference.testclasses.Panini$Itr")));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Panini(spoon.test.reference.testclasses.Panini) Test(org.junit.Test)

Example 12 with CtNewClass

use of spoon.reflect.code.CtNewClass in project spoon by INRIA.

the class NewClassTest method testCtNewClassInNoClasspath.

@Test
public void testCtNewClassInNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/new-class");
    launcher.setSourceOutputDirectory("./target/new-class");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("IndexWriter");
    final List<CtNewClass> ctNewClasses = aClass.getElements(new TypeFilter<>(CtNewClass.class));
    final CtNewClass ctNewClass = ctNewClasses.get(0);
    final CtNewClass secondNewClass = ctNewClasses.get(1);
    final CtClass anonymousClass = ctNewClass.getAnonymousClass();
    assertNotNull(anonymousClass);
    assertNotNull(anonymousClass.getSuperclass());
    assertEquals("With", anonymousClass.getSuperclass().getSimpleName());
    assertEquals("org.apache.lucene.store.Lock$With", anonymousClass.getSuperclass().getQualifiedName());
    assertEquals("Lock", anonymousClass.getSuperclass().getDeclaringType().getSimpleName());
    assertEquals("org.apache.lucene.store.Lock.With", anonymousClass.getSuperclass().toString());
    assertEquals("1", anonymousClass.getSimpleName());
    assertEquals("2", secondNewClass.getAnonymousClass().getSimpleName());
    assertEquals(1, anonymousClass.getMethods().size());
    canBeBuilt("./target/new-class", 8, true);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) 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