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 org.objenesis.instantiator.ObjectInstantiator; 19 import org.objenesis.instantiator.android.AndroidInstantiator; 20 import org.objenesis.instantiator.gcj.GCJInstantiator; 21 import org.objenesis.instantiator.jrockit.JRockit131Instantiator; 22 import org.objenesis.instantiator.jrockit.JRockitLegacyInstantiator; 23 import org.objenesis.instantiator.perc.PercInstantiator; 24 import org.objenesis.instantiator.sun.Sun13Instantiator; 25 import org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator; 26 27 /** 28 * Guess the best instantiator for a given class. The instantiator will instantiate the class 29 * without calling any constructor. Currently, the selection doesn't depend on the class. It relies 30 * on the 31 * <ul> 32 * <li>JVM version</li> 33 * <li>JVM vendor</li> 34 * <li>JVM vendor version</li> 35 * </ul> 36 * However, instantiators are stateful and so dedicated to their class. 37 * 38 * @author Henri Tremblay 39 * @see ObjectInstantiator 40 */ 41 public class StdInstantiatorStrategy extends BaseInstantiatorStrategy { 42 43 /** 44 * Return an {@link ObjectInstantiator} allowing to create instance without any constructor being 45 * called. 46 * 47 * @param type Class to instantiate 48 * @return The ObjectInstantiator for the class 49 */ newInstantiatorOf(Class type)50 public ObjectInstantiator newInstantiatorOf(Class type) { 51 52 if(JVM_NAME.startsWith(SUN)) { 53 if(VM_VERSION.startsWith("1.3")) { 54 return new Sun13Instantiator(type); 55 } 56 } 57 else if(JVM_NAME.startsWith(JROCKIT)) { 58 if(VM_VERSION.startsWith("1.3")) { 59 return new JRockit131Instantiator(type); 60 } 61 else if(VM_VERSION.startsWith("1.4")) { 62 // JRockit vendor version will be RXX where XX is the version 63 // Versions prior to 26 need special handling 64 // From R26 on, java.vm.version starts with R 65 if(!VENDOR_VERSION.startsWith("R")) { 66 // On R25.1 and R25.2, ReflectionFactory should work. Otherwise, we must use the 67 // Legacy instantiator. 68 if(VM_INFO == null || !VM_INFO.startsWith("R25.1") || !VM_INFO.startsWith("R25.2")) { 69 return new JRockitLegacyInstantiator(type); 70 } 71 } 72 } 73 } 74 else if(JVM_NAME.startsWith(DALVIK)) { 75 return new AndroidInstantiator(type); 76 } 77 else if(JVM_NAME.startsWith(GNU)) { 78 return new GCJInstantiator(type); 79 } 80 else if(JVM_NAME.startsWith(PERC)) { 81 return new PercInstantiator(type); 82 } 83 84 // Fallback instantiator, should work with: 85 // - Java Hotspot version 1.4 and higher 86 // - JRockit 1.4-R26 and higher 87 // - IBM and Hitachi JVMs 88 // ... might works for others so we just give it a try 89 return new SunReflectionFactoryInstantiator(type); 90 } 91 } 92