Java - ObjectStreamClass lookup(Class<?> cl) method



Description

The Java ObjectStreamClass lookup(Class<?> cl) method finds the descriptor for a class that can be serialized. Creates an ObjectStreamClass instance if one does not exist yet for class. Null is returned if the specified class does not implement java.io.Serializable or java.io.Externalizable.

  • ObjectStreamClass.lookup(Class<?> c) returns an ObjectStreamClass that describes how the class c will be serialized.

  • If the class doesn't implement Serializable, it returns null.

  • You can then inspect things like −

    • serialVersionUID

    • serializable fields

    • class name

    • class reference

Declaration

Following is the declaration for java.io.ObjectStreamClass.lookup(Class<?> cl) method.

public static ObjectStreamClass lookup(Class<?> cl)

Parameters

cl − class for which to get the descriptor.

Return Value

This method returns the class descriptor for the specified class.

Exception

NA

Example - Usage of ObjectStreamClass lookup(Class<?> cl) method

The following example shows the usage of ObjectStreamClass lookup(Class<?> cl) method.

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.util.Calendar;

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
   
      // create a new object stream class for Integers
      ObjectStreamClass osc = ObjectStreamClass.lookup(Integer.class);

      // get the name for Integers
      System.out.println("" + osc.getName());

      // create a new object stream class for Calendar
      ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);

      // get the name for Calendar
      System.out.println("" + osc2.getName());
   }
}

Output

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

java.lang.Integer
java.util.Calendar

Example - Lookup a user-defined class and print its serialVersionUID

The following example shows the usage of ObjectStreamClass lookup(Class<?> cl) method. We're checking the serialVersionUID and confirm that the class is serializable.

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.Serializable;

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(Employee.class);

      if (osc != null) {
         System.out.println("Class: " + osc.getName());
         System.out.println("serialVersionUID: " + osc.getSerialVersionUID());
      } else {
         System.out.println("Employee is not serializable.");
      }
   }

   static class Employee implements Serializable {
      private static final long serialVersionUID = 100L;
      String name;
      int id;
   }
}

Output

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

Class: com.tutorialspoint.ObjectStreamClassDemo$Employee
serialVersionUID: 100

Explanation

  • lookup(Employee.class) returns metadata for the Employee class.

  • This includes the name and serialVersionUID.

Example - Lookup a non-serializable class and handle the null result

The following example shows the usage of ObjectStreamClass lookup(Class<?> cl) method. We're detecting whether a class can be serialized.

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(Config.class);

      if (osc == null) {
         System.out.println("Config is NOT serializable.");
      } else {
         System.out.println("Config is serializable with UID: " + osc.getSerialVersionUID());
      }
   }

   // Not implementing Serializable
   static class Config {
      String key;
      String value;
   }
}

Output

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

Config is NOT serializable.

Explanation

  • Since Config doesn't implement Serializable, lookup() returns null.

  • Always a good idea to check before proceeding with serialization logic.

java_io_objectstreamclass.htm
Advertisements