使用javassist动态生成类
@Test
public void testGennerateFirstClass() throws Exception{
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.makeClass("类名");
String methodCode = "publi coid insert(){System.out.println(123);}";
CtMethod ctMethod = CtMethod.make(methodCode, ctClass);
ctClass.addMethod(ctMethod);
ctClass.toClass();
Class<?> clazz = Class.forName("类名");
Object obj = clazz.newInstance();
Method insertMethod = clazz.getDeclaredMethod("insert");
insertMethod.invoke(obj);
}
使用javassist动态生成类并实现接口
@Test
public void testGenerateImpl() throws Exception{
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.makeClass("类地址");
CtClass ctInterface = pool.makeInterface("类接口地址");
ctClass.addInterface(ctInterface);
CtMethod ctMethod = CtMethod.make("条件执行语句",ctClass);
ctClass.addMethod(ctMethod);
Class<?> clazz = ctClass.toClass();
AccountDao accountDao = (AccountDao) clazz.newInstance();
accountDao.delete();
}
实现接口中所有的方法
public class javassistTest {
@Test
public void testGenerateAccountDaoImpl() throws Exception{
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.makeClass("类名地址");
CtClass ctInterface = pool.makeInterface("接口地址");
ctClass.addInterface(ctInterface);
Method[] methods = AccountDao.class.getDeclaredMethods();
Arrays.stream(methods).forEach(method -> {
try{
StringBuilder methodCode = new StringBuilder();
methodCode.append("public");
methodCode.append(method.getReturnType().getName());
methodCode.append(" ");
methodCode.append(method.getName());
methodCode.append("(");
Class<?>[] paramterTypes = method.getParameterTypes();
for (int i = 0; i < paramterTypes.length; i++) {
Class<?> parameterType = paramterTypes[i];
methodCode.append(parameterType.getName());
methodCode.append(" ");
methodCode.append("arg" + i);
if(i != paramterTypes.length - 1){
methodCode.append(",");
}
}
methodCode.append("){System.out.println(1111);return 1;}}");
String returnTypeSimpleName = method.getReturnType().getSimpleName();
if ("void".equals(returnTypeSimpleName)){
}else if ("int".equals(returnTypeSimpleName)){
methodCode.append("return 1;");
}else if("String".equals(returnTypeSimpleName)){
methodCode.append("return \"hello\";");
}
methodCode.append("}");
System.out.println(methodCode);
CtMethod ctMethod = CtMethod.make(methodCode.toString(), ctClass);
ctClass.addMethod(ctMethod);
}catch (Exception e){
e.printStackTrace();
}
});
Class<?> clazz = ctClass.toClass();
AccountDao accountDao = (AccountDao) clazz.newInstance();
}
工具类GenerateDaoProxy的编写
package com.example.bank.utils;
import org.apache.ibatis.javassist.ClassPool;
import org.apache.ibatis.javassist.CtClass;
import org.apache.ibatis.javassist.CtMethod;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.SqlSession;
import java.lang.reflect.Method;
import java.util.Arrays;
public class GenerateDaoProxy{
public static Object generate(SqlSession sqlSession, Class daoInterface){
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.makeClass(daoInterface.getName() + "Proxy");
CtClass ctInterface = pool.makeInterface(daoInterface.getName());
ctClass.addInterface(ctInterface);
Method[] methods = daoInterface.getDeclaredMethods();
Arrays.stream(methods).forEach(method -> {
try{
StringBuilder methodCode = new StringBuilder();
methodCode.append("public ");
methodCode.append(method.getReturnType().getName());
methodCode.append(" ");
methodCode.append(method.getName());
methodCode.append("(");
Class<?>[] paramterTypes = method.getParameterTypes();
for(int i = 0; i < paramterTypes.length; i++){
Class<?> paramterType = paramterTypes[i];
methodCode.append(paramterType.getName());
methodCode.append(" ");
methodCode.append("arg" + i);
if(i != paramterTypes.length - 1){
methodCode.append(",");
}
}
methodCode.append(")");
methodCode.append("{");
methodCode.append("org.apache.ibatis.session.SqlSession sqlSession = com.example.bank.utils.SqlSessionUtil.openSession();");
String sqlId = daoInterface.getName() + "." + method.getName();
SqlCommandType sqlCommandType = sqlSession.getConfiguration().getMappedStatement(sqlId).getSqlCommandType();
if(sqlCommandType == SqlCommandType.INSERT){
}
if(sqlCommandType == SqlCommandType.DELETE){
}
if(sqlCommandType == SqlCommandType.UPDATE){
methodCode.append("return sqlSession.update(\""+sqlId+"\",arg0);");
}
if(sqlCommandType == SqlCommandType.SELECT){
String returnType = method.getReturnType().getName();
methodCode.append("return ("+returnType+") sqlSession.selectOne(\""+sqlId+"\",arg0);");
}
methodCode.append("}");
CtMethod ctMethod = CtMethod.make(methodCode.toString(), ctClass);
ctClass.addMethod(ctMethod);
}catch(Exception e){
e.printStackTrace();
}
});
Object obj = null;
try{
Class<?> clazz = ctClass.toClass();
obj = clazz.newInstance();
} catch (Exception e){
e.printStackTrace();
}
return obj;
}
}