本文整理汇总了Java中org.objenesis.Objenesis.getInstantiatorOf方法的典型用法代码示例。如果您正苦于以下问题:Java Objenesis.getInstantiatorOf方法的具体用法?Java Objenesis.getInstantiatorOf怎么用?Java Objenesis.getInstantiatorOf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.objenesis.Objenesis
的用法示例。
在下文中一共展示了Objenesis.getInstantiatorOf方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newInstance
import org.objenesis.Objenesis; //导入方法依赖的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: FSTObjenesisInstantiator
import org.objenesis.Objenesis; //导入方法依赖的package包/类
public FSTObjenesisInstantiator( Objenesis objenesis, Class clazz ) {
objInstantiator = objenesis.getInstantiatorOf( clazz );
}