Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - ObjectOutputStream annotateProxyClass(Class<?> cl) method



Description

The Java ObjectOutputStream annotateProxyClass(Class<?> cl) can be implemented by subclasses to store custom data in the stream along with descriptors for dynamic proxy classes.

This method is called exactly once for each unique proxy class descriptor in the stream. The default implementation of this method in ObjectOutputStream does nothing.

The corresponding method in ObjectInputStream is resolveProxyClass. For a given subclass of ObjectOutputStream that overrides this method, the resolveProxyClass method in the corresponding subclass of ObjectInputStream must read any data or objects written by annotateProxyClass.

What is annotateProxyClass() used for?

  • It's called by ObjectOutputStream during serialization when a proxy class is written.

  • Allows you to add custom annotations or metadata to the stream.

  • Useful for advanced scenarios like custom class loading or remote method invocation (RMI).

Declaration

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

protected void annotateProxyClass(Class<?> cl)

Parameters

cl − The class to annotate custom data for.

Return Value

This method does not return a value.

Exception

IOException − Any exception thrown by the underlying OutputStream.

Example - Usage of ObjectOutputStream annotateProxyClass(Class<?> cl) method

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

ObjectOutputStreamDemo.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.OutputStream;

public class ObjectOutputStreamDemo extends ObjectOutputStream {

   public ObjectOutputStreamDemo(OutputStream out) throws IOException {
      super(out);
   }

   public static void main(String[] args) {
      int i = 319874;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStreamDemo oout = new ObjectOutputStreamDemo(out);

         // write something in the file
         oout.writeInt(i);
         oout.writeInt(1653984);
         oout.flush();

         // call annotateProxyClass but it does nothing
         oout.annotateProxyClass(Integer.class);

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

         // read and print an int
         System.out.println("" + ois.readInt());

         // read and print an int
         System.out.println("" + ois.readInt());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

319874
1653984

Example - Log Proxy Class Serialization

The following example shows the usage of ObjectOutputStream annotateProxyClass(Class<?> cl) method. We're printing the proxy class name being serialized.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      InvocationHandler handler = new LoggingHandler();
      Serializable proxyInstance = (Serializable) Proxy.newProxyInstance(
         ObjectOutputStreamDemo.class.getClassLoader(),
         new Class<?>[]{Runnable.class, Serializable.class},
         handler
      );

      try (FileOutputStream fos = new FileOutputStream("proxy1.ser");
         ObjectOutputStream oos = new CustomObjectOutputStream(fos)) {
         oos.writeObject(proxyInstance);
      }
   }

   static class LoggingHandler implements InvocationHandler, Serializable {
      public Object invoke(Object proxy, Method method, Object[] args) {
         return null;
      }
   }

   static class CustomObjectOutputStream extends ObjectOutputStream {
      public CustomObjectOutputStream(OutputStream out) throws IOException {
         super(out);
      }

      @Override
      protected void annotateProxyClass(Class<?> cl) throws IOException {
         System.out.println("Serializing proxy class: " + cl.getName());
      }
   }
}

Output

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

Serializing proxy class: jdk.proxy1.$Proxy0

Explanation

  • The handler (LoggingHandler) implements Serializable.

  • The proxy implements Runnable and Serializable.

Example - Add Custom Annotation Metadata to Proxy Class

The following example shows the usage of ObjectOutputStream annotateProxyClass(Class<?> cl) method. We'll embed custom metadata (like proxy origin info) in the stream for a proxy class

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      InvocationHandler handler = new LoggingHandler();

      Serializable proxyInstance = (Serializable) Proxy.newProxyInstance(
         ObjectOutputStreamDemo.class.getClassLoader(),
         new Class<?>[]{Comparable.class, Serializable.class},
         handler
      );

      try (FileOutputStream fos = new FileOutputStream("proxy2.ser");
         ObjectOutputStream oos = new CustomObjectOutputStream(fos)) {
         oos.writeObject(proxyInstance);
         System.out.println("Proxy serialized successfully.");
      }
   }

   // A simple Serializable InvocationHandler
   static class LoggingHandler implements InvocationHandler, Serializable {
      public Object invoke(Object proxy, Method method, Object[] args) {
         System.out.println("Method called: " + method.getName());
         return null;
      }
   }

   // Custom ObjectOutputStream to override annotateProxyClass
   static class CustomObjectOutputStream extends ObjectOutputStream {
      public CustomObjectOutputStream(OutputStream out) throws IOException {
         super(out);
      }

      @Override
      protected void annotateProxyClass(Class<?> cl) throws IOException {
         System.out.println("annotateProxyClass called for: " + cl.getName());
      }
   }
}

Output

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

annotateProxyClass called for: jdk.proxy1.$Proxy0
Proxy serialized successfully.

Explanation

  • Creates a dynamic proxy for the Comparable interface.

  • Uses a custom ObjectOutputStream that overrides annotateProxyClass.

  • Ensures the proxy is serializable by −

    • Adding Serializable to the proxy interface list.

    • Using a serializable InvocationHandler.

java_io_objectoutputstream.htm
Advertisements