当前位置: 首页>>代码示例>>Java>>正文


Java Objenesis.getInstantiatorOf方法代码示例

本文整理汇总了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;
}
 
开发者ID:awenblue,项目名称:powermock,代码行数:39,代码来源:WhiteboxImpl.java

示例2: FSTObjenesisInstantiator

import org.objenesis.Objenesis; //导入方法依赖的package包/类
public FSTObjenesisInstantiator( Objenesis objenesis, Class clazz ) {
    objInstantiator = objenesis.getInstantiatorOf( clazz );
}
 
开发者ID:RuedigerMoeller,项目名称:fast-serialization,代码行数:4,代码来源:FSTObjenesisInstantiator.java


注:本文中的org.objenesis.Objenesis.getInstantiatorOf方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。