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 */ 16 package org.objenesis; 17 18 import java.io.Serializable; 19 20 import org.objenesis.instantiator.ObjectInstantiator; 21 22 /** 23 * Use Objenesis in a static way. <strong>It is strongly not recommended to use this class.</strong> 24 * 25 * @author Henri Tremblay 26 */ 27 public final class ObjenesisHelper { 28 29 private static final Objenesis OBJENESIS_STD = new ObjenesisStd(); 30 31 private static final Objenesis OBJENESIS_SERIALIZER = new ObjenesisSerializer(); 32 ObjenesisHelper()33 private ObjenesisHelper() { 34 } 35 36 /** 37 * Will create a new object without any constructor being called 38 * 39 * @param <T> Type instantiated 40 * @param clazz Class to instantiate 41 * @return New instance of clazz 42 */ newInstance(Class<T> clazz)43 public static <T> T newInstance(Class<T> clazz) { 44 return OBJENESIS_STD.newInstance(clazz); 45 } 46 47 /** 48 * Will create an object just like it's done by ObjectInputStream.readObject (the default 49 * constructor of the first non serializable class will be called) 50 * 51 * @param <T> Type instantiated 52 * @param clazz Class to instantiate 53 * @return New instance of clazz 54 */ newSerializableInstance(Class<T> clazz)55 public static <T extends Serializable> T newSerializableInstance(Class<T> clazz) { 56 return (T) OBJENESIS_SERIALIZER.newInstance(clazz); 57 } 58 59 /** 60 * Will pick the best instantiator for the provided class. If you need to create a lot of 61 * instances from the same class, it is way more efficient to create them from the same 62 * ObjectInstantiator than calling {@link #newInstance(Class)}. 63 * 64 * @param <T> Type to instantiate 65 * @param clazz Class to instantiate 66 * @return Instantiator dedicated to the class 67 */ getInstantiatorOf(Class<T> clazz)68 public static <T> ObjectInstantiator<T> getInstantiatorOf(Class<T> clazz) { 69 return OBJENESIS_STD.getInstantiatorOf(clazz); 70 } 71 72 /** 73 * Same as {@link #getInstantiatorOf(Class)} but providing an instantiator emulating 74 * ObjectInputStream.readObject behavior. 75 * 76 * @see #newSerializableInstance(Class) 77 * @param <T> Type to instantiate 78 * @param clazz Class to instantiate 79 * @return Instantiator dedicated to the class 80 */ getSerializableObjectInstantiatorOf(Class<T> clazz)81 public static <T extends Serializable> ObjectInstantiator<T> getSerializableObjectInstantiatorOf(Class<T> clazz) { 82 return OBJENESIS_SERIALIZER.getInstantiatorOf(clazz); 83 } 84 } 85