Search in sources :

Example 1 with ConstructorFactory

use of spoon.reflect.factory.ConstructorFactory in project spoon by INRIA.

the class ConstructorFactoryTest method testCreateDefault.

@Test
public void testCreateDefault() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    ClassFactory classf = factory.Class();
    ConstructorFactory ctorf = factory.Constructor();
    CtClass<?> ctclass = classf.create("Sample");
    ctorf.createDefault(ctclass);
    CtConstructor<?> c = ctclass.getConstructor();
    Assert.assertEquals(0, c.getParameters().size());
}
Also used : ClassFactory(spoon.reflect.factory.ClassFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CoreFactory(spoon.reflect.factory.CoreFactory) ConstructorFactory(spoon.reflect.factory.ConstructorFactory) ClassFactory(spoon.reflect.factory.ClassFactory) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) ConstructorFactory(spoon.reflect.factory.ConstructorFactory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 2 with ConstructorFactory

use of spoon.reflect.factory.ConstructorFactory 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)

Aggregations

Test (org.junit.Test)2 ClassFactory (spoon.reflect.factory.ClassFactory)2 ConstructorFactory (spoon.reflect.factory.ConstructorFactory)2 CoreFactory (spoon.reflect.factory.CoreFactory)2 Factory (spoon.reflect.factory.Factory)2 DefaultCoreFactory (spoon.support.DefaultCoreFactory)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CtParameter (spoon.reflect.declaration.CtParameter)1 ModifierKind (spoon.reflect.declaration.ModifierKind)1 FactoryImpl (spoon.reflect.factory.FactoryImpl)1 CtTypeReference (spoon.reflect.reference.CtTypeReference)1 StandardEnvironment (spoon.support.StandardEnvironment)1