
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectStreamClass lookupAny(Class<?> cl) method
Description
The Java ObjectStreamClass lookupAny(Class<?> cl) method returns the descriptor for any class, regardless of whether it implements Serializable.
lookupAny() is similar to lookup(), but it works for any class, not just serializable ones.
It returns an ObjectStreamClass instance, even if the class does not implement Serializable.
-
It's useful when −
You need to inspect fields or class metadata regardless of serializability.
You're building custom serialization tools or class inspectors.
lookup() returns null for non-serializable classes. lookupAny() never returns null.
Declaration
Following is the declaration for java.io.ObjectStreamClass.lookupAny(Class<?> cl) method.
public static ObjectStreamClass lookupAny(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 lookupAny(Class<?> cl) method
The following example shows the usage of ObjectStreamClass lookupAny(Class<?> cl) method.
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; public class ObjectStreamClassDemo { public static void main(String[] args) { // create a new object stream class for Integers ObjectStreamClass osc = ObjectStreamClass.lookupAny(Integer.class); // get the name for Integers System.out.println("" + osc.getName()); // create a new object stream class for Thread, which is not serializable ObjectStreamClass osc2 = ObjectStreamClass.lookupAny(Thread.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 non-serializable class and print its name
The following example shows the usage of ObjectStreamClass lookupAny(Class<?> cl) method. We're using lookupAny() to inspect a class that is not 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.lookupAny(Settings.class); System.out.println("Class: " + osc.getName()); System.out.println("Is class serializable? " + Serializable.class.isAssignableFrom(osc.forClass())); } static class Settings { String theme; boolean darkMode; } }
Output
Let us compile and run the above program, this will produce the following result−
Class: com.tutorialspoint.ObjectStreamClassDemo$Settings Is class serializable? false
Explanation
Settings does not implement Serializable, but lookupAny() still returns metadata.
We use forClass() and isAssignableFrom() to check if it's serializable.
Example - Compare lookup() and lookupAny() behavior
The following example shows the usage of ObjectStreamClass lookupAny(Class<?> cl) method. We're showcasing how lookup() returns null while lookupAny() still works.
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; public class ObjectStreamClassDemo { public static void main(String[] args) { ObjectStreamClass normal = ObjectStreamClass.lookup(Helper.class); ObjectStreamClass any = ObjectStreamClass.lookupAny(Helper.class); System.out.println("lookup() result: " + (normal == null ? "null" : "found")); System.out.println("lookupAny() result: " + any.getName()); } static class Helper { int version; String type; } }
Output
Let us compile and run the above program, this will produce the following result−
lookup() result: null lookupAny() result: com.tutorialspoint.ObjectStreamClassDemo$Helper
Explanation
lookup() returns null because Helper is not serializable.
lookupAny() still returns a valid ObjectStreamClass.