01: package org.drools.objenesis.instantiator;
02:
03: import java.io.Serializable;
04:
05: /**
06: * Helper for common serialization-compatible instantiation functions
07: *
08: * @author Leonardo Mesquita
09: */
10: public class SerializationInstantiatorHelper {
11:
12: /**
13: * Returns the first non-serializable superclass of a given class. According to Java Object
14: * Serialization Specification, objects read from a stream are initialized by calling an
15: * accessible no-arg constructor from the first non-serializable superclass in the object's
16: * hierarchy, allowing the state of non-serializable fields to be correctly initialized.
17: *
18: * @param type Serializable class for which the first non-serializable superclass is to be found
19: * @return The first non-serializable superclass of 'type'.
20: * @see java.io.Serializable
21: */
22: public static Class getNonSerializableSuperClass(final Class type) {
23: Class result = type;
24: while (Serializable.class.isAssignableFrom(result)) {
25: result = result.getSuperclass();
26: if (result == null) {
27: throw new Error(
28: "Bad class hierarchy: No non-serializable parents");
29: }
30: }
31: return result;
32:
33: }
34: }
|