Search in sources :

Example 1 with CtMethodImpl

use of spoon.support.reflect.declaration.CtMethodImpl in project spoon by INRIA.

the class FactoryTest method testFactoryOverriding.

@Test
public void testFactoryOverriding() throws Exception {
    @SuppressWarnings("serial")
    class MyCtMethod<T> extends CtMethodImpl<T> {
    }
    ;
    @SuppressWarnings("serial") final CoreFactory specialCoreFactory = new DefaultCoreFactory() {

        @Override
        public <T> CtMethod<T> createMethod() {
            MyCtMethod<T> m = new MyCtMethod<T>();
            m.setFactory(getMainFactory());
            return m;
        }
    };
    Launcher launcher = new Launcher() {

        @Override
        public Factory createFactory() {
            return new FactoryImpl(specialCoreFactory, new StandardEnvironment());
        }
    };
    CtClass<?> type = build("spoon.test.testclasses", "SampleClass", launcher.getFactory());
    CtMethod<?> m = type.getMethodsByName("method3").get(0);
    assertTrue(m instanceof MyCtMethod);
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) Launcher(spoon.Launcher) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CoreFactory(spoon.reflect.factory.CoreFactory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 2 with CtMethodImpl

use of spoon.support.reflect.declaration.CtMethodImpl in project spoon by INRIA.

the class FilterTest method intersectionOfTwoFilters.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void intersectionOfTwoFilters() throws Exception {
    Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
    TypeFilter<CtMethod> statementFilter = new TypeFilter<CtMethod>(CtMethod.class);
    TypeFilter<CtMethodImpl> statementImplFilter = new TypeFilter<CtMethodImpl>(CtMethodImpl.class);
    CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.INTERSECTION, statementFilter, statementImplFilter);
    List<CtMethod> methodsWithInterfaceSuperclass = Query.getElements(factory, statementFilter);
    List<CtMethodImpl> methodWithConcreteClass = Query.getElements(factory, statementImplFilter);
    assertEquals(methodsWithInterfaceSuperclass.size(), methodWithConcreteClass.size());
    assertEquals(methodsWithInterfaceSuperclass, methodWithConcreteClass);
    List intersection = Query.getElements(factory, compositeFilter);
    assertEquals(methodsWithInterfaceSuperclass.size(), intersection.size());
    assertEquals(methodsWithInterfaceSuperclass, intersection);
}
Also used : CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) List(java.util.List) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 3 with CtMethodImpl

use of spoon.support.reflect.declaration.CtMethodImpl in project spoon by INRIA.

the class ElementTest method testGetFactory.

@Test
public void testGetFactory() {
    // contract: getFactory should always return an object
    // even if an element is created via its constructor
    // and not through the factory
    Launcher spoon = new Launcher();
    CtElement element = spoon.getFactory().createAnnotation();
    assertNotNull(element.getFactory());
    CtElement otherElement = new CtAnnotationImpl<>();
    assertNotNull(otherElement.getFactory());
    CtElement yetAnotherOne = new CtMethodImpl<>();
    assertNotNull(yetAnotherOne.getFactory());
    // contract: a singleton is used for the default factory
    assertSame(otherElement.getFactory(), yetAnotherOne.getFactory());
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) Launcher(spoon.Launcher) CtAnnotationImpl(spoon.support.reflect.declaration.CtAnnotationImpl) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 CtMethodImpl (spoon.support.reflect.declaration.CtMethodImpl)3 Launcher (spoon.Launcher)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CtElement (spoon.reflect.declaration.CtElement)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CoreFactory (spoon.reflect.factory.CoreFactory)1 Factory (spoon.reflect.factory.Factory)1 FactoryImpl (spoon.reflect.factory.FactoryImpl)1 CompositeFilter (spoon.reflect.visitor.filter.CompositeFilter)1 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)1 DefaultCoreFactory (spoon.support.DefaultCoreFactory)1 StandardEnvironment (spoon.support.StandardEnvironment)1 CtAnnotationImpl (spoon.support.reflect.declaration.CtAnnotationImpl)1