Package org.objenesis.instantiator

Examples of org.objenesis.instantiator.ObjectInstantiator


   * @param Class<?>
   * @return Object if one was instantiated, null otherwise
   */
  public static Object getInstanceOfClassForcibly(Class<?> clazz) {
    Objenesis objenesis = new ObjenesisStd();
    ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(clazz);
    return thingyInstantiator.newInstance();
//    try {
//      Constructor<?> c = clazz.getDeclaredConstructor();
//      c.setAccessible(true);
//      return c.newInstance();
//    } catch (IllegalArgumentException e) {
View Full Code Here


   * @param Class<?>
   * @return Object if one was instantiated, null otherwise
   */
  public static Object getInstanceOfClass(Class<?> clazz) {
    Objenesis objenesis = new ObjenesisStd();
    ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(clazz);
    return thingyInstantiator.newInstance();
//    try {
//      return clazz.newInstance();
//    } catch (InstantiationException e) {
//      // TODO Auto-generated catch block
//      e.printStackTrace();
View Full Code Here

    */
   public synchronized ObjectInstantiator getInstantiatorOf(Class clazz) {
      if(cache == null) {
         return strategy.newInstantiatorOf(clazz);
      }
      ObjectInstantiator instantiator = (ObjectInstantiator) cache.get(clazz.getName());
      if(instantiator == null) {
         instantiator = strategy.newInstantiatorOf(clazz);
         cache.put(clazz.getName(), instantiator);
      }
      return instantiator;
View Full Code Here

        return new Proxy().createInstance(aCLass, invocationHandler);
    }

    public <T> T createInstance(final Class<T> aClass, final Callback invocationHandler) {
        Callback[] callbacks = {invocationHandler, NoOp.INSTANCE};
        ObjectInstantiator instantiator = get(aClass, callbacks);
        Object instance = instantiator.newInstance();
        Factory factory = (Factory) instance;
        factory.setCallbacks(callbacks);
        return aClass.cast(instance);
    }
View Full Code Here

  protected ObjectInstantiator newInstantiator (final Class type) {
    if (!Util.isAndroid) {
      // ReflectASM.
      try {
        final ConstructorAccess access = ConstructorAccess.get(type);
        return new ObjectInstantiator() {
          public Object newInstance () {
            try {
              return access.newInstance();
            } catch (Exception ex) {
              throw new KryoException("Error constructing instance of class: " + className(type), ex);
            }
          }
        };
      } catch (Exception ignored) {
      }
    }
    // Reflection.
    try {
      Constructor ctor;
      try {
        ctor = type.getConstructor((Class[])null);
      } catch (Exception ex) {
        ctor = type.getDeclaredConstructor((Class[])null);
        ctor.setAccessible(true);
      }
      final Constructor constructor = ctor;
      return new ObjectInstantiator() {
        public Object newInstance () {
          try {
            return constructor.newInstance();
          } catch (Exception ex) {
            throw new KryoException("Error constructing instance of class: " + className(type), ex);
View Full Code Here

  /** 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();
  }
View Full Code Here

      boolean isNonStaticMemberClass = enclosingType != null && type.isMemberClass()
        && !Modifier.isStatic(type.getModifiers());
      if (!isNonStaticMemberClass) {
        try {
          final ConstructorAccess access = ConstructorAccess.get(type);
          return new ObjectInstantiator() {
            public Object newInstance () {
              try {
                return access.newInstance();
              } catch (Exception ex) {
                throw new KryoException("Error constructing instance of class: " + className(type), ex);
              }
            }
          };
        } catch (Exception ignored) {
        }
      }
    }
    // Reflection.
    try {
      Constructor ctor;
      try {
        ctor = type.getConstructor((Class[])null);
      } catch (Exception ex) {
        ctor = type.getDeclaredConstructor((Class[])null);
        ctor.setAccessible(true);
      }
      final Constructor constructor = ctor;
      return new ObjectInstantiator() {
        public Object newInstance () {
          try {
            return constructor.newInstance();
          } catch (Exception ex) {
            throw new KryoException("Error constructing instance of class: " + className(type), ex);
View Full Code Here

  /** 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();
  }
View Full Code Here

  /** 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();
  }
View Full Code Here

        boolean isNonStaticMemberClass = enclosingType != null && type.isMemberClass()
          && !Modifier.isStatic(type.getModifiers());
        if (!isNonStaticMemberClass) {
          try {
            final ConstructorAccess access = ConstructorAccess.get(type);
            return new ObjectInstantiator() {
              public Object newInstance () {
                try {
                  return access.newInstance();
                } catch (Exception ex) {
                  throw new KryoException("Error constructing instance of class: " + className(type), ex);
                }
              }
            };
          } catch (Exception ignored) {
          }
        }
      }
      // Reflection.
      try {
        Constructor ctor;
        try {
          ctor = type.getConstructor((Class[])null);
        } catch (Exception ex) {
          ctor = type.getDeclaredConstructor((Class[])null);
          ctor.setAccessible(true);
        }
        final Constructor constructor = ctor;
        return new ObjectInstantiator() {
          public Object newInstance () {
            try {
              return constructor.newInstance();
            } catch (Exception ex) {
              throw new KryoException("Error constructing instance of class: " + className(type), ex);
View Full Code Here

TOP

Related Classes of org.objenesis.instantiator.ObjectInstantiator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.