Java - ObjectInputStream.GetField getObjectStreamClass() method



Description

The Java ObjectInputStream.getField getObjectStreamClass() method gets the ObjectStreamClass that describes the fields in the stream. It returns the ObjectStreamClass associated with the field set being deserialized. You can use it to inspect metadata about the serialized class—like field names, types, etc.

Declaration

Following is the declaration for java.io.ObjectInputStream.getField.getObjectStreamClass() method.

public abstract ObjectStreamClass getObjectStreamClass()

Parameters

NA

Return Value

This method returns the descriptor class that describes the serializable fields.

Exception

NA

Example - Usage of ObjectInputStream.getField getObjectStreamClass() method

The following example shows the usage of ObjectInputStream.getField getObjectStreamClass() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;

public class ObjectInputStreamDemo implements Serializable {
   public static void main(String[] args) {
      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.writeObject(new Example());
         oout.flush();
         oout.close();

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

         // read an object from the stream and cast it to Example
         Example a = (Example) ois.readObject();

         // print var of a
         System.out.println("" + a.var);

         // get and print the objectStreamClass
         System.out.println("" + a.v);

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   static public class Example implements Serializable {
      static String var = "Hello World";
      static ObjectStreamClass v;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", String.class)
      };

      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // get the field and assign it at var
         ObjectInputStream.GetField fields = in.readFields();

         // get var
         var = (String) fields.get("var", "reset");
         v = fields.getObjectStreamClass();
      }

      private void writeObject(ObjectOutputStream out) throws IOException {

         // write into the ObjectStreamField array the variable string
         ObjectOutputStream.PutField fields = out.putFields();
         fields.put("var", var);
         out.writeFields();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello World
com.tutorialspoint.ObjectInputStreamDemo$Example: static final long serialVersionUID = 1534274623819585475L;

Example - Print field names and types during deserialization

The following example shows the usage of ObjectInputStream.getField getObjectStreamClass() method. During deserialization, we're using getObjectStreamClass() to list all fields that were serialized.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;

public class ObjectInputStreamDemo {
   public static void main(String[] args) throws Exception {
      String file = "person_objclass1.bin";

      // Serialize
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
         Person p = new Person("Alice", 25);
         oos.writeObject(p);
      }

      // Deserialize
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
         Person p = (Person) ois.readObject(); // triggers readObject internally
      }
   }

   static class Person implements Serializable {
      private static final long serialVersionUID = 1L;
      String name;
      int age;

      public Person(String name, int age) {
         this.name = name;
         this.age = age;
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         ObjectStreamClass osc = fields.getObjectStreamClass();

         System.out.println("Fields in ObjectStreamClass:");
         for (ObjectStreamField f : osc.getFields()) {
            System.out.println("- " + f.getName() + " (" + f.getType().getSimpleName() + ")");
         }
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Fields in ObjectStreamClass:
- name (String)
- age (int)

Explanation

  • Object is serialized, and then later de-serialized and its fields are output in the console.

Example - Getting metadata of class

The following example shows the usage of ObjectInputStream.getField getObjectStreamClass() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;

public class ObjectInputStreamDemo {
	public static void main(String[] args) throws Exception {
        // Serialize
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(new Person());
        }

        // Deserialize
        try (ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(bos.toByteArray()))) {
            Person p = (Person) ois.readObject();
        }
    }
}

class Person implements Serializable {
    private String name;
    private int age;

    private void readObject(ObjectInputStream in) 
            throws IOException, ClassNotFoundException {
        
        ObjectInputStream.GetField fields = in.readFields();
        ObjectStreamClass desc = fields.getObjectStreamClass();
        
        System.out.println("Serialized class: " + desc.getName());
        System.out.println("SerialVersionUID: " + desc.getSerialVersionUID());
        
        this.name = (String) fields.get("name", "Unknown");
        this.age = fields.get("age", 0);
    }
}

Output

Let us compile and run the above program, this will produce the following result−

Serialized class: com.tutorialspoint.Person
SerialVersionUID: -5242539655975973321

Explanation

  • getObjectStreamClass() returns metadata about the serialized class.

  • Provides −

    • Class name (desc.getName())

    • SerialVersionUID (desc.getSerialVersionUID())

java_io_objectinputstream.getfield.htm
Advertisements