use of spoon.reflect.code.CtConstructorCall 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.code.CtConstructorCall in project spoon by INRIA.
the class ExecutableRefTest method testSameTypeInConstructorCallBetweenItsObjectAndItsExecutable.
@Test
public void testSameTypeInConstructorCallBetweenItsObjectAndItsExecutable() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/executable/CmiContext_1.2.java");
launcher.setSourceOutputDirectory("./target/executable");
launcher.run();
final CtClass<Object> aClass = launcher.getFactory().Class().get("org.objectweb.carol.jndi.spi.CmiContext");
final List<CtConstructorCall> ctConstructorCalls = aClass.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class));
for (CtConstructorCall constructorCall : ctConstructorCalls) {
assertNotNull(constructorCall.getExecutable());
}
canBeBuilt("./target/executable", 8, true);
}
use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class TypeReferenceTest method testConstructorCallInNoClasspath.
@Test
public void testConstructorCallInNoClasspath() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/resources/noclasspath/Demo5.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Object> demo5 = launcher.getFactory().Class().get("Demo5");
final CtMethod<Object> foo = demo5.getMethod("foo");
final List<CtConstructorCall> elements = foo.getElements(new TypeFilter<>(CtConstructorCall.class));
assertEquals("A.B<C>", elements.get(0).getType().toString());
assertEquals("D", elements.get(1).getType().toString());
}
use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationInNewInstance.
@Test
public void testUsageOfTypeAnnotationInNewInstance() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtConstructorCall<?> ctConstructorCall = ctClass.getElements(new AbstractFilter<CtConstructorCall<?>>(CtConstructorCall.class) {
@Override
public boolean matches(CtConstructorCall<?> element) {
return "String".equals(element.getType().getSimpleName());
}
}).get(0);
final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctConstructorCall.getType().getAnnotations();
assertEquals("Type of the new class must use an annotation", 1, typeAnnotations.size());
assertEquals("Type of the new class is typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
assertEquals("New class with an type annotation must be well printed", "new java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String()", ctConstructorCall.toString());
}
use of spoon.reflect.code.CtConstructorCall in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInStatements.
@Test
public void testUsageOfTypeAnnotationWithGenericTypesInStatements() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtMethod<?> method = ctClass.getMethodsByName("m4").get(0);
// New type parameter declaration.
final List<CtTypeParameter> typeParameters = method.getFormalCtTypeParameters();
assertEquals("Method has 1 generic parameter", 1, typeParameters.size());
assertEquals("Method with an type annotation must be well printed", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
final CtBlock<?> body = method.getBody();
final String expectedFirstStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T> list = new java.util.ArrayList<>()";
final CtStatement firstStatement = body.getStatement(0);
assertEquals("Type annotation on generic parameter declared in the method", expectedFirstStatement, firstStatement.toString());
final CtConstructorCall firstConstructorCall = firstStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
final CtTypeReference<?> firstTypeReference = firstConstructorCall.getType().getActualTypeArguments().get(0);
assertTrue(firstTypeReference.isImplicit());
assertEquals("T", firstTypeReference.getSimpleName());
final String expectedSecondStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "?> list2 = new java.util.ArrayList<>()";
final CtStatement secondStatement = body.getStatement(1);
assertEquals("Wildcard with an type annotation must be well printed", expectedSecondStatement, secondStatement.toString());
final CtConstructorCall secondConstructorCall = secondStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
final CtTypeReference<?> secondTypeReference = secondConstructorCall.getType().getActualTypeArguments().get(0);
assertTrue(secondTypeReference.isImplicit());
assertEquals("Object", secondTypeReference.getSimpleName());
final String expectedThirdStatement = "java.util.List<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation> list3 = new java.util.ArrayList<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation>()";
assertEquals("Type in generic parameter with an type annotation must be well printed", expectedThirdStatement, body.getStatement(2).toString());
}
Aggregations