下面列出了java.io.ObjectStreamClass#forClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
output.writeObject(new TestClass());
ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
TestObjectInputStream input = new TestObjectInputStream(bais);
input.readObject();
ObjectStreamClass osc = input.getDescriptor();
// All OSC public API methods should complete without throwing.
osc.getName();
osc.forClass();
osc.getField("str");
osc.getFields();
osc.getSerialVersionUID();
osc.toString();
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
output.writeObject(new TestClass());
ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
TestObjectInputStream input = new TestObjectInputStream(bais);
input.readObject();
ObjectStreamClass osc = input.getDescriptor();
// All OSC public API methods should complete without throwing.
osc.getName();
osc.forClass();
osc.getField("str");
osc.getFields();
osc.getSerialVersionUID();
osc.toString();
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
Class<?> cl = desc.forClass();
if (ProxyFactory.isProxyClass(cl)) {
writeBoolean(true);
Class<?> superClass = cl.getSuperclass();
Class<?>[] interfaces = cl.getInterfaces();
byte[] signature = ProxyFactory.getFilterSignature(cl);
String name = superClass.getName();
writeObject(name);
// we don't write the marker interface ProxyObject
writeInt(interfaces.length - 1);
for (int i = 0; i < interfaces.length; i++) {
Class<?> interfaze = interfaces[i];
if (interfaze != ProxyObject.class && interfaze != Proxy.class) {
name = interfaces[i].getName();
writeObject(name);
}
}
writeInt(signature.length);
write(signature);
} else {
writeBoolean(false);
super.writeClassDescriptor(desc);
}
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
output.writeObject(new TestClass());
ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
TestObjectInputStream input = new TestObjectInputStream(bais);
input.readObject();
ObjectStreamClass osc = input.getDescriptor();
// All OSC public API methods should complete without throwing.
osc.getName();
osc.forClass();
osc.getField("str");
osc.getFields();
osc.getSerialVersionUID();
osc.toString();
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
output.writeObject(new TestClass());
ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
TestObjectInputStream input = new TestObjectInputStream(bais);
input.readObject();
ObjectStreamClass osc = input.getDescriptor();
// All OSC public API methods should complete without throwing.
osc.getName();
osc.forClass();
osc.getField("str");
osc.getFields();
osc.getSerialVersionUID();
osc.toString();
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
output.writeObject(new TestClass());
ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
TestObjectInputStream input = new TestObjectInputStream(bais);
input.readObject();
ObjectStreamClass osc = input.getDescriptor();
// All OSC public API methods should complete without throwing.
osc.getName();
osc.forClass();
osc.getField("str");
osc.getFields();
osc.getSerialVersionUID();
osc.toString();
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
output.writeObject(new TestClass());
ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
TestObjectInputStream input = new TestObjectInputStream(bais);
input.readObject();
ObjectStreamClass osc = input.getDescriptor();
// All OSC public API methods should complete without throwing.
osc.getName();
osc.forClass();
osc.getField("str");
osc.getFields();
osc.getSerialVersionUID();
osc.toString();
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
Class<?> clazz = desc.forClass();
if (clazz.isPrimitive() || clazz.isArray()) {
write(0);
super.writeClassDescriptor(desc);
} else {
write(1);
writeUTF(desc.getName());
}
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
Class<?> clazz = desc.forClass();
if (clazz.isPrimitive() || clazz.isArray() || clazz.isInterface() ||
desc.getSerialVersionUID() == 0) {
write(TYPE_FAT_DESCRIPTOR);
super.writeClassDescriptor(desc);
} else {
write(TYPE_THIN_DESCRIPTOR);
writeUTF(desc.getName());
}
}
SerializableDataKey(ObjectStreamClass desc, boolean store) {
Class<?> forClass = desc.forClass();
if (forClass != null) {
if (store) {
throw new AssertionError("Must not store ObjectStreamClass instances with strong references to classes");
} else if (ObjectStreamClass.lookup(forClass) == desc) {
this.klazz = new WeakReference<>(forClass);
}
}
this.hashCode = (3 * desc.getName().hashCode()) ^ (7 * (int) (desc.getSerialVersionUID() >>> 32))
^ (11 * (int) desc.getSerialVersionUID());
this.osc = desc;
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
Class<?> clazz = desc.forClass();
if (clazz.isPrimitive() || clazz.isArray()) {
write(0);
super.writeClassDescriptor(desc);
} else {
write(1);
writeUTF(desc.getName());
}
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException
{
Class<?> clazz = desc.forClass();
if( clazz.isPrimitive() || clazz.isArray() )
{
write(0);
super.writeClassDescriptor(desc);
}
else
{
write(1);
writeUTF(desc.getName());
}
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException
{
Class<?> clazz = desc.forClass();
if( clazz.isPrimitive() || clazz.isArray() )
{
write(0);
super.writeClassDescriptor(desc);
}
else
{
write(1);
writeUTF(desc.getName());
}
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException
{
Class<?> clazz = desc.forClass();
if( clazz.isPrimitive() || clazz.isArray() )
{
write(0);
super.writeClassDescriptor(desc);
}
else
{
write(1);
writeUTF(desc.getName());
}
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc)
throws IOException
{
Class<?> clazz = desc.forClass();
if (clazz == null)
{
throw new RuntimeException();
}
int classIdx = virtualizationSerializer.getClassDescriptorIdx(clazz);
writeIntCompressed(classIdx);
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException
{
Class<?> clazz = desc.forClass();
if( clazz.isPrimitive() || clazz.isArray() )
{
write(0);
super.writeClassDescriptor(desc);
}
else
{
write(1);
writeUTF(desc.getName());
}
}
public SerializableDataKey(ObjectStreamClass desc, boolean store) throws IOException {
Class<?> forClass = desc.forClass();
if (forClass != null) {
if (store) {
throw new AssertionError("Must not store ObjectStreamClass instances with strong references to classes");
} else if (ObjectStreamClass.lookup(forClass) == desc) {
this.klazz = new WeakReference<>(forClass);
}
}
this.hashCode = (3 * desc.getName().hashCode()) ^ (7 * (int) (desc.getSerialVersionUID() >>> 32))
^ (11 * (int) desc.getSerialVersionUID());
this.osc = desc;
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
Class<?> clazz = desc.forClass();
if (clazz.isPrimitive() || clazz.isArray() || clazz.isInterface() ||
desc.getSerialVersionUID() == 0) {
write(TYPE_FAT_DESCRIPTOR);
super.writeClassDescriptor(desc);
} else {
write(TYPE_THIN_DESCRIPTOR);
writeUTF(desc.getName());
}
}
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException
{
Class<?> clazz = desc.forClass();
if( clazz.isPrimitive() || clazz.isArray() )
{
write(0);
super.writeClassDescriptor(desc);
}
else
{
write(1);
writeUTF(desc.getName());
}
}
/**
* Read the first class descriptor which indicates the serialized class, then throws a {@link ClassFoundException}
* containing that class to avoid further reading
*/
@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
ObjectStreamClass descriptor = super.readClassDescriptor();
throw new ClassFoundException(descriptor.forClass(), descriptor.getName());
}