Search in sources :

Example 6 with CtElement

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

the class FactoryTest method specificationCoreFactoryCreate.

@Test
public void specificationCoreFactoryCreate() throws Exception {
    // contract: all concrete metamodel classes must be instantiable by CoreFactory.create
    for (CtType<? extends CtElement> itf : SpoonTestHelpers.getAllInstantiableMetamodelInterfaces()) {
        CtElement o = itf.getFactory().Core().create(itf.getActualClass());
        assertNotNull(o);
        assertTrue(itf.getActualClass().isInstance(o));
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) Test(org.junit.Test)

Example 7 with CtElement

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

the class FieldFactoryTest method testCreateFromSource.

@Test
public void testCreateFromSource() throws Exception {
    CtClass<?> target = build("spoon.test.testclasses", "SampleClass");
    Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Object> type = factory.Class().get(Foo.class);
    CtField<?> source = type.getField("i");
    FieldFactory ff = type.getFactory().Field();
    TypeFactory tf = type.getFactory().Type();
    ff.create(target, source);
    CtField<?> field = target.getField("i");
    Assert.assertEquals("i", field.getSimpleName());
    CtTypeReference<?> tref = tf.createReference("int");
    Assert.assertEquals(tref, field.getType());
    CtElement parent = field.getParent();
    Assert.assertTrue(parent instanceof CtClass<?>);
    Assert.assertEquals("SampleClass", ((CtClass<?>) parent).getSimpleName());
}
Also used : FieldFactory(spoon.reflect.factory.FieldFactory) CtElement(spoon.reflect.declaration.CtElement) FieldFactory(spoon.reflect.factory.FieldFactory) Factory(spoon.reflect.factory.Factory) TypeFactory(spoon.reflect.factory.TypeFactory) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Example 8 with CtElement

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

the class FilterTest method testFilterChildrenWithoutFilterQueryStep.

@Test
public void testFilterChildrenWithoutFilterQueryStep() 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();
    // Contract: the filterChildren(null) without Filter returns same results like filter which returns true for each input.
    List<CtElement> list = launcher.getFactory().Package().getRootPackage().filterChildren(null).list();
    Iterator<CtElement> iter = list.iterator();
    launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        return true;
    }).forEach(real -> {
        // the elements produced by each query are same
        CtElement expected = iter.next();
        if (real != expected) {
            assertEquals(expected, real);
        }
    });
    assertTrue(list.size() > 0);
    assertTrue(iter.hasNext() == false);
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 9 with CtElement

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

the class FilterTest method testEarlyTerminatingQuery.

@Test
public void testEarlyTerminatingQuery() throws Exception {
    // contract: a method first evaluates query until first element is found and then terminates the query
    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 {

        boolean wasTerminated = false;

        void failIfTerminated(String place) {
            assertTrue("The " + place + " is called after query was terminated.", wasTerminated == false);
        }
    }
    Context context = new Context();
    CtMethod firstMethod = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        context.failIfTerminated("Filter#match of filterChildren");
        return true;
    }).map((CtElement e) -> {
        context.failIfTerminated("Array returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return new CtElement[] { e, e };
    }).map((CtElement e) -> {
        context.failIfTerminated("List returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return Arrays.asList(new CtElement[] { e, e });
    }).map((CtElement e, CtConsumer<Object> out) -> {
        context.failIfTerminated("CtConsumableFunction#apply of map");
        if (e instanceof CtMethod) {
            // this call should pass and cause termination of the query
            out.accept(e);
            context.wasTerminated = true;
        // let it call out.accept(e); once more to check that next step is not called after query is terminated
        }
        out.accept(e);
    }).map(e -> {
        context.failIfTerminated("CtFunction#apply of map after CtConsumableFunction");
        return e;
    }).first(CtMethod.class);
    assertTrue(firstMethod != null);
    assertTrue(context.wasTerminated);
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 10 with CtElement

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

the class FilterTest method unionOfTwoFilters.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void unionOfTwoFilters() throws Exception {
    Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
    TypeFilter<CtNewClass> newClassFilter = new TypeFilter<CtNewClass>(CtNewClass.class);
    TypeFilter<CtMethod> methodFilter = new TypeFilter<CtMethod>(CtMethod.class);
    CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.UNION, methodFilter, newClassFilter);
    List filteredWithCompositeFilter = Query.getElements(factory, compositeFilter);
    List<CtMethod> methods = Query.getElements(factory, methodFilter);
    List<CtNewClass> newClasses = Query.getElements(factory, newClassFilter);
    List<CtElement> union = new ArrayList<CtElement>();
    union.addAll(methods);
    union.addAll(newClasses);
    assertEquals(methods.size() + newClasses.size(), union.size());
    assertEquals(union.size(), filteredWithCompositeFilter.size());
    assertTrue(filteredWithCompositeFilter.containsAll(union));
}
Also used : CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtNewClass(spoon.reflect.code.CtNewClass) List(java.util.List) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtElement (spoon.reflect.declaration.CtElement)86 Test (org.junit.Test)39 Launcher (spoon.Launcher)23 Factory (spoon.reflect.factory.Factory)18 ArrayList (java.util.ArrayList)17 CtMethod (spoon.reflect.declaration.CtMethod)17 CtType (spoon.reflect.declaration.CtType)15 CtStatement (spoon.reflect.code.CtStatement)14 CtTypeReference (spoon.reflect.reference.CtTypeReference)14 List (java.util.List)12 CtClass (spoon.reflect.declaration.CtClass)12 CtField (spoon.reflect.declaration.CtField)12 CtIf (spoon.reflect.code.CtIf)11 CtInvocation (spoon.reflect.code.CtInvocation)11 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)11 CtLocalVariable (spoon.reflect.code.CtLocalVariable)10 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 CtScanner (spoon.reflect.visitor.CtScanner)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9 Collection (java.util.Collection)8