use of spoon.reflect.declaration.ModifierKind in project spoon by INRIA.
the class FieldTest method testAddAFieldInAClassAtAPositionGiven.
@Test
public void testAddAFieldInAClassAtAPositionGiven() throws Exception {
final Factory factory = createFactory();
final CtClass<Object> fieldClass = factory.Class().create("FieldClass");
final HashSet<ModifierKind> modifiers = new HashSet<ModifierKind>();
modifiers.add(ModifierKind.STATIC);
final CtField<Integer> first = createField(factory, modifiers, "FIELD");
fieldClass.addField(first);
final CtField<Integer> second = createField(factory, modifiers, "FIELD_2");
second.setDefaultExpression(factory.Code().createCodeSnippetExpression(first.getSimpleName() + " + 1"));
fieldClass.addField(1, second);
final CtField<Integer> third = createField(factory, modifiers, "FIELD_3");
third.setDefaultExpression(factory.Code().createCodeSnippetExpression(first.getSimpleName() + " + 1"));
fieldClass.addField(1, third);
assertEquals(3, fieldClass.getFields().size());
assertEquals(first, fieldClass.getFields().get(0));
assertEquals(third, fieldClass.getFields().get(1));
assertEquals(second, fieldClass.getFields().get(2));
}
use of spoon.reflect.declaration.ModifierKind 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());
}
use of spoon.reflect.declaration.ModifierKind in project spoon by INRIA.
the class FieldFactoryTest method testCreate.
@Test
public void testCreate() throws Exception {
CtClass<?> type = build("spoon.test.testclasses", "SampleClass");
FieldFactory ff = type.getFactory().Field();
TypeFactory tf = type.getFactory().Type();
Set<ModifierKind> mods = new HashSet<ModifierKind>();
mods.add(ModifierKind.PRIVATE);
CtTypeReference<?> tref = tf.createReference(String.class);
ff.create(type, mods, tref, "name");
CtField<?> field = type.getField("name");
Assert.assertEquals("name", field.getSimpleName());
Assert.assertEquals(tref, field.getType());
CtElement parent = field.getParent();
Assert.assertTrue(parent instanceof CtClass<?>);
Assert.assertEquals("SampleClass", ((CtClass<?>) parent).getSimpleName());
}
Aggregations