Java.io.ObjectInputStream.resolveObject() 方法
描述
java.io.ObjectInputStream.resolveProxyClass(String[] interfaces) 方法返回一个代理类,它实现了代理类描述符中命名的接口; 子类可以实现此方法以从流中读取自定义数据以及动态代理类的描述符,从而允许它们为接口和代理类使用备用加载机制。
声明
以下是 java.io.ObjectInputStream.resolveObject() 方法的声明。
protected Class<?> resolveProxyClass(String[] interfaces)
参数
interfaces − 代理类描述符中反序列化的接口名称列表
返回值
此方法返回指定接口的代理类。
异常
IOException − 底层 InputStream 抛出的任何异常。
ClassNotFoundException − 如果找不到代理类或任何命名接口。
示例
下面的例子展示了 java.io.ObjectInputStream.resolveProxyClass() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class ObjectInputStreamDemo extends ObjectInputStream {
public ObjectInputStreamDemo(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
ObjectInputStreamDemo ois = new ObjectInputStreamDemo(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

