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