Search in sources :

Example 6 with InsertableMethod

use of com.github.stephanenicolas.afterburner.inserts.InsertableMethod 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 7 with InsertableMethod

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

the class AfterBurner method afterOverrideMethod.

/**
     * Add/Inserts java instructions into a given override method of a given class. After the overriden method call.
     * @param targetClass the class to inject code into.
     * @param targetMethodName the method to inject code into. Body will be injected right after the call to super.<targetName>.
     * @param body the instructions of java to be injected.
     * @throws CannotCompileException if the source contained in insertableMethod can't be compiled.
     * @throws AfterBurnerImpossibleException if something else goes wrong, wraps other exceptions.
     */
public void afterOverrideMethod(CtClass targetClass, String targetMethodName, String body) throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException {
    InsertableMethod insertableMethod = new InsertableMethodBuilder(this, signatureExtractor).insertIntoClass(targetClass).afterOverrideMethod(targetMethodName).withBody(body).createInsertableMethod();
    addOrInsertMethod(insertableMethod);
}
Also used : InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod)

Example 8 with InsertableMethod

use of com.github.stephanenicolas.afterburner.inserts.InsertableMethod 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)8 Test (org.junit.Test)6 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)4 CtField (javassist.CtField)3 CtClass (javassist.CtClass)2