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