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