1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| package com.xiaoguan.javassist;
import com.xiaoguan.javassist.bank.dao.AccountDao; import javassist.CannotCompileException; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import org.junit.Test;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays;
public class JavassistTest { @Test public void testGenerateAccountDaoImpl() throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.makeClass("com.xiaoguan.bank.dao.impl.AccountDaoImpl"); CtClass ctInterface = pool.makeInterface("com.xiaoguan.javassist.bank.dao.AccountDao"); ctClass.addInterface(ctInterface); Method[] declaredMethod = AccountDao.class.getDeclaredMethods(); Arrays.stream(declaredMethod).forEach(method -> { try { StringBuilder methodCode=new StringBuilder(); methodCode.append("public "); methodCode.append(method.getReturnType().toString().replace("class","")); methodCode.append(" "); methodCode.append(method.getName()); methodCode.append("("); Class<?>[] parameterTypes = method.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { Class<?> parameterType = parameterTypes[i]; methodCode.append(parameterType.getName()); methodCode.append(" "); methodCode.append("arg"+i); if(i!=parameterTypes.length-1){ methodCode.append(","); } } methodCode.append("){System.out.println(8888);"); String simpleName = method.getReturnType().getSimpleName(); if ("void".equals(simpleName)) {
} else if ("int".equals(simpleName)) { methodCode.append("return 1;"); } else if ("String".equals(simpleName)) { 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<?> aClass = ctClass.toClass(); AccountDao accountDao = (AccountDao) aClass.newInstance(); accountDao.insert("aaaa"); accountDao.delete(); accountDao.update("111",12.0); accountDao.selectByActno("111");
} @Test public void testGenerateImpl() throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.makeClass("com.xiaoguan.bank.dao.impl.AccountDaoImpl"); CtClass ctInterface = pool.makeInterface("com.xiaoguan.javassist.bank.dao.AccountDao"); ctClass.addInterface(ctInterface); CtMethod ctMethod = CtMethod.make("public void delete(){System.out.print(\"hello delete!\");}", ctClass);
ctClass.addMethod(ctMethod); Class<?> aClass = ctClass.toClass(); AccountDao account = (AccountDao) aClass.newInstance(); account.delete(); } @Test public void testGenerateFirstClass() throws CannotCompileException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.makeClass("com.xiaoguan.bank.dao.impl.AccountDaoImpl"); String methodCode="public void insert(){System.out.print(123456789);}"; CtMethod ctMethod = CtMethod.make(methodCode, ctClass); ctClass.addMethod(ctMethod); ctClass.toClass(); Class<?> aClass = Class.forName("com.xiaoguan.bank.dao.impl.AccountDaoImpl"); Object obj = aClass.newInstance(); Method insertMethod = aClass.getDeclaredMethod("insert"); insertMethod.invoke(obj);
} }
|