Home | History | Annotate | Download | only in sun
      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.sun;
     17 
     18 import org.objenesis.ObjenesisException;
     19 import org.objenesis.instantiator.SerializationInstantiatorHelper;
     20 
     21 /**
     22  * Instantiates a class by making a call to internal Sun private methods. It is only supposed to
     23  * work on Sun HotSpot 1.3 JVM. This instantiator will create classes in a way compatible with
     24  * serialization, calling the first non-serializable superclass' no-arg constructor.
     25  *
     26  * @author Leonardo Mesquita
     27  * @see org.objenesis.instantiator.ObjectInstantiator
     28  */
     29 public class Sun13SerializationInstantiator extends Sun13InstantiatorBase {
     30    private final Class superType;
     31 
     32    public Sun13SerializationInstantiator(Class type) {
     33       super(type);
     34       this.superType = SerializationInstantiatorHelper.getNonSerializableSuperClass(type);
     35    }
     36 
     37    public Object newInstance() {
     38       try {
     39          return allocateNewObjectMethod.invoke(null, new Object[] {type, superType});
     40       }
     41       catch(Exception e) {
     42          throw new ObjenesisException(e);
     43       }
     44    }
     45 
     46 }
     47