Home | History | Annotate | Download | only in strategy
      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.strategy;
     17 
     18 import java.io.NotSerializableException;
     19 import java.io.Serializable;
     20 
     21 import org.objenesis.ObjenesisException;
     22 import org.objenesis.instantiator.ObjectInstantiator;
     23 import org.objenesis.instantiator.android.AndroidSerializationInstantiator;
     24 import org.objenesis.instantiator.basic.ObjectStreamClassInstantiator;
     25 import org.objenesis.instantiator.gcj.GCJSerializationInstantiator;
     26 import org.objenesis.instantiator.perc.PercSerializationInstantiator;
     27 import org.objenesis.instantiator.sun.Sun13SerializationInstantiator;
     28 
     29 /**
     30  * Guess the best serializing instantiator for a given class. The returned instantiator will
     31  * instantiate classes like the genuine java serialization framework (the constructor of the first
     32  * not serializable class will be called). Currently, the selection doesn't depend on the class. It
     33  * relies on the
     34  * <ul>
     35  * <li>JVM version</li>
     36  * <li>JVM vendor</li>
     37  * <li>JVM vendor version</li>
     38  * </ul>
     39  * However, instantiators are stateful and so dedicated to their class.
     40  *
     41  * @author Henri Tremblay
     42  * @see ObjectInstantiator
     43  */
     44 public class SerializingInstantiatorStrategy extends BaseInstantiatorStrategy {
     45 
     46    /**
     47     * Return an {@link ObjectInstantiator} allowing to create instance following the java
     48     * serialization framework specifications.
     49     *
     50     * @param type Class to instantiate
     51     * @return The ObjectInstantiator for the class
     52     */
     53    public ObjectInstantiator newInstantiatorOf(Class type) {
     54       if(!Serializable.class.isAssignableFrom(type)) {
     55          throw new ObjenesisException(new NotSerializableException(type+" not serializable"));
     56       }
     57       if(JVM_NAME.startsWith(SUN)) {
     58          if(VM_VERSION.startsWith("1.3")) {
     59             return new Sun13SerializationInstantiator(type);
     60          }
     61       }
     62       else if(JVM_NAME.startsWith(DALVIK)) {
     63          return new AndroidSerializationInstantiator(type);
     64       }
     65       else if(JVM_NAME.startsWith(GNU)) {
     66          return new GCJSerializationInstantiator(type);
     67       }
     68       else if(JVM_NAME.startsWith(PERC)) {
     69     	  return new PercSerializationInstantiator(type);
     70       }
     71 
     72       return new ObjectStreamClassInstantiator(type);
     73    }
     74 
     75 }
     76