Search in sources :

Example 56 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class AfterBurner method addOrInsertMethod.

/**
     * Add/Inserts java instructions into a given method of a given class.
     * @param insertableMethod contains all information to perform byte code injection.
     * @throws CannotCompileException if the source contained in insertableMethod can't be compiled.
     * @throws AfterBurnerImpossibleException if something else goes wrong, wraps other exceptions.
     */
public void addOrInsertMethod(InsertableMethod insertableMethod) throws CannotCompileException, AfterBurnerImpossibleException {
    log.info("InsertableMethod : " + insertableMethod);
    // create or complete onViewCreated
    String targetMethodName = insertableMethod.getTargetMethodName();
    CtClass classToTransform = insertableMethod.getClassToInsertInto();
    CtMethod targetMethod = extractExistingMethod(classToTransform, targetMethodName);
    log.info("Method : " + targetMethod);
    if (targetMethod != null) {
        InsertableMethodInjectorEditor injectorEditor = new InsertableMethodInjectorEditor(classToTransform, insertableMethod);
        targetMethod.instrument(injectorEditor);
        if (!injectorEditor.isSuccessful) {
            throw new CannotCompileException("Transformation failed. Insertion method not found.: " + targetMethodName);
        }
    } else {
        classToTransform.addMethod(CtNewMethod.make(insertableMethod.getFullMethod(), classToTransform));
    }
}
Also used : CtClass(javassist.CtClass) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 57 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class CtMethodJavaWriterTest method testExtractSignature_with_params.

@Test
public void testExtractSignature_with_params() throws CannotCompileException, NotFoundException {
    //GIVEN
    CtClass targetClass = ClassPool.getDefault().makeClass("Target" + TestCounter.testCounter++);
    CtMethod fooMethod = CtNewMethod.make("public void foo(int a, String b) {}", targetClass);
    targetClass.addMethod(fooMethod);
    //WHEN
    String extractSignature = signatureExtractor.createJavaSignature(fooMethod);
    //THEN
    assertEquals("public void foo(int p0, java.lang.String p1)", extractSignature);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 58 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class CtMethodJavaWriterTest method testExtractSignature_with_throws.

@Test
public void testExtractSignature_with_throws() throws CannotCompileException, NotFoundException {
    //GIVEN
    CtClass targetClass = ClassPool.getDefault().makeClass("Target" + TestCounter.testCounter++);
    CtMethod fooMethod = CtNewMethod.make("public void foo() throws Exception, Throwable {}", targetClass);
    targetClass.addMethod(fooMethod);
    //WHEN
    String extractSignature = signatureExtractor.createJavaSignature(fooMethod);
    //THEN
    assertEquals("public void foo() throws java.lang.Exception, java.lang.Throwable", extractSignature);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 59 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class CtMethodJavaWriterTest method testInvokeSuper_without_params.

@Test
public void testInvokeSuper_without_params() throws CannotCompileException, NotFoundException {
    //GIVEN
    CtClass targetClass = ClassPool.getDefault().makeClass("Target" + TestCounter.testCounter++);
    CtMethod fooMethod = CtNewMethod.make("public void foo() {}", targetClass);
    targetClass.addMethod(fooMethod);
    //WHEN
    String extractSignature = signatureExtractor.invokeSuper(fooMethod);
    //THEN
    assertEquals("super.foo();", extractSignature);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 60 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class ExampleProcessor method shouldTransform.

@Override
public boolean shouldTransform(CtClass candidateClass) {
    boolean hasDoStuff = false;
    CtMethod[] methods = candidateClass.getMethods();
    for (CtMethod method : methods) {
        if (method.getName().equals("doStuff") || method.getName().equals("doOtherStuff")) {
            hasDoStuff = true;
        }
    }
    return hasDoStuff;
}
Also used : CtMethod(javassist.CtMethod)

Aggregations

CtMethod (javassist.CtMethod)76 CtClass (javassist.CtClass)46 NotFoundException (javassist.NotFoundException)23 CannotCompileException (javassist.CannotCompileException)20 ClassPool (javassist.ClassPool)18 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2