StdInstantiatorStrategy.java revision eebfcaffb2d52b214abfdb1c0102395c88c9db54
1/** 2 * Copyright 2006-2017 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 */ 16package org.objenesis.strategy; 17 18import org.objenesis.instantiator.ObjectInstantiator; 19import org.objenesis.instantiator.android.Android10Instantiator; 20import org.objenesis.instantiator.android.Android17Instantiator; 21import org.objenesis.instantiator.android.Android18Instantiator; 22import org.objenesis.instantiator.basic.AccessibleInstantiator; 23import org.objenesis.instantiator.basic.ObjectInputStreamInstantiator; 24import org.objenesis.instantiator.gcj.GCJInstantiator; 25import org.objenesis.instantiator.perc.PercInstantiator; 26import org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator; 27import org.objenesis.instantiator.sun.UnsafeFactoryInstantiator; 28 29import java.io.Serializable; 30 31import static org.objenesis.strategy.PlatformDescription.*; 32 33/** 34 * Guess the best instantiator for a given class. The instantiator will instantiate the class 35 * without calling any constructor. Currently, the selection doesn't depend on the class. It relies 36 * on the 37 * <ul> 38 * <li>JVM version</li> 39 * <li>JVM vendor</li> 40 * <li>JVM vendor version</li> 41 * </ul> 42 * However, instantiators are stateful and so dedicated to their class. 43 * 44 * @author Henri Tremblay 45 * @see ObjectInstantiator 46 */ 47public class StdInstantiatorStrategy extends BaseInstantiatorStrategy { 48 49 /** 50 * Return an {@link ObjectInstantiator} allowing to create instance without any constructor being 51 * called. 52 * 53 * @param type Class to instantiate 54 * @return The ObjectInstantiator for the class 55 */ 56 public <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> type) { 57 58 if(PlatformDescription.isThisJVM(HOTSPOT) || PlatformDescription.isThisJVM(OPENJDK)) { 59 if(PlatformDescription.isGoogleAppEngine()) { 60 if(Serializable.class.isAssignableFrom(type)) { 61 return new ObjectInputStreamInstantiator<T>(type); 62 } 63 return new AccessibleInstantiator<T>(type); 64 } 65 // The UnsafeFactoryInstantiator would also work. But according to benchmarks, it is 2.5 66 // times slower. So I prefer to use this one 67 return new SunReflectionFactoryInstantiator<T>(type); 68 } 69 else if(PlatformDescription.isThisJVM(DALVIK)) { 70 if(PlatformDescription.isAndroidOpenJDK()) { 71 // Starting at Android N which is based on OpenJDK 72 return new UnsafeFactoryInstantiator<T>(type); 73 } 74 if(ANDROID_VERSION <= 10) { 75 // Android 2.3 Gingerbread and lower 76 return new Android10Instantiator<T>(type); 77 } 78 if(ANDROID_VERSION <= 17) { 79 // Android 3.0 Honeycomb to 4.2 Jelly Bean 80 return new Android17Instantiator<T>(type); 81 } 82 // Android 4.3 until Android N 83 return new Android18Instantiator<T>(type); 84 } 85 else if(PlatformDescription.isThisJVM(JROCKIT)) { 86 // JRockit is compliant with HotSpot 87 return new SunReflectionFactoryInstantiator<T>(type); 88 } 89 else if(PlatformDescription.isThisJVM(GNU)) { 90 return new GCJInstantiator<T>(type); 91 } 92 else if(PlatformDescription.isThisJVM(PERC)) { 93 return new PercInstantiator<T>(type); 94 } 95 96 // Fallback instantiator, should work with most modern JVM 97 return new UnsafeFactoryInstantiator<T>(type); 98 99 } 100} 101