本文整理汇总了Java中org.objenesis.instantiator.ObjectInstantiator类的典型用法代码示例。如果您正苦于以下问题:Java ObjectInstantiator类的具体用法?Java ObjectInstantiator怎么用?Java ObjectInstantiator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ObjectInstantiator类属于org.objenesis.instantiator包,在下文中一共展示了ObjectInstantiator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newInstance
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/**
* Create a new instance of a class without invoking its constructor.
* <p>
* No byte-code manipulation is needed to perform this operation and thus
* it's not necessary use the <code>PowerMockRunner</code> or
* <code>PrepareForTest</code> annotation to use this functionality.
*
* @param <T>
* The type of the instance to create.
* @param classToInstantiate
* The type of the instance to create.
* @return A new instance of type T, created without invoking the
* constructor.
*/
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> classToInstantiate) {
int modifiers = classToInstantiate.getModifiers();
final Object object;
if (Modifier.isInterface(modifiers)) {
object = Proxy.newProxyInstance(WhiteboxImpl.class.getClassLoader(), new Class<?>[] { classToInstantiate },
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return TypeUtils.getDefaultValue(method.getReturnType());
}
});
} else if (classToInstantiate.isArray()) {
object = Array.newInstance(classToInstantiate.getComponentType(), 0);
} else if (Modifier.isAbstract(modifiers)) {
throw new IllegalArgumentException(
"Cannot instantiate an abstract class. Please use the ConcreteClassGenerator in PowerMock support to generate a concrete class first.");
} else {
Objenesis objenesis = new ObjenesisStd();
ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(classToInstantiate);
object = thingyInstantiator.newInstance();
}
return (T) object;
}
示例2: createInstance
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/**
* Takes a class type and constructs it. If the class does not have an empty constructor this
* will
* find the parametrized constructor and use nulls and default values to construct the class.
*
* @param clazz The class to construct.
* @param <T> The type of the class to construct.
* @return The constructed object.
*/
@SuppressWarnings("unchecked")
public static <T> T createInstance(Class<T> clazz) {
if (clazz.isInterface()) {
if (clazz == List.class) {
return (T) new ArrayList();
} else if (clazz == Map.class) {
return (T) new HashMap();
}
throw new UnsupportedOperationException("Interface types can not be instantiated.");
}
ObjectInstantiator instantiator = OBJENESIS.getInstantiatorOf(clazz);
return (T) instantiator.newInstance();
}
示例3: getInstantiatorOf
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/**
* Will pick the best instantiator for the provided class. If you need to create a lot of
* instances from the same class, it is way more efficient to create them from the same
* ObjectInstantiator than calling {@link #newInstance(Class)}.
*
* @param clazz Class to instantiate
* @return Instantiator dedicated to the class
*/
@SuppressWarnings("unchecked")
public <T> ObjectInstantiator<T> getInstantiatorOf(Class<T> clazz) {
if(clazz.isPrimitive()) {
throw new IllegalArgumentException("Primitive types can't be instantiated in Java");
}
if(cache == null) {
return strategy.newInstantiatorOf(clazz);
}
ObjectInstantiator<?> instantiator = cache.get(clazz.getName());
if(instantiator == null) {
ObjectInstantiator<?> newInstantiator = strategy.newInstantiatorOf(clazz);
instantiator = cache.putIfAbsent(clazz.getName(), newInstantiator);
if(instantiator == null) {
instantiator = newInstantiator;
}
}
return (ObjectInstantiator<T>) instantiator;
}
示例4: newInstance
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/** Creates a new instance of a class using {@link Registration#getInstantiator()}. If the registration's instantiator is null,
* a new one is set using {@link #newInstantiator(Class)}. */
public <T> T newInstance (Class<T> type) {
Registration registration = getRegistration(type);
ObjectInstantiator instantiator = registration.getInstantiator();
if (instantiator == null) {
instantiator = newInstantiator(type);
registration.setInstantiator(instantiator);
}
return (T)instantiator.newInstance();
}
示例5: newInstance
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/** Creates a new instance of a class using {@link Registration#getInstantiator()}. If the registration's instantiator is null,
* a new one is set using {@link #newInstantiator(Class)}. */
public <T> T newInstance (Class<T> type) {
Registration registration = getRegistration(type);
ObjectInstantiator instantiator = registration.getInstantiator();
if (instantiator == null) {
if(registration.getSerializer().isDefaultSerializer())
instantiator = defaultStrategy.newInstantiatorOf(type);
else
instantiator = newInstantiator(type);
registration.setInstantiator(instantiator);
}
return (T)instantiator.newInstance();
}
示例6: newInstantiatorOf
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ObjectInstantiator newInstantiatorOf(Class type) {
if (type.isInterface() && List.class.isAssignableFrom(type)) {
return new ListInstantiator();
}
return delegate.newInstantiatorOf(type);
}
示例7: newInstantiatorOf
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/**
* Return an {@link ObjectInstantiator} allowing to create instance without any constructor
* being called.
* @param type Class to instantiate
* @return The ObjectInstantiator for the class
*/
public <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> type) {
if (FeatureDetection.hasUnsafe()) {
return new UnsafeFactoryInstantiator<T>(type);
}
return super.newInstantiatorOf(type);
}
示例8: newInstantiator
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
@Override protected ObjectInstantiator newInstantiator(final Class type) {
if (Node.class.isAssignableFrom(type)) {
return s.newInstantiatorOf(type);
}
else {
return super.newInstantiator(type);
}
}
示例9: ObjenesisBase
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/**
* Flexible constructor allowing to pick the strategy and if caching should be used
*
* @param strategy Strategy to use
* @param useCache If {@link ObjectInstantiator}s should be cached
*/
public ObjenesisBase(InstantiatorStrategy strategy, boolean useCache) {
if(strategy == null) {
throw new IllegalArgumentException("A strategy can't be null");
}
this.strategy = strategy;
this.cache = useCache ? new ConcurrentHashMap<String, ObjectInstantiator<?>>() : null;
}
示例10: SingleInstantiatorStrategy
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/**
* Create a strategy that will return always the same instantiator type. We assume this instantiator
* has one constructor taking the class to instantiate in parameter.
*
* @param <T> the type we want to instantiate
* @param instantiator the instantiator type
*/
public <T extends ObjectInstantiator<?>> SingleInstantiatorStrategy(Class<T> instantiator) {
try {
constructor = instantiator.getConstructor(Class.class);
}
catch(NoSuchMethodException e) {
throw new ObjenesisException(e);
}
}
示例11: newInstantiatorOf
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/**
* Return an {@link ObjectInstantiator} allowing to create instance following the java
* serialization framework specifications.
*
* @param type Class to instantiate
* @return The ObjectInstantiator for the class
*/
public <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> type) {
if(!Serializable.class.isAssignableFrom(type)) {
throw new ObjenesisException(new NotSerializableException(type+" not serializable"));
}
if(JVM_NAME.startsWith(HOTSPOT) || PlatformDescription.isThisJVM(OPENJDK)) {
// Java 7 GAE was under a security manager so we use a degraded system
if(isGoogleAppEngine() && PlatformDescription.SPECIFICATION_VERSION.equals("1.7")) {
return new ObjectInputStreamInstantiator<T>(type);
}
return new SunReflectionFactorySerializationInstantiator<T>(type);
}
else if(JVM_NAME.startsWith(DALVIK)) {
if(PlatformDescription.isAndroidOpenJDK()) {
return new ObjectStreamClassInstantiator<T>(type);
}
return new AndroidSerializationInstantiator<T>(type);
}
else if(JVM_NAME.startsWith(GNU)) {
return new GCJSerializationInstantiator<T>(type);
}
else if(JVM_NAME.startsWith(PERC)) {
return new PercSerializationInstantiator<T>(type);
}
return new SunReflectionFactorySerializationInstantiator<T>(type);
}
示例12: testGetInstantiatorOf
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
@Test
public final void testGetInstantiatorOf() {
Objenesis o = new ObjenesisStd();
ObjectInstantiator<?> i1 = o.getInstantiatorOf(getClass());
// Test instance creation
assertEquals(getClass(), i1.newInstance().getClass());
// Test caching
ObjectInstantiator<?> i2 = o.getInstantiatorOf(getClass());
assertSame(i1, i2);
}
示例13: testNewInstance
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
@Test
public void testNewInstance() throws Exception {
ObjectInstantiator<EmptyClass> o1 = new MagicInstantiator<EmptyClass>(EmptyClass.class);
assertEquals(EmptyClass.class, o1.newInstance().getClass());
ObjectInstantiator<EmptyClass> o2 = new MagicInstantiator<EmptyClass>(EmptyClass.class);
assertEquals(EmptyClass.class, o2.newInstance().getClass());
}
示例14: newInstance
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/** Creates a new instance of a class using {@link Registration#getInstantiator()}. If the registration's instantiator is null,
* a new one is set using {@link #newInstantiator(Class)}. */
public <T> T newInstance (Class<T> type) {
Registration registration = getRegistration(type);
ObjectInstantiator instantiator = registration.getInstantiator();
if (instantiator == null) {
instantiator = newInstantiator(type);
registration.setInstantiator(instantiator);
}
return (T)instantiator.newInstance();
}
示例15: getInstantiator
import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/** @return May be null if not yet set. */
public ObjectInstantiator getInstantiator () {
return instantiator;
}