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


Java ObjectInstantiator类代码示例

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

示例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();
}
 
开发者ID:AndrewReitz,项目名称:shillelagh,代码行数:25,代码来源:ShillelaghUtil.java

示例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;
}
 
开发者ID:easymock,项目名称:objenesis,代码行数:27,代码来源:ObjenesisBase.java

示例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();
}
 
开发者ID:HoratiusTang,项目名称:EsperDist,代码行数:12,代码来源:Kryo.java

示例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();
}
 
开发者ID:HoratiusTang,项目名称:EsperDist,代码行数:15,代码来源:Kryo.java

示例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);
}
 
开发者ID:xored,项目名称:vertx-typed-rpc,代码行数:9,代码来源:ListInstantiatorStrategy.java

示例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);
}
 
开发者ID:JadiraOrg,项目名称:jadira,代码行数:14,代码来源:UnsafeInstantiatorStrategy.java

示例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);
    }
}
 
开发者ID:etamponi,项目名称:object-graph,代码行数:9,代码来源:Node.java

示例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;
}
 
开发者ID:easymock,项目名称:objenesis,代码行数:14,代码来源:ObjenesisBase.java

示例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);
   }
}
 
开发者ID:easymock,项目名称:objenesis,代码行数:16,代码来源:SingleInstantiatorStrategy.java

示例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);
}
 
开发者ID:easymock,项目名称:objenesis,代码行数:34,代码来源:SerializingInstantiatorStrategy.java

示例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);
}
 
开发者ID:easymock,项目名称:objenesis,代码行数:12,代码来源:ObjenesisTest.java

示例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());
}
 
开发者ID:easymock,项目名称:objenesis,代码行数:9,代码来源:MagicInstantiatorTest.java

示例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();
}
 
开发者ID:esialb,项目名称:kryo-mavenized,代码行数:12,代码来源:Kryo.java

示例15: getInstantiator

import org.objenesis.instantiator.ObjectInstantiator; //导入依赖的package包/类
/** @return May be null if not yet set. */
public ObjectInstantiator getInstantiator () {
	return instantiator;
}
 
开发者ID:HoratiusTang,项目名称:EsperDist,代码行数:5,代码来源:Registration.java


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