为什么代理类需要扩展java.lang.reflect.Proxy

时间:2018-01-08 15:57:38

标签: java dynamic proxy

我找到了代理类的定义过程,但是为什么自动生成的代理类必须是Proxy的子类?

sun.misc.ProxyGenerator.java,

/** name of the superclass of proxy classes */
private final static String superclassName = "java/lang/reflect/Proxy";
......
try {
        /*
         * Write all the items of the "ClassFile" structure.
         * See JVMS section 4.1.
         */
                                    // u4 magic;
        dout.writeInt(0xCAFEBABE);
                                    // u2 minor_version;
        dout.writeShort(CLASSFILE_MINOR_VERSION);
                                    // u2 major_version;
        dout.writeShort(CLASSFILE_MAJOR_VERSION);

        cp.write(dout);             // (write constant pool)

                                    // u2 access_flags;
        dout.writeShort(accessFlags);
                                    // u2 this_class;
        dout.writeShort(cp.getClass(dotToSlash(className)));


        // why jdk do this ???
                                    // u2 super_class;
        dout.writeShort(cp.getClass(superclassName));

                                    // u2 interfaces_count;
        dout.writeShort(interfaces.length);
                                    // u2 interfaces[interfaces_count];
        for (Class<?> intf : interfaces) {
            dout.writeShort(cp.getClass(
                dotToSlash(intf.getName())));
        }

0 个答案:

没有答案