2 vish23n vish23n 于 2014.08.13 00:09 提问

使用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);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!