Javassist 通用工具之 CodeInjector
Javassist 通用工具之CodeInjector
最近在做一个APM项目,要在运行时代码修改。目前常用修改的几种工具有:ASM、BCEL、Javassist。经过对比,项目中采用了Javassist。
看这篇文章,需要对Javassist有一定的了解,可以参考:Javassist: Quick Start
在使用Javassist过程中,最常用的方法有CtMethod(或者CtConstructor)的insertBefore,insertAfter,addCatch,另外还有一种是injectAround(这种是需要自己来完成的)。可以参考:Spring:Aop before after afterReturn afterThrowing around 的原理
在代码里引入了before,beforeAround,beforeAfter,beforeThrowing,beforeReturning的概念,是取材于Spring AOP配置中的叫法。
package org.fjn.frameworkex.javassist;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.CtNewMethod;
/**
* Code Inject Tool
*
* @author <a href="mailto:fs1194361820@163.com">fs1194361820@163.com</a>
*
*/
public class CodeInjector {
public static final String METHOD_RUTURN_VALUE_VAR = "__FJN__result";
public static final String METHOD_EXEC_EXCEPTION_VAR = "ex";
public static final String CONSTRUCTOR_DELEGATE_PREFIX = "__FJN__DOC__";
public static final String METHOD_DELEGATE_PREFIX = "__FJN__DOM__";
public static final String PROCEED = "$proceed";
public static final String CRLF = "\n";
public static final String CRLF_TAB = "\n\t";
public static final String CRLF_2TAB = "\n\t\t";
public static final String CRLF_3TAB = "\n\t\t\t";
public static final String CRLF_4TAB = "\n\t\t\t\t";
public static final String getDelegateMethodNameOfConstructor(String constructorName) {
return CONSTRUCTOR_DELEGATE_PREFIX + constructorName;
}
public static final String getDelegateMethodNameOfMethod(String methodName) {
return METHOD_DELEGATE_PREFIX + methodName;
}
/**
* Inject around code to the specified method
*
* @see #injectInterceptor(CtClass, CtMethod, String, String, String,
* String, String)
*/
public void injectAround(CtMethod method, String beforeAround, String afterAround, String afterThrowing,
String afterReturning) throws Exception {
CtClass clazz = method.getDeclaringClass();
injectAround(clazz, method, beforeAround, afterAround, afterThrowing, afterReturning);
}
/**
* Inject around code to the specified method
*
* @see #injectInterceptor(CtClass, CtMethod, String, String, String,
* String, String)
*/
public void injectAround(CtClass clazz, CtMethod method, String beforeAround, String afterAround,
String afterThrowing, String afterReturning) throws Exception {
injectInterceptor(clazz, method, null, beforeAround, afterAround, afterThrowing, afterReturning);
}
/**
* Inject around code to the specified method
*
* <pre>
* <code>
* <span style="font-size:12px; color:green;">before block ... </span>
* try{
* <span style="font-size:12px; color:green;">beforeAround block ... </span>
* $procced($$);
* <span style="font-size:12px; color:green;">afterAround block ... </span>
* }catch (Throwable ex){
* <span style="font-size:12px; color:green;">afterThrowing block ... </span>
* }finally{
* <span style="font-size:12px; color:green;">afterReturning block ... </span>
* }
* </code>
* </pre>
*
*/
public void injectInterceptor(CtClass clazz, CtMethod method, String before, String beforeAround,
String afterAround, String afterThrowing, String afterReturning) throws Exception {
clazz.defrost();
int modifiers = method.getModifiers();
CtClass returnType = method.getReturnType();
CtClass[] parameters = method.getParameterTypes();
CtClass[] exceptions = method.getExceptionTypes();
String methodName = method.getName();
String delegateMethod = getDelegateMethodNameOfMethod(methodName);
method.setName(delegateMethod);
StringBuilder buffer = new StringBuilder(256);
boolean hasReturnValue = (CtClass.voidType == returnType);
buffer.append("{" + CRLF_TAB);
{
if (hasReturnValue) {
String returnClass = returnType.getName();
buffer.append(returnClass + " " + METHOD_RUTURN_VALUE_VAR + ";");
}
if (before != null) {
buffer.append(before);
}
buffer.append(CRLF_TAB);
buffer.append("try {" + CRLF_2TAB);
{
if (beforeAround != null) {
buffer.append(beforeAround);
}
buffer.append(CRLF_2TAB);
if (hasReturnValue) {
buffer.append(METHOD_RUTURN_VALUE_VAR + " = ($r)" + delegateMethod + "($$);");
} else {
buffer.append(delegateMethod + "($$);");
}
if (afterAround != null) {
buffer.append(CRLF_2TAB);
buffer.append(afterAround);
}
if (hasReturnValue) {
buffer.append(CRLF_2TAB);
buffer.append("return " + METHOD_RUTURN_VALUE_VAR);
}
}
buffer.append(CRLF_TAB);
buffer.append("} catch (Throwable ex) {");
{
buffer.append(CRLF_2TAB);
if (afterThrowing != null) {
buffer.append(afterThrowing);
}
buffer.append(CRLF_2TAB);
buffer.append("throw ex;");
}
buffer.append(CRLF_TAB);
buffer.append("}");
if (afterReturning != null) {
buffer.append(CRLF_TAB);
buffer.append("finally {");
{
buffer.append(CRLF_2TAB);
buffer.append(afterReturning);
}
buffer.append(CRLF_TAB);
buffer.append("}");
}
}
buffer.append(CRLF);
buffer.append("}");
System.out.println(methodName + " will be modified as :\n" + buffer.toString());
CtMethod newMethod = CtNewMethod.make(modifiers, returnType, methodName, parameters, exceptions,
buffer.toString(), clazz);
clazz.addMethod(newMethod);
}
/**
* Inject around code to the specified constructor
*
* @see #injectAround(CtClass, CtConstructor, String, String, String,
* String)
*/
public void injectAround(CtConstructor constructor, String beforeAround, String afterAround, String afterThrowing,
String afterReturning) throws Exception {
CtClass clazz = constructor.getDeclaringClass();
injectAround(clazz, constructor, beforeAround, afterAround, afterThrowing, afterReturning);
}
/**
* Inject around code to the specified constructor
*
* <pre>
* <code>
* try{
* <span style="font-size:12px; color:green;">beforeAround block ... </span>
* $procced($$);
* <span style="font-size:12px; color:green;">afterAround block ... </span>
* }catch (Throwable ex){
* <span style="font-size:12px; color:green;">afterThrowing block ... </span>
* }finally{
* <span style="font-size:12px; color:green;">afterReturning block ... </span>
* }
* </code>
* </pre>
*
*/
public void injectAround(CtClass clazz, CtConstructor constructor, String beforeAround, String afterAround,
String afterThrowing, String afterReturning) throws Exception {
clazz.defrost();
String delegateMethodName = getDelegateMethodNameOfConstructor(constructor.getName());
CtMethod delegateMethod = constructor.toMethod(delegateMethodName, clazz);
clazz.addMethod(delegateMethod);
injectAround(clazz, delegateMethod, beforeAround, afterAround, afterThrowing, afterReturning);
constructor.setBody("{" + PROCEED + "($$);", "this", delegateMethodName);
}
/**
* Copy form the src method's body to a overrid method's body in target
* class.
*
* @param targetClass
* overrid the method in the target class
* @param srcMethod
* the overrid from will copy from the src method. If the target
* class has not owner overrid method , you should specified the
* srcMethod in the super class.
* @param body
* the body of the override method
* @param delegateObject
* the delegate object default value is "this".
* @param delegateMethod
* @throws Exception
*/
public void overrideMethod(CtClass targetClass, CtMethod srcMethod, String body, String delegateObject,
String delegateMethod) throws Exception {
targetClass.defrost();
System.out.println(body);
if (delegateObject == null) {
delegateObject = "this";
}
// override method in a super class of the target class
if (srcMethod.getDeclaringClass() != targetClass) {
CtMethod destMethod = CtNewMethod.copy(srcMethod, targetClass, null);
if (body != null && !body.isEmpty()) {
if (delegateMethod != null && !delegateMethod.isEmpty()) {
destMethod.setBody(body, delegateObject, delegateMethod);
} else {
destMethod.setBody(body);
}
}
targetClass.addMethod(destMethod);
}
// override method in the target class
else {
if (delegateMethod != null && !delegateMethod.isEmpty()) {
srcMethod.setBody(body, delegateObject, delegateMethod);
} else {
srcMethod.setBody(body);
}
}
}
}
injectInterceptor()的 实现原理:将原来的方法改名为一个delegateMethod,重新创建一个target method,方法体是织入代码,并调用delegateMethod。
injectAround(CtConstrouctor)的实现原理:先将构造体的内容提取到一个delegateMethod中,再对delegateMethod做织入,最后设置新的构建体。在新的构造体中调用delegateMethod。
Javassist 通用工具之 CodeInjector的更多相关文章
- 用 javassist 来修改 class 文件
import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; public class Test { ...
- SSH整合时执行hibernate查询报错:java.lang.ClassCastException: com.ch.hibernate.Department_$$_javassist_0 cannot be cast to javassist.util.proxy
今天在整合ssh三个框架时,有一个功能,是查询所有员工信息,且员工表和部门表是多对一的映射关系,代码能正常运行到查询得到一个List集合,但在页面展示的时候,就报异常了, java.lang.Clas ...
- Java动态编程初探——Javassist
最近需要通过配置生成代码,减少重复编码和维护成本.用到了一些动态的特性,和大家分享下心得. 我们常用到的动态特性主要是反射,在运行时查找对象属性.方法,修改作用域,通过方法名称调用方法等.在线的应用不 ...
- Java javassist动态代理
package org.windwant.spring.core.proxy; import javassist.ClassPool; import javassist.CtClass; import ...
- Javassist 字节码操作
1.读写字节码 Javassist是用来处理java字节码的类库.字节码保存在二进制文件中称为类文件.每个类文件夹包括一个java类或接口. Javasssist.CtClass这个类是一个类文件的抽 ...
- 【hibernate 报错】No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer 【get和load的区别】
报错: HTTP Status 500 - Could not write content: No serializer found for class org.hibernate.proxy.poj ...
- Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类
Android RecyclerView单击.长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类 我写的附录文章2,介绍了 ...
- javassist AOP
对于AOP,这个概念,不用解释,主要用途很多,我这里主要是为了后续研究如何实现APM做准备.前面研究了动态代理实现AOP,考虑到性能的问题,改用javassist直接修改直接码实现! javassis ...
- org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.
2011-08-16 13:26:58,484 [http-8080-1] ERROR [core.web.ExceptionInterceptor] - org.codehaus.jackson.m ...
随机推荐
- MailKit---状态更改和删除
当我们拉取邮件列表,并展示邮件后需要打开邮件,同时标识本邮件状态为已读状态,或者我们直接删除邮件.下面介绍基本的应用. 首先了解邮件的所有枚举状态:MailKit.MessageFlags包括:(No ...
- 易懂 易上手的cookie 最简单明了 js中cookie的使用方法及教程
今天项目中需要用到cookie 看到我的cookie不行了 大喊一声我曹 怎么可以这样 我就疯狂的在网上找 找啊 找 但是我感觉都太官方了 废话不说 看栗子 1.引入jQuery与jQuery.C ...
- git 切换远程分支
http://zhidao.baidu.com/link?url=cuqJsL9skJJn5c556zXfP1dgCAOUK37CDXkNIw_sS0YKmvoROTI0HP7-PbKjgs6Lv4X ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(15)-权限管理系统准备
系列目录 这节我们说下权限系统的特点,本系统采用的是MVC4+EF5+IOC 接口编程的架构,其中的权限树用的是DWTree,功能上做到灵活,授权操控细致,权限可以细到按钮级别 ,为了部署简单,导致设 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(24)-权限管理系统-将权限授权给角色
系列目录 过了个年回来,回顾一下,我们上次讲了角色管理,我们这一次来讲将权限授权给角色,这一节也是大家比较关心的.因为我们已经跑通了整个系统,知道权限的流转,我们先来看一张图 这张图主要分要3块,角色 ...
- 从零开始编写自己的C#框架(14)——T4模板在逻辑层中的应用(三)
原本关于T4模板原想分5个章节详细解说的,不过因为最近比较忙,也不想将整个系列时间拉得太长,所以就将它们整合在一块了,可能会有很多细节没有讲到,希望大家自己对着代码与模板去研究. 本章代码量会比较大, ...
- 设计模式(十三):从“FQ”中来认识代理模式(Proxy Pattern)
我们知道Google早就被墙了,所以FQ才能访问Google呢,这个“FQ”的过程就是一个代理的过程.“代理模式”在之前的博客中不止一次的提及过,之前的委托回调就是代理模式的具体应用.今天我们就从“F ...
- linq to entity常用操作
一.聚合函数查询 ; using (xxxEntities db = new xxxEntities()) { sum = db.userinfo.AsNoTracking().Where(d =&g ...
- C#字符串的倒序输出
介绍 在本文中,我将演示如何将字符串的单词倒序输出.在这里我不是要将“John” 这样的字符串倒序为成“nhoJ”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...
- ASP.NET MVC 登录验证
好久没写随笔了,这段时间没 什么事情,领导 一直没安排任务,索性 一直在研究代码,说实在的,这个登录都 搞得我云里雾里的,所以这次我可能也讲得不是 特别清楚,但是 我尽力把我知道的讲出来,顺便也对自 ...