ObjectInputStream resolveProxyClass()方法

java.io.ObjectInputStream.resolveProxyClass(String[] interfaces) 方法返回一个实现在代理类的描述符指定接口的代理类;子类可以实现此方法,从随描述符和动态代理类流中读取自定义数据,允许它们使用的替换加载机制的接口和代理类。

1 语法

protected Class<?> resolveProxyClass(String[] interfaces)

2 参数

interfaces:在代理类描述符中反序列化的接口名称列表

3 返回值

返回指定接口的代理类。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.ObjectInputStream.resolveProxyClass(String[] interfaces)方法的例子
 */
import java.io.*;

public class Demo extends ObjectInputStream {

    public Demo(InputStream in) throws IOException {
        super(in);
    }

    public static void main(String[] args) {
        String s = "Hello World";

        try {
            // create a new file with an ObjectOutputStream
            FileOutputStream out = new FileOutputStream("test.txt");
            ObjectOutputStream oout = new ObjectOutputStream(out);

            // write something in the file
            oout.writeUTF(s);
            oout.flush();

            // create an ObjectInputStream for the file we created before
            Demo ois = new Demo(new FileInputStream("test.txt"));

            // create a list that will be used to resolve proxy class
            String[] list = {Serializable.class.getName()};

            // print the class proxy
            System.out.println("" + ois.resolveProxyClass(list));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

输出结果为:

class com.sun.proxy.$Proxy0

热门文章

优秀文章