Search in sources :

Example 11 with CtConstructorCall

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

the class GenericsTest method testConstructorCallGenerics.

@Test
public void testConstructorCallGenerics() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
    assertEquals(2, aTacos.getFormalCtTypeParameters().size());
    final CtTypeReference interfaces = aTacos.getSuperInterfaces().toArray(new CtTypeReference[0])[0];
    assertEquals(1, interfaces.getActualTypeArguments().size());
    final CtMethod<?> m = aTacos.getMethodsByName("m").get(0);
    final CtElement local1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal1 = (CtTypeReference<Object>) local1.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    final CtConstructorCall<Object> rightSideLocal1 = (CtConstructorCall<Object>) local1.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(1, leftSideLocal1.getActualTypeArguments().size());
    assertEquals(1, rightSideLocal1.getType().getActualTypeArguments().size());
    assertEquals("java.util.List<java.lang.String> l = new java.util.ArrayList<>()", local1.toString());
    final CtElement local2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal2 = (CtTypeReference<Object>) local2.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    assertEquals(0, leftSideLocal2.getActualTypeArguments().size());
    assertEquals("java.util.List l2", local2.toString());
    final CtElement local3 = m.getBody().getStatement(2).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal3 = (CtTypeReference<Object>) local3.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    final CtConstructorCall<Object> rightSideLocal3 = (CtConstructorCall<Object>) local3.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(2, leftSideLocal3.getActualTypeArguments().size());
    assertEquals(2, rightSideLocal3.getType().getActualTypeArguments().size());
    assertEquals("spoon.test.generics.testclasses.IBurritos<?, ?> burritos = new Burritos<>()", local3.toString());
    final CtElement local4 = m.getBody().getStatement(3).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal4 = (CtTypeReference<Object>) local4.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    final CtConstructorCall<Object> rightSideLocal4 = (CtConstructorCall<Object>) local4.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(1, leftSideLocal4.getActualTypeArguments().size());
    assertEquals(1, rightSideLocal4.getType().getActualTypeArguments().size());
    assertEquals("java.util.List<?> l3 = new java.util.ArrayList<java.lang.Object>()", local4.toString());
    final CtConstructorCall constructorCall1 = (CtConstructorCall) m.getBody().getStatement(4).getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(1, constructorCall1.getActualTypeArguments().size());
    assertEquals(2, constructorCall1.getType().getActualTypeArguments().size());
    assertEquals("new <java.lang.Integer>spoon.test.generics.testclasses.Tacos<java.lang.Object, java.lang.String>()", constructorCall1.toString());
    final CtConstructorCall constructorCall2 = (CtConstructorCall) m.getBody().getStatement(5).getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(0, constructorCall2.getActualTypeArguments().size());
    assertEquals(2, constructorCall2.getType().getActualTypeArguments().size());
    assertEquals("new spoon.test.generics.testclasses.Tacos<>()", constructorCall2.toString());
    canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 12 with CtConstructorCall

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

the class GenericsTest method testDiamond1.

@Test
public void testDiamond1() {
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("class Diamond {\n" + "	java.util.List<String> f = new java.util.ArrayList<>();\n" + "}").compile();
    CtField<?> f = clazz.getFields().get(0);
    CtConstructorCall<?> val = (CtConstructorCall<?>) f.getDefaultExpression();
    // the diamond is resolved to String but we don't print it, so we use the fully qualified name.
    assertTrue(val.getType().getActualTypeArguments().get(0).isImplicit());
    assertEquals("", val.getType().getActualTypeArguments().get(0).toString());
    assertEquals("java.lang.String", val.getType().getActualTypeArguments().get(0).getQualifiedName());
    assertEquals("new java.util.ArrayList<>()", val.toString());
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 13 with CtConstructorCall

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

the class GenericsTest method testName.

@Test
public void testName() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<?> aFactory = launcher.getFactory().Class().get(Tacos.BeerFactory.class);
    final CtMethod<?> m = aFactory.getMethodsByName("newBeer").get(0);
    final CtConstructorCall constructorCall1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals("new Beer()", constructorCall1.toString());
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) Tacos(spoon.test.generics.testclasses.Tacos) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 14 with CtConstructorCall

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

the class GenericsTest method testDiamondComplexGenericsRxJava.

@Test
public void testDiamondComplexGenericsRxJava() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/rxjava/");
    launcher.setSourceOutputDirectory("./target/spooned-rxjava");
    launcher.run();
    Factory factory = launcher.getFactory();
    List<CtConstructorCall> invocations = factory.getModel().getElements(new TypeFilter<>(CtConstructorCall.class));
    boolean invocationDetected = false;
    for (CtConstructorCall call : invocations) {
        if (call.getType().getSimpleName().equals("ToNotificationSubscriber")) {
            assertEquals(1, call.getType().getActualTypeArguments().size());
            CtTypeReference actualTA = call.getType().getActualTypeArguments().get(0);
            assertTrue(actualTA instanceof CtWildcardReference);
            assertEquals("?", actualTA.getSimpleName());
            assertTrue(((CtWildcardReference) actualTA).isDefaultBoundingType());
            invocationDetected = true;
        }
    }
    canBeBuilt("./target/spooned-rxjava", 8);
    assertTrue(invocationDetected);
}
Also used : CtWildcardReference(spoon.reflect.reference.CtWildcardReference) CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 15 with CtConstructorCall

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

the class CUFilterTest method testSingleExcludeWithoutFilter.

@Test
public void testSingleExcludeWithoutFilter() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/same-package/A.java");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    assertEquals(1, model.getAllTypes().size());
    // make sure `B` is not available in `model.getAllTypes`
    assertEquals("A", model.getAllTypes().iterator().next().getSimpleName());
    // make sure declaration of `B` is unknown in `model`
    final CtReturn ctReturn = model.getAllTypes().iterator().next().getMethod("createB").getBody().getStatement(0);
    final CtConstructorCall ctConstructorCall = (CtConstructorCall) ctReturn.getReturnedExpression();
    assertEquals("spoon.test.same.B", ctConstructorCall.getType().getQualifiedName());
}
Also used : CtReturn(spoon.reflect.code.CtReturn) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) 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