Search in sources :

Example 16 with CtConstructorCall

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();
}
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 17 with CtConstructorCall

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);
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 18 with CtConstructorCall

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());
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 19 with CtConstructorCall

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Example 20 with CtConstructorCall

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

Aggregations

CtConstructorCall (spoon.reflect.code.CtConstructorCall)26 Test (org.junit.Test)20 Launcher (spoon.Launcher)14 Factory (spoon.reflect.factory.Factory)9 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtClass (spoon.reflect.declaration.CtClass)6 CtReturn (spoon.reflect.code.CtReturn)5 CtElement (spoon.reflect.declaration.CtElement)4 CtMethod (spoon.reflect.declaration.CtMethod)4 MainTest (spoon.test.main.MainTest)4 ArrayList (java.util.ArrayList)3 CtComment (spoon.reflect.code.CtComment)3 CtExpression (spoon.reflect.code.CtExpression)3 CtIf (spoon.reflect.code.CtIf)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtStatement (spoon.reflect.code.CtStatement)3 CtConstructor (spoon.reflect.declaration.CtConstructor)3 CtParameter (spoon.reflect.declaration.CtParameter)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3