use of spoon.reflect.reference.CtTypeReference 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.reference.CtTypeReference in project spoon by INRIA.
the class ConstructorFactoryTest method testCreate.
@Test
public void testCreate() throws Exception {
CtClass<?> type = build("spoon.test.testclasses", "SampleClass");
Factory factory = type.getFactory();
ConstructorFactory ctorf = factory.Constructor();
CoreFactory coref = factory.Core();
Set<ModifierKind> mods = new HashSet<ModifierKind>();
mods.add(ModifierKind.PUBLIC);
List<CtParameter<?>> params = new ArrayList<CtParameter<?>>();
CtParameter<?> param = coref.createParameter();
CtTypeReference<?> tref = factory.Type().createReference(String.class);
param.setType((CtTypeReference) tref);
param.setSimpleName("str");
params.add(param);
Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<CtTypeReference<? extends Throwable>>();
ctorf.create(type, mods, params, thrownTypes);
CtConstructor<?> c = type.getConstructor(tref);
Assert.assertEquals(1, c.getParameters().size());
Assert.assertEquals("str", c.getParameters().get(0).getSimpleName());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class TypeReferenceTest method testRecursiveTypeReference.
@Test
public void testRecursiveTypeReference() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses/Tacos.java");
launcher.setSourceOutputDirectory("./target/spoon-test");
launcher.run();
final CtInvocation<?> inv = Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return !element.getExecutable().isConstructor() && super.matches(element);
}
}).get(0);
assertNotNull(inv.getExecutable());
final CtTypeReference<?> returnType = inv.getExecutable().getType();
assertNotNull(returnType);
assertEquals(1, returnType.getActualTypeArguments().size());
final CtTypeParameterReference genericType = (CtTypeParameterReference) returnType.getActualTypeArguments().get(0);
assertNotNull(genericType);
assertNotNull(genericType.getBoundingType());
CtTypeReference<?> extendsGeneric = genericType.getBoundingType();
assertNotNull(extendsGeneric);
assertEquals(1, extendsGeneric.getActualTypeArguments().size());
CtTypeParameterReference genericExtends = (CtTypeParameterReference) extendsGeneric.getActualTypeArguments().get(0);
assertNotNull(genericExtends);
assertNotNull(genericExtends.getBoundingType());
assertTrue(genericExtends.getBoundingType() instanceof CtTypeReference);
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class TypeReferenceTest method testTypeReferenceWithGenerics.
@Test
public void testTypeReferenceWithGenerics() throws Exception {
// contract: in noclasspath, a generic type name should not contain generic information
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/import-with-generics/TestWithGenerics.java");
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
CtField field = launcher.getModel().getElements(new TypeFilter<CtField>(CtField.class)).get(0);
CtTypeReference fieldTypeRef = field.getType();
assertEquals("spoon.test.imports.testclasses.withgenerics.Target", fieldTypeRef.getQualifiedName());
assertEquals(2, fieldTypeRef.getActualTypeArguments().size());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class TypeReferenceTest method testCorrectEnumParent.
@Test
public void testCorrectEnumParent() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
CtEnum e = launcher.getFactory().Enum().create("spoon.test.reference.EnumE");
CtTypeReference correctParent = launcher.getFactory().Type().createReference(java.lang.Enum.class);
assertEquals(correctParent, e.getReference().getSuperclass());
}
Aggregations