Search in sources :

Example 1 with InsertableConstructor

use of com.github.stephanenicolas.afterburner.inserts.InsertableConstructor in project afterburner by stephanenicolas.

the class AfterBurnerTest method testInsertConstructor_with_no_constructor.

@Test(expected = AfterBurnerImpossibleException.class)
public void testInsertConstructor_with_no_constructor() throws Exception {
    // GIVEN
    target.addConstructor(CtNewConstructor.make("public Target() {}", target));
    target.addField(new CtField(CtClass.intType, "foo", target));
    InsertableConstructor insertableConstructor = new SimpleInsertableConstructor(target, "foo = 2;", false);
    // WHEN
    afterBurner.insertConstructor(insertableConstructor);
    // THEN
    fail();
}
Also used : CtField(javassist.CtField) InsertableConstructor(com.github.stephanenicolas.afterburner.inserts.InsertableConstructor) SimpleInsertableConstructor(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableConstructor) SimpleInsertableConstructor(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableConstructor) Test(org.junit.Test)

Example 2 with InsertableConstructor

use of com.github.stephanenicolas.afterburner.inserts.InsertableConstructor in project afterburner by stephanenicolas.

the class InsertableConstructorBuilderTest method testCheckAllFields_should_succeed_with_all_fields_defined_using_class.

@Test
public void testCheckAllFields_should_succeed_with_all_fields_defined_using_class() throws Exception {
    //GIVEN
    String body = "";
    Class<?> classToInsertInto = String.class;
    ;
    //WHEN
    InsertableConstructor constructor = builder.insertIntoClass(classToInsertInto).withBody(body).createInsertableConstructor();
    //THEN
    assertNotNull(constructor);
    assertEquals(ClassPool.getDefault().get(String.class.getName()), constructor.getClassToInsertInto());
    assertEquals(body, constructor.getConstructorBody(null));
    assertTrue(constructor.acceptParameters(null));
}
Also used : InsertableConstructor(com.github.stephanenicolas.afterburner.inserts.InsertableConstructor) Test(org.junit.Test)

Example 3 with InsertableConstructor

use of com.github.stephanenicolas.afterburner.inserts.InsertableConstructor in project afterburner by stephanenicolas.

the class AfterBurnerTest method testInsertConstructor.

@Test
public void testInsertConstructor() throws Exception {
    // GIVEN
    target.addConstructor(CtNewConstructor.make("public Target() {}", target));
    target.addField(new CtField(CtClass.intType, "foo", target));
    InsertableConstructor insertableConstructor = new SimpleInsertableConstructor(target, "foo = 2;", true);
    // WHEN
    afterBurner.insertConstructor(insertableConstructor);
    // THEN
    targetClass = target.toClass();
    targetInstance = targetClass.newInstance();
    assertHasFooFieldWithValue(target, 2);
}
Also used : CtField(javassist.CtField) InsertableConstructor(com.github.stephanenicolas.afterburner.inserts.InsertableConstructor) SimpleInsertableConstructor(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableConstructor) SimpleInsertableConstructor(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableConstructor) Test(org.junit.Test)

Example 4 with InsertableConstructor

use of com.github.stephanenicolas.afterburner.inserts.InsertableConstructor in project afterburner by stephanenicolas.

the class InsertableConstructorBuilderTest method testCheckAllFields_should_succeed_with_all_fields_defined.

@Test
public void testCheckAllFields_should_succeed_with_all_fields_defined() throws AfterBurnerImpossibleException {
    //GIVEN
    CtClass classToInsertInto = CtClass.intType;
    String body = "";
    //WHEN
    InsertableConstructor constructor = builder.insertIntoClass(classToInsertInto).withBody(body).createInsertableConstructor();
    //THEN
    assertNotNull(constructor);
    assertEquals(classToInsertInto, constructor.getClassToInsertInto());
    assertEquals(body, constructor.getConstructorBody(null));
    assertTrue(constructor.acceptParameters(null));
}
Also used : CtClass(javassist.CtClass) InsertableConstructor(com.github.stephanenicolas.afterburner.inserts.InsertableConstructor) Test(org.junit.Test)

Aggregations

InsertableConstructor (com.github.stephanenicolas.afterburner.inserts.InsertableConstructor)4 Test (org.junit.Test)4 SimpleInsertableConstructor (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableConstructor)2 CtField (javassist.CtField)2 CtClass (javassist.CtClass)1