Search in sources :

Example 1 with SimpleInsertableMethod

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

the class AfterBurnerTest method testInsertMethod_after.

@Test
public void testInsertMethod_after() throws Exception {
    // GIVEN
    target.addMethod(CtNewMethod.make("public void bar() { }", target));
    target.addMethod(CtNewMethod.make("public boolean foo() { bar(); return false; }", target));
    target.addField(new CtField(CtClass.intType, "foo", target));
    InsertableMethod insertableMethod = new SimpleInsertableMethod(target, "foo", null, "bar", "foo = 2;", null);
    // WHEN
    afterBurner.addOrInsertMethod(insertableMethod);
    // THEN
    targetClass = target.toClass();
    targetInstance = targetClass.newInstance();
    assertHasFooMethodWithReturnValue(target, false);
    assertHasFooFieldWithValue(target, 2);
}
Also used : SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) CtField(javassist.CtField) SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) Test(org.junit.Test)

Example 2 with SimpleInsertableMethod

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

the class AfterBurnerTest method testInsertMethod_before.

@Test
public void testInsertMethod_before() throws Exception {
    // GIVEN
    target.addMethod(CtNewMethod.make("public void bar() { }", target));
    target.addMethod(CtNewMethod.make("public boolean foo() { bar(); return false; }", target));
    target.addField(new CtField(CtClass.intType, "foo", target));
    InsertableMethod insertableMethod = new SimpleInsertableMethod(target, "foo", "bar", null, "foo = 2;", null);
    // WHEN
    afterBurner.addOrInsertMethod(insertableMethod);
    // THEN
    targetClass = target.toClass();
    targetInstance = targetClass.newInstance();
    assertHasFooMethodWithReturnValue(target, false);
    assertHasFooFieldWithValue(target, 2);
}
Also used : SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) CtField(javassist.CtField) SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) Test(org.junit.Test)

Example 3 with SimpleInsertableMethod

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

the class AfterBurnerTest method testAddMethod.

@Test
public void testAddMethod() throws Exception {
    // GIVEN
    InsertableMethod insertableMethod = new SimpleInsertableMethod(target, "foo", null, null, null, "public boolean foo() { return true; }");
    // WHEN
    afterBurner.addOrInsertMethod(insertableMethod);
    // THEN
    targetClass = target.toClass();
    targetInstance = targetClass.newInstance();
    assertHasFooMethodWithReturnValue(target, true);
}
Also used : SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) Test(org.junit.Test)

Example 4 with SimpleInsertableMethod

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

the class AfterBurnerTest method testInsertMethod_not_before_not_after.

@Test(expected = AfterBurnerImpossibleException.class)
public void testInsertMethod_not_before_not_after() throws Exception {
    // GIVEN
    target.addMethod(CtNewMethod.make("public void bar() { }", target));
    target.addMethod(CtNewMethod.make("public boolean foo() { bar(); return false; }", target));
    target.addField(new CtField(CtClass.intType, "foo", target));
    InsertableMethod insertableMethod = new SimpleInsertableMethod(target, "foo", null, null, "foo = 2;", null);
    // WHEN
    afterBurner.addOrInsertMethod(insertableMethod);
    // THEN
    fail();
}
Also used : SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) CtField(javassist.CtField) SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) Test(org.junit.Test)

Aggregations

InsertableMethod (com.github.stephanenicolas.afterburner.inserts.InsertableMethod)4 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)4 Test (org.junit.Test)4 CtField (javassist.CtField)3