📜  Java.io.ObjectStreamClass 在Java中

📅  最后修改于: 2022-05-13 01:54:43.227000             🧑  作者: Mango

Java.io.ObjectStreamClass 在Java中

这个类是类的序列化描述符。它包含类的名称和 serialVersionUID。可以使用查找方法找到/创建此Java VM 中加载的特定类的 ObjectStreamClass。它扩展类 Object 并实现可序列化。字段:静态 ObjectStreamField[] NO_FIELDS – 这是 serialPersistentFields 值,表示没有可序列化的字段。方法:

  1. Class forClass():该方法返回该版本映射到的本地VM中的类。如果没有对应的本地类,则返回 Null。
Syntax: public Class forClass()
Returns: the Class instance that this descriptor represents
Exception: NA
  1. static ObjectStreamClass lookup(Class class):查找可以序列化的类的描述符。如果类尚不存在,则创建一个 ObjectStreamClass 实例。如果指定的类没有实现Java.io.Serializable 或Java.io.Externalizable,则返回 Null。
Syntax: public static ObjectStreamClass lookup(Class cl)
Return: the class descriptor for the specified class
Exception: NA
  1. static ObjectStreamClass lookupAny(Class class):返回任意类的描述符,不管它是否实现了Serializable。
Syntax: public static ObjectStreamClass lookupAny(Class class)
Return: the class descriptor for the specified class
Exception: NA
Java
// Java code illustrating forClass(),
// lookup() and lookupAny() method
 
import java.io.ObjectStreamClass;
import java.util.ArrayList;
 
 
public class ObjectStreamDemo
{
    public static void main(String arg[])
    {
        // creating object stream class for Number
        ObjectStreamClass geeks_stream
            = ObjectStreamClass.lookup(Number.class);
        ObjectStreamClass quiz_stream
            = ObjectStreamClass.lookupAny(ArrayList.class);
         
        // checking class instance
        System.out.println(geeks_stream.forClass());
        System.out.println(quiz_stream.forClass());
         
    }
     
}


Java
// Java code illustrating getField(), toString()
// getClass(), getSerialVersionUID()
 
import java.io.ObjectStreamClass;
import java.util.ArrayList;
 
 
public class ObjectStreamDemo
{
    public static void main(String arg[])
    {
        // creating object stream class for Number
        ObjectStreamClass geeks_stream
            = ObjectStreamClass.lookup(Number.class);
         
        // checking field
        System.out.println(geeks_stream.getField("quiz_stream"));
        System.out.println(geeks_stream.getFields());
         
        // class name
        System.out.println("class name: " + geeks_stream.getClass());
         
        // checking serial version UID
        System.out.println(geeks_stream.getSerialVersionUID());
         
        System.out.println(geeks_stream.toString());
    }
     
}


  1. 输出:
class java.lang.Number
class java.util.ArrayList
  1. ObjectStreamField getField(String name):通过名称获取该类的字段。
Syntax: public ObjectStreamField getField(String name)
Return: The ObjectStreamField object of the named 
field or null if there is no such named field.
Exception: NA
  1. ObjectStreamField[] getFields():返回这个可序列化类的字段数组。
Syntax: public ObjectStreamField[] getFields()
Returns: an array containing an element for each 
persistent field of this class. Returns an array of length zero if
 there are no fields.
Exception: NA
  1. String getName():返回此描述符所描述的类的名称。此方法以 Class.getName() 方法使用的格式返回类的名称。
Syntax: public String getName()
Return: a string representing the name of the class
Exception: NA
  1. long getSerialVersionUID():返回此类的 serialVersionUID。 serialVersionUID 定义了一组具有相同名称的类,这些类都是从一个公共根类演变而来的,并同意使用公共格式进行序列化和反序列化。 NonSerializable 类的 serialVersionUID 为 0L。
Syntax: public long getSerialVersionUID()
Returns: the SUID of the class described by this descriptor
Exception: NA
  1. String toString():返回描述此 ObjectStreamClass 的字符串。
Syntax: public String toString()
Returns: a string representation of the object.
Exception: NA

Java

// Java code illustrating getField(), toString()
// getClass(), getSerialVersionUID()
 
import java.io.ObjectStreamClass;
import java.util.ArrayList;
 
 
public class ObjectStreamDemo
{
    public static void main(String arg[])
    {
        // creating object stream class for Number
        ObjectStreamClass geeks_stream
            = ObjectStreamClass.lookup(Number.class);
         
        // checking field
        System.out.println(geeks_stream.getField("quiz_stream"));
        System.out.println(geeks_stream.getFields());
         
        // class name
        System.out.println("class name: " + geeks_stream.getClass());
         
        // checking serial version UID
        System.out.println(geeks_stream.getSerialVersionUID());
         
        System.out.println(geeks_stream.toString());
    }
     
}
  1. 输出:
null
[Ljava.io.ObjectStreamField;@45ee12a7
class name: class java.io.ObjectStreamClass
-8742448824652078965
java.lang.Number: static final long serialVersionUID = -8742448824652078965L;