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);
}
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);
}
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();
}
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());
}
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")));
}
Aggregations