Java Code Examples for org.objenesis.instantiator.ObjectInstantiator#newInstance()

The following examples show how to use org.objenesis.instantiator.ObjectInstantiator#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CglibProxy.java    From festival with Apache License 2.0 6 votes vote down vote up
@Override
public Object getProxy(ClassLoader classLoader) {
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setCallbackType(MethodInterceptor.class);

    Class<?> targetClass = support.getBeanClass();
    enhancer.setSuperclass(targetClass);
    enhancer.setInterfaces(new Class[]{FestivalProxy.class, TargetClassAware.class});
    Class<?> proxyClass = enhancer.createClass();

    Objenesis objenesis = new ObjenesisStd();
    ObjectInstantiator<?> instantiator = objenesis.getInstantiatorOf(proxyClass);
    Object proxyInstance = instantiator.newInstance();

    ((Factory) proxyInstance).setCallbacks(new Callback[]{new CglibMethodInterceptor(support)});

    return proxyInstance;
}
 
Example 2
Source File: ShillelaghUtil.java    From shillelagh with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source File: PreAnalyzeFieldsTest.java    From jesterj with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void throwAway() {
  log.info("BEFORE_CLASS BEGINS");
  // this is just ot keep checkClusterConfiguration happy... actually better to do this per-test
  // to avoid cross talk between tests (see SOLR-12801 test revamp in solr)
  // future versions of solr won't require this....
  Objenesis objenesis = new ObjenesisStd();
  ObjectInstantiator<MiniSolrCloudCluster> instantiatorOf = objenesis.getInstantiatorOf(MiniSolrCloudCluster.class);
  cluster = instantiatorOf.newInstance();
  log.info("BEFORE_CLASS ENDSS");
}
 
Example 4
Source File: Deserializer.java    From buck with Apache License 2.0 5 votes vote down vote up
public <T extends AddsToRuleKey> T create(Class<T> requestedClass) throws IOException {
  String className = stream.readUTF();
  Class<?> instanceClass;
  try {
    instanceClass = classFinder.find(className);
  } catch (ClassNotFoundException e) {
    throw new RuntimeException(e);
  }
  Preconditions.checkState(requestedClass.isAssignableFrom(instanceClass));

  Optional<CustomClassBehaviorTag> serializerTag =
      CustomBehaviorUtils.getBehavior(instanceClass, CustomClassSerialization.class);
  if (serializerTag.isPresent()) {
    @SuppressWarnings("unchecked")
    CustomClassSerialization<T> customSerializer =
        (CustomClassSerialization<T>) serializerTag.get();
    return customSerializer.deserialize(this);
  }

  ObjectInstantiator instantiator =
      instantiators.computeIfAbsent(instanceClass, objenesis::getInstantiatorOf);
  @SuppressWarnings("unchecked")
  T instance = (T) instantiator.newInstance();
  ClassInfo<? super T> classInfo = DefaultClassInfoFactory.forInstance(instance);

  initialize(instance, classInfo);

  return instance;
}
 
Example 5
Source File: ProxyingInstantiatorTest.java    From objenesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testNewInstance() {
   ObjectInstantiator<EmptyClass> inst = new ProxyingInstantiator<>(EmptyClass.class);
   EmptyClass c = inst.newInstance();
   assertEquals("org.objenesis.EmptyClass$$$Objenesis", c.getClass().getName());
}
 
Example 6
Source File: ProxyingInstantiatorTest.java    From objenesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testJavaLangInstance() {
   ObjectInstantiator<Object> inst = new ProxyingInstantiator<>(Object.class);
   Object c = inst.newInstance();
   assertEquals("org.objenesis.subclasses.java.lang.Object$$$Objenesis", c.getClass().getName());
}