Search in sources :

Example 6 with ModifierKind

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));
}
Also used : ModifierKind(spoon.reflect.declaration.ModifierKind) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ModifierKind

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());
}
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)

Example 8 with ModifierKind

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());
}
Also used : FieldFactory(spoon.reflect.factory.FieldFactory) CtElement(spoon.reflect.declaration.CtElement) ModifierKind(spoon.reflect.declaration.ModifierKind) TypeFactory(spoon.reflect.factory.TypeFactory) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ModifierKind (spoon.reflect.declaration.ModifierKind)8 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 Factory (spoon.reflect.factory.Factory)3 ArrayList (java.util.ArrayList)2 CtTypeReference (spoon.reflect.reference.CtTypeReference)2 Launcher (spoon.Launcher)1 CtBlock (spoon.reflect.code.CtBlock)1 CtExpression (spoon.reflect.code.CtExpression)1 CtReturn (spoon.reflect.code.CtReturn)1 CtStatement (spoon.reflect.code.CtStatement)1 CtElement (spoon.reflect.declaration.CtElement)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtParameter (spoon.reflect.declaration.CtParameter)1 ClassFactory (spoon.reflect.factory.ClassFactory)1 ConstructorFactory (spoon.reflect.factory.ConstructorFactory)1 CoreFactory (spoon.reflect.factory.CoreFactory)1 FieldFactory (spoon.reflect.factory.FieldFactory)1 TypeFactory (spoon.reflect.factory.TypeFactory)1 DefaultCoreFactory (spoon.support.DefaultCoreFactory)1