Search in sources :

Example 96 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CtBodyHolderTest method testWhileWithBlock.

@Test
public void testWhileWithBlock() throws Exception {
    Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
    CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
    assertEquals(2, cwbClass.getMethods().size());
    CtMethod<?> method = cwbClass.getMethod("method2");
    CtBlock<?> methodBody = method.getBody();
    assertTrue(methodBody.getStatement(3) instanceof CtWhile);
    CtWhile whileStmnt = (CtWhile) methodBody.getStatement(3);
    checkCtBody(whileStmnt, "while_block", 0);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) CtWhile(spoon.reflect.code.CtWhile) Test(org.junit.Test)

Example 97 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CtBodyHolderTest method testForWithBlock.

@Test
public void testForWithBlock() throws Exception {
    Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
    CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
    assertEquals(2, cwbClass.getMethods().size());
    CtMethod<?> method = cwbClass.getMethod("method2");
    CtBlock<?> methodBody = method.getBody();
    assertTrue(methodBody.getStatement(2) instanceof CtFor);
    CtFor forStmnt = (CtFor) methodBody.getStatement(2);
    checkCtBody(forStmnt, "for_block", 0);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) CtFor(spoon.reflect.code.CtFor) Test(org.junit.Test)

Example 98 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CtClassTest method testAllTypeReferencesToALocalTypeShouldNotStartWithNumber.

@Test
public void testAllTypeReferencesToALocalTypeShouldNotStartWithNumber() throws Exception {
    // contract: When we have a local type, we should never start with its number. But this
    // number isn't rewrite only in the class declaration but in all type references and in
    // the constructor.
    final CtType<Pozole> aPozole = buildClass(Pozole.class);
    final CtClass<?> cook = aPozole.getNestedType("1Cook");
    assertEquals("1Cook", cook.getSimpleName());
    assertEquals("spoon.test.ctClass.testclasses.Pozole$1Cook", cook.getQualifiedName());
    final Set<? extends CtConstructor<?>> constructors = cook.getConstructors();
    final String expectedConstructor = "public Cook() {" + System.lineSeparator() + "}";
    assertEquals(expectedConstructor, constructors.toArray(new CtConstructor[constructors.size()])[0].toString());
    assertEquals("final java.lang.Class<Cook> cookClass = Cook.class", cook.getMethod("m").getBody().getStatement(0).toString());
    Factory factory = aPozole.getFactory();
    aPozole.removeModifier(ModifierKind.PUBLIC);
    factory.Code().createCodeSnippetStatement(aPozole.toString()).compile();
    CtClass internalClass = factory.Core().createClass();
    internalClass.setSimpleName("Foo");
    cook.getParent(CtBlock.class).addStatement(internalClass);
    assertEquals("Foo", internalClass.getSimpleName());
    assertEquals("spoon.test.ctClass.testclasses.Pozole$Foo", internalClass.getQualifiedName());
    internalClass.addConstructor(factory.Core().createConstructor());
    CtConstructor cons = (CtConstructor) internalClass.getConstructors().toArray(new CtConstructor[0])[0];
    cons.setBody(factory.Core().createBlock());
    CtConstructorCall call = cook.getFactory().Core().createConstructorCall();
    call.setExecutable(cons.getReference());
    assertEquals(internalClass, internalClass.getReference().getDeclaration());
    assertEquals("new Foo()", call.toString());
    internalClass.insertAfter(call);
    factory.getEnvironment().setAutoImports(true);
    factory.Code().createCodeSnippetStatement(aPozole.toString()).compile();
    factory.getEnvironment().setAutoImports(false);
    factory.Code().createCodeSnippetStatement(aPozole.toString()).compile();
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Pozole(spoon.test.ctClass.testclasses.Pozole) CtBlock(spoon.reflect.code.CtBlock) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Factory(spoon.reflect.factory.Factory) CtConstructor(spoon.reflect.declaration.CtConstructor) Test(org.junit.Test)

Example 99 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class TypeReferenceTest method testAnnotationOnMethodWithPrimitiveReturnTypeInNoClasspath.

@Test
public void testAnnotationOnMethodWithPrimitiveReturnTypeInNoClasspath() throws Exception {
    // contract: In no classpath mode, if we have an annotation declared on a method and overridden
    // from a super class in an anonymous class, we should rewrite correctly the annotation and don't
    // throw a NPE.
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/A.java");
    launcher.setSourceOutputDirectory("./target/class-declaration");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("A");
    final CtClass anonymousClass = aClass.getElements(new TypeFilter<>(CtNewClass.class)).get(0).getAnonymousClass();
    final CtMethod run = anonymousClass.getMethod("run");
    assertNotNull(run);
    assertEquals(1, run.getAnnotations().size());
    assertEquals("@java.lang.Override", run.getAnnotations().get(0).toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 100 with CtClass

use of spoon.reflect.declaration.CtClass 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)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14