Search in sources :

Example 21 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class EnumsTypeTest method testEnumsType.

@Test
public void testEnumsType() throws Exception {
    // contract: shadow enum should still be considered as an enum
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/reference-test/EnumsRef.java");
    Factory factory = launcher.getFactory();
    List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar");
    String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
    factory.getEnvironment().setSourceClasspath(dependencyClasspath);
    assertEquals(1, classpath.size());
    launcher.buildModel();
    List<CtAssignment> assignments = Query.getElements(factory, new TypeFilter<>(CtAssignment.class));
    CtTypeReference typeRefFromSource = assignments.get(0).getType();
    CtType typeFromSource = typeRefFromSource.getTypeDeclaration();
    assertTrue(typeRefFromSource.isEnum());
    assertTrue(typeFromSource.isEnum());
    assertTrue(typeFromSource instanceof CtEnum);
    CtTypeReference typeRefFromJar = assignments.get(1).getType();
    CtType typeFromJar = typeRefFromJar.getTypeDeclaration();
    // fail
    assertTrue(typeRefFromJar.isEnum());
    // fail
    assertTrue(typeFromJar.isEnum());
    // fail
    assertTrue(typeFromJar instanceof CtEnum);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtEnum(spoon.reflect.declaration.CtEnum) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 22 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class FilterTest method testFunctionQueryStep.

@Test
public void testFunctionQueryStep() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        int count = 0;
    }
    Context context = new Context();
    CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
        return true;
    }).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
    ((CtQueryImpl) query).logging(true);
    query.forEach((CtInterface<?> c) -> {
        assertEquals("ITostada", c.getSimpleName());
        context.count++;
    });
    assertTrue(context.count > 0);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 23 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class GenericsTest method testIsGenericsMethod.

@Test
public void testIsGenericsMethod() throws Exception {
    CtType<Tacos> aTacos = buildNoClasspath(Tacos.class).Type().get(Tacos.class);
    CtTypeParameter typeParameter = aTacos.getFormalCtTypeParameters().get(0);
    assertTrue(typeParameter.isGenerics());
    assertTrue(typeParameter.getReference().isGenerics());
    CtTypeReference ctTypeReference = aTacos.getSuperInterfaces().toArray(new CtTypeReference[aTacos.getSuperInterfaces().size()])[0];
    assertFalse(aTacos.isGenerics());
    // this is a generic type reference spoon.test.generics.testclasses.ITacos<V>
    assertEquals("spoon.test.generics.testclasses.ITacos<V>", ctTypeReference.toString());
    assertTrue(ctTypeReference.isGenerics());
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtTypeReference(spoon.reflect.reference.CtTypeReference) Tacos(spoon.test.generics.testclasses.Tacos) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 24 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class GenericsTest method checkFakeTpl.

private void checkFakeTpl(CtInterface<?> fakeTplItf) {
    assertNotNull(fakeTplItf);
    CtMethod<?> applyMethod = fakeTplItf.getMethodsByName("apply").get(0);
    CtTypeReference<?> returnType = applyMethod.getType();
    assertEquals("T", returnType.getSimpleName());
    assertTrue(returnType instanceof CtTypeParameterReference);
    assertEquals("CtElement", returnType.getSuperclass().getSimpleName());
    CtParameter<?> targetType = applyMethod.getParameters().get(0);
    List<CtTypeReference<?>> targetTypeArgument = targetType.getType().getActualTypeArguments();
    assertEquals(1, targetTypeArgument.size());
    assertTrue(targetTypeArgument.get(0) instanceof CtWildcardReference);
    CtMethod<?> testMethod = fakeTplItf.getMethodsByName("test").get(0);
    List<CtParameter<?>> parameters = testMethod.getParameters();
    assertEquals(3, parameters.size());
    CtParameter thirdParam = parameters.get(2);
    assertTrue(thirdParam.getType() instanceof CtTypeParameterReference);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtWildcardReference(spoon.reflect.reference.CtWildcardReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtParameter(spoon.reflect.declaration.CtParameter)

Example 25 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class ImportTest method testCanAccess.

@Test
public void testCanAccess() throws Exception {
    class Checker {

        final Launcher launcher;

        final CtTypeReference<?> aClientClass;

        final CtTypeReference<?> anotherClass;

        Checker() {
            launcher = new Launcher();
            launcher.setArgs(new String[] { "-i", "./src/test/java/spoon/test/imports/testclasses", "--with-imports" });
            launcher.buildModel();
            aClientClass = launcher.getFactory().Class().get(ClientClass.class).getReference();
            anotherClass = launcher.getFactory().Class().get(Tacos.class).getReference();
        }

        void checkCanAccess(String aClassName, boolean isInterface, boolean canAccessClientClass, boolean canAccessAnotherClass, String clientAccessType, String anotherAccessType) {
            CtTypeReference<?> target;
            if (isInterface) {
                target = launcher.getFactory().Interface().create(aClassName).getReference();
            } else {
                target = launcher.getFactory().Class().get(aClassName).getReference();
            }
            boolean isNested = target.getDeclaringType() != null;
            CtTypeReference<?> accessType;
            target.setParent(aClientClass.getTypeDeclaration());
            if (canAccessClientClass) {
                assertTrue("ClientClass should have access to " + aClassName + " but it has not", aClientClass.canAccess(target));
            } else {
                assertFalse("ClientClass should have NO access to " + aClassName + " but it has", aClientClass.canAccess(target));
            }
            if (isNested) {
                accessType = target.getAccessType();
                if (clientAccessType != null) {
                    assertEquals(clientAccessType, accessType.getQualifiedName());
                } else if (accessType != null) {
                    fail("ClientClass should have NO accessType to " + aClassName + " but it has " + accessType.getQualifiedName());
                }
            }
            target.setParent(anotherClass.getTypeDeclaration());
            if (canAccessAnotherClass) {
                assertTrue("Tacos class should have access to " + aClassName + " but it has not", anotherClass.canAccess(target));
            } else {
                assertFalse("Tacos class should have NO access to " + aClassName + " but it has", anotherClass.canAccess(target));
            }
            if (isNested) {
                if (anotherAccessType != null) {
                    accessType = target.getAccessType();
                    assertEquals(anotherAccessType, accessType.getQualifiedName());
                } else {
                    try {
                        accessType = target.getAccessType();
                    } catch (SpoonException e) {
                        if (e.getMessage().indexOf("Cannot compute access path to type: ") == -1) {
                            throw e;
                        }
                        // else OK, it should throw exception
                        accessType = null;
                    }
                    if (accessType != null) {
                        fail("Tacos class should have NO accessType to " + aClassName + " but it has " + accessType.getQualifiedName());
                    }
                }
            }
        }
    }
    Checker c = new Checker();
    c.checkCanAccess("spoon.test.imports.testclasses.ClientClass", false, true, true, null, null);
    c.checkCanAccess("spoon.test.imports.testclasses.ClientClass$InnerClass", false, true, false, "spoon.test.imports.testclasses.ClientClass", "spoon.test.imports.testclasses.ClientClass");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.ChildClass", false, true, true, null, null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.PublicInterface2", true, true, true, null, null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.PublicInterface2$NestedInterface", true, true, true, "spoon.test.imports.testclasses.internal.PublicInterface2", "spoon.test.imports.testclasses.internal.PublicInterface2");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.PublicInterface2$NestedClass", true, true, true, "spoon.test.imports.testclasses.internal.PublicInterface2", "spoon.test.imports.testclasses.internal.PublicInterface2");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", true, true, true, "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PackageProtectedInterface", true, false, false, "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface", true, true, false, "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface$NestedOfProtectedInterface", true, true, true, /*canAccess, but has no access to accessType*/
    "spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface$NestedPublicInterface", true, true, true, /*canAccess, but has no access to accessType*/
    "spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", true, true, true, /*canAccess, but has no access to accessType*/
    "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface$NestedOfPublicInterface", true, true, true, /*canAccess, has access to first accessType, but not to full accesspath*/
    "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface$NestedPublicInterface", true, true, true, /*canAccess, has access to first accessType, but not to full accesspath*/
    "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface");
}
Also used : SpoonException(spoon.SpoonException) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) 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