1 /** 2 * Copyright 2006-2013 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.objenesis.instantiator; 17 18 import java.io.Serializable; 19 20 /** 21 * Helper for common serialization-compatible instantiation functions 22 * 23 * @author Leonardo Mesquita 24 */ 25 public class SerializationInstantiatorHelper { 26 27 /** 28 * Returns the first non-serializable superclass of a given class. According to Java Object 29 * Serialization Specification, objects read from a stream are initialized by calling an 30 * accessible no-arg constructor from the first non-serializable superclass in the object's 31 * hierarchy, allowing the state of non-serializable fields to be correctly initialized. 32 * 33 * @param type Serializable class for which the first non-serializable superclass is to be found 34 * @return The first non-serializable superclass of 'type'. 35 * @see java.io.Serializable 36 */ 37 public static Class getNonSerializableSuperClass(Class type) { 38 Class result = type; 39 while(Serializable.class.isAssignableFrom(result)) { 40 result = result.getSuperclass(); 41 if(result == null) { 42 throw new Error("Bad class hierarchy: No non-serializable parents"); 43 } 44 } 45 return result; 46 47 } 48 } 49