Search in sources :

Example 1 with InsertableMethod

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

the class AfterBurner method beforeOverrideMethod.

/**
     * Add/Inserts java instructions into a given override method of a given class. Before 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 before 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 beforeOverrideMethod(CtClass targetClass, String targetMethodName, String body) throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException {
    InsertableMethod insertableMethod = new InsertableMethodBuilder(this, signatureExtractor).insertIntoClass(targetClass).beforeOverrideMethod(targetMethodName).withBody(body).createInsertableMethod();
    addOrInsertMethod(insertableMethod);
}
Also used : InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod)

Example 2 with InsertableMethod

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

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

the class InsertableMethodBuilderTest method testCheckAllFields_should_succeed_with_insert_after_method_defined.

@Test
public void testCheckAllFields_should_succeed_with_insert_after_method_defined() throws AfterBurnerImpossibleException {
    //GIVEN
    CtClass classToInsertInto = CtClass.intType;
    String targetMethod = "";
    String insertionAfterMethod = "";
    String fullMethod = "";
    String body = "";
    //WHEN
    InsertableMethod method = builder.insertIntoClass(classToInsertInto).inMethodIfExists(targetMethod).afterACallTo(insertionAfterMethod).withBody(body).elseCreateMethodIfNotExists(fullMethod).createInsertableMethod();
    //THEN
    assertNotNull(method);
    assertEquals(classToInsertInto, method.getClassToInsertInto());
    assertEquals(targetMethod, method.getTargetMethodName());
    assertNull(method.getInsertionBeforeMethod());
    assertEquals(insertionAfterMethod, method.getInsertionAfterMethod());
    assertEquals(fullMethod, method.getFullMethod());
    assertEquals(body, method.getBody());
}
Also used : CtClass(javassist.CtClass) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) Test(org.junit.Test)

Example 4 with InsertableMethod

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

the class InsertableMethodBuilderTest method testCheckAllFields_should_succeed_with_insert_before_method_defined.

@Test
public void testCheckAllFields_should_succeed_with_insert_before_method_defined() throws AfterBurnerImpossibleException {
    //GIVEN
    CtClass classToInsertInto = CtClass.intType;
    String targetMethod = "target";
    String insertionBeforeMethod = "insertionBeforeMethod";
    String fullMethod = "fullMethod";
    String body = "body";
    //WHEN
    InsertableMethod method = builder.insertIntoClass(classToInsertInto).inMethodIfExists(targetMethod).beforeACallTo(insertionBeforeMethod).withBody(body).elseCreateMethodIfNotExists(fullMethod).createInsertableMethod();
    //THEN
    assertNotNull(method);
    assertEquals(classToInsertInto, method.getClassToInsertInto());
    assertEquals(targetMethod, method.getTargetMethodName());
    assertEquals(insertionBeforeMethod, method.getInsertionBeforeMethod());
    assertNull(method.getInsertionAfterMethod());
    assertEquals(fullMethod, method.getFullMethod());
    assertEquals(body, method.getBody());
}
Also used : CtClass(javassist.CtClass) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) Test(org.junit.Test)

Example 5 with InsertableMethod

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

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