| 20 | public static String clsName = "hello"; |
| 21 | |
| 22 | @Override |
| 23 | public CtClass genMethod(String mName,String mContext) throws NotFoundException { |
| 24 | clsName = "tmethod" + System.nanoTime(); |
| 25 | |
| 26 | log.info("开始生成模板类:" + clsName); |
| 27 | CtClass clzz = ClassPool.getDefault().makeClass(clsName); |
| 28 | if ((clzz.getDeclaredConstructors()).length != 0) { |
| 29 | clzz.removeConstructor(clzz.getDeclaredConstructors()[0]); |
| 30 | } |
| 31 | try { |
| 32 | System.out.println(mContext); |
| 33 | clzz.addMethod(CtMethod.make(mContext,clzz)); //添加方法 |
| 34 | log.info("生成模板类:" + clsName + "成功"); |
| 35 | log.info("开始生成模板方法:" + mName); |
| 36 | |
| 37 | }catch (Exception e){ |
| 38 | log.error("生成模板类失败:" + clsName); |
| 39 | log.error(e.getMessage()); |
| 40 | } |
| 41 | return clzz; |
| 42 | } |
| 43 | |
| 44 | public static void main(String[] args) { |
| 45 | String mName = "hello"; |