Search in sources :

Example 6 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class CtTypeParameterTest method checkType.

private void checkType(CtType<?> type) throws NoSuchFieldException, SecurityException {
    List<CtTypeParameter> formalTypeParameters = type.getFormalCtTypeParameters();
    for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
        checkTypeParamErasureOfType(ctTypeParameter, type.getActualClass());
    }
    for (CtTypeMember member : type.getTypeMembers()) {
        if (member instanceof CtFormalTypeDeclarer) {
            CtFormalTypeDeclarer ftDecl = (CtFormalTypeDeclarer) member;
            formalTypeParameters = ftDecl.getFormalCtTypeParameters();
            if (member instanceof CtExecutable<?>) {
                CtExecutable<?> exec = (CtExecutable<?>) member;
                for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
                    checkTypeParamErasureOfExecutable(ctTypeParameter);
                }
                for (CtParameter<?> param : exec.getParameters()) {
                    checkParameterErasureOfExecutable(param);
                }
            } else if (member instanceof CtType<?>) {
                CtType<?> nestedType = (CtType<?>) member;
                // recursive call for nested type
                checkType(nestedType);
            }
        }
    }
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtType(spoon.reflect.declaration.CtType) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 7 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class TestCompilationUnit method testGetUnitTypeWorksWithDeclaredType.

@Test
public void testGetUnitTypeWorksWithDeclaredType() {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/api/testclasses/Bar.java");
    launcher.buildModel();
    CtType type = launcher.getFactory().Type().get(Bar.class);
    CompilationUnit compilationUnit = type.getPosition().getCompilationUnit();
    assertEquals(CompilationUnit.UNIT_TYPE.TYPE_DECLARATION, compilationUnit.getUnitType());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 8 with CtType

use of spoon.reflect.declaration.CtType 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 9 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class FilterTest method testNameFilterWithGenericType.

@Test
public void testNameFilterWithGenericType() {
    // contract: NamedElementFilter of T should only return T elements
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal4/Constants.java");
    spoon.buildModel();
    CtType type = spoon.getFactory().Type().get(Constants.class);
    List<CtMethod> ctMethods = type.getElements(new NamedElementFilter<>(CtMethod.class, "CONSTANT"));
    assertTrue(ctMethods.isEmpty());
    List<CtField> ctFields = type.getElements(new NamedElementFilter<>(CtField.class, "CONSTANT"));
    assertEquals(1, ctFields.size());
    assertTrue(ctFields.get(0) instanceof CtField);
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 10 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class FilterTest method testClassCastExceptionOnForEach.

@Test
public void testClassCastExceptionOnForEach() throws Exception {
    // contract: bound query, without any mapping
    // This test could fail with a version of JDK <= 8.0.40.
    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();
        // contract: if the query produces elements which cannot be cast to forEach consumer, then they are ignored
        launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
            context.count++;
        });
        assertTrue(context.count > 0);
    }
    {
        Context context = new Context();
        // contract: if the for each implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
                context.count++;
                throw new ClassCastException("TEST");
            });
            fail("It must fail, because body of forEach should be called and thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the for each implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach(new CtConsumer<CtType>() {

                @Override
                public void accept(CtType t) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            });
            fail("It must fail, because body of forEach should be called and thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the select implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).select(new Filter<CtType>() {

                @Override
                public boolean matches(CtType element) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            }).list();
            fail("It must fail, because body of select thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the select implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).select((CtType element) -> {
                context.count++;
                throw new ClassCastException("TEST");
            }).list();
            fail("It must fail, because body of select thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtFunction) implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtFunction<CtType, Object>() {

                @Override
                public Object apply(CtType input) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtFunction) implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input) -> {
                context.count++;
                throw new ClassCastException("TEST");
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtConsumableFunction) implementation made by local class throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtConsumableFunction<CtType>() {

                @Override
                public void apply(CtType input, CtConsumer<Object> outputConsumer) {
                    context.count++;
                    throw new ClassCastException("TEST");
                }
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
    {
        Context context = new Context();
        // contract: if the map(CtConsumableFunction) implementation made by lambda throws CCE then it is reported
        try {
            launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input, CtConsumer<Object> outputConsumer) -> {
                context.count++;
                throw new ClassCastException("TEST");
            }).failurePolicy(QueryFailurePolicy.IGNORE).list();
            fail("It must fail, because body of map thrown CCE");
        } catch (ClassCastException e) {
            assertTrue(context.count > 0);
            assertEquals("TEST", e.getMessage());
        }
    }
}
Also used : CtType(spoon.reflect.declaration.CtType) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtFunction(spoon.reflect.visitor.chain.CtFunction) Test(org.junit.Test)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11