问题描述
- 使用ClassPool生成代理类 报错javassist.CannotCompileException
-
javassist.CannotCompileException: [source error] TryTest(com.asp.eb.TestPacket) not found in java.lang.Object代码是这样的:
ClassPool pool = ClassPool.getDefault(); pool.insertClassPath(new ClassClassPath(clazz)); CtClass oldClass = pool.get(clazz.getName()); CtClass ct = pool.makeClass(new StringBuffer().append(oldClass.getName()).append("$Proxy").toString()); CtClass superCt = pool.get(HttpPacketHandler.class.getName()); ct.addInterface(superCt); StringBuffer sb = new StringBuffer("public void handle (com.asp.eb.TestPacket packet)throws Exception{"); sb.append("int code = $1.getPacketId();"); sb.append("switch(code){"); Iterator ite =opMethods.entrySet().iterator(); while(ite.hasNext()){ Map.Entry entry = (Map.Entry)ite.next(); sb.append("case ").append(entry.getKey()).append(":"); sb.append("super.").append((String)entry.getValue()).append("($$);"); sb.append("break;"); opcodes.add(((Integer)entry.getKey()).intValue()); } sb.append("}"); sb.append("}"); System.out.println("----------------------------------------"); System.out.println(sb.toString()); System.out.println("----------------------------------------"); CtMethod method = CtMethod.make(sb.toString(),ct);
时间: 2024-12-04 07:26:54