Search in sources :

Example 66 with CtTypeReference

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);
}
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 67 with CtTypeReference

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());
}
Also used : ModifierKind(spoon.reflect.declaration.ModifierKind) ArrayList(java.util.ArrayList) CoreFactory(spoon.reflect.factory.CoreFactory) ConstructorFactory(spoon.reflect.factory.ConstructorFactory) ClassFactory(spoon.reflect.factory.ClassFactory) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtParameter(spoon.reflect.declaration.CtParameter) CoreFactory(spoon.reflect.factory.CoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtTypeReference(spoon.reflect.reference.CtTypeReference) ConstructorFactory(spoon.reflect.factory.ConstructorFactory) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 68 with CtTypeReference

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);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 69 with 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());
}
Also used : CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 70 with CtTypeReference

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());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CtEnum(spoon.reflect.declaration.CtEnum) Test(org.junit.Test)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9