protected T newObject () { try { return (T)constructor.newInstance((Object[])null); } catch (Exception ex) { throw new GdxRuntimeException("Unable to create new instance: " + constructor.getDeclaringClass().getName(), ex); } } }
/** Returns an array of {@link Constructor} containing the public constructors of the class represented by the supplied Class. */ static public Constructor[] getConstructors (Class c) { java.lang.reflect.Constructor[] constructors = c.getConstructors(); Constructor[] result = new Constructor[constructors.length]; for (int i = 0, j = constructors.length; i < j; i++) { result[i] = new Constructor(constructors[i]); } return result; }
protected Object newInstance (Class type) { try { return ClassReflection.newInstance(type); } catch (Exception ex) { try { // Try a private constructor. Constructor constructor = ClassReflection.getDeclaredConstructor(type); constructor.setAccessible(true); return constructor.newInstance(); } catch (SecurityException ignored) { } catch (ReflectionException ignored) { if (ClassReflection.isAssignableFrom(Enum.class, type)) { if (type.getEnumConstants() == null) type = type.getSuperclass(); return type.getEnumConstants()[0]; } if (type.isArray()) throw new SerializationException("Encountered JSON object when expected array of type: " + type.getName(), ex); else if (ClassReflection.isMemberClass(type) && !ClassReflection.isStaticClass(type)) throw new SerializationException("Class cannot be created (non-static member class): " + type.getName(), ex); else throw new SerializationException("Class cannot be created (missing no-arg constructor): " + type.getName(), ex); } catch (Exception privateConstructorException) { ex = privateConstructorException; } throw new SerializationException("Error constructing instance of class: " + type.getName(), ex); } }
private Object copy(Object object) { try { Constructor theConstructor = null; for (Constructor constructor : ClassReflection.getConstructors(object.getClass())) { if (constructor.getParameterTypes().length == 1 && ClassReflection.isAssignableFrom(constructor.getParameterTypes()[0], object.getClass())) { theConstructor = constructor; break; } } return theConstructor.newInstance(object); } catch (Throwable ex) { throw new GdxRuntimeException(ex); } }
Vector2 fromCopyConstructor = (Vector2)copyConstroctor.newInstance(fromDefaultConstructor); println("From copy constructor: " + fromCopyConstructor);
private Constructor findConstructor (Class<T> type) { try { return ClassReflection.getConstructor(type, (Class[])null); } catch (Exception ex1) { try { Constructor constructor = ClassReflection.getDeclaredConstructor(type, (Class[])null); constructor.setAccessible(true); return constructor; } catch (ReflectionException ex2) { return null; } } }
/** Uses the constructor to create and initialize a new instance of the constructor's declaring class, with the supplied * initialization parameters. */ public Object newInstance (Object... args) throws ReflectionException { try { return constructor.newInstance(args); } catch (IllegalArgumentException e) { throw new ReflectionException("Illegal argument(s) supplied to constructor for class: " + getDeclaringClass().getName(), e); } catch (InstantiationException e) { throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e); } catch (IllegalAccessException e) { throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e); } catch (InvocationTargetException e) { throw new ReflectionException("Exception occurred in constructor for class: " + getDeclaringClass().getName(), e); } }
@Override public <T> T newDialog(Class<T> cls) { String className = cls.getName(); if (registeredDialogs.containsKey(className)) { try { final Class<T> dialogClazz = ClassReflection.forName(registeredDialogs.get(className)); Object dialogObject = ClassReflection.getConstructor(dialogClazz, Activity.class).newInstance(activity); return dialogClazz.cast(dialogObject); } catch (Exception e) { e.printStackTrace(); } } throw new RuntimeException(cls.getName() + "is not registered."); }
private Constructor findConstructor (Class<T> type) { try { return ClassReflection.getConstructor(type, (Class[])null); } catch (Exception ex1) { try { Constructor constructor = ClassReflection.getDeclaredConstructor(type, (Class[])null); constructor.setAccessible(true); return constructor; } catch (ReflectionException ex2) { return null; } } }
/** Uses the constructor to create and initialize a new instance of the constructor's declaring class, with the supplied * initialization parameters. */ public Object newInstance (Object... args) throws ReflectionException { try { return constructor.newInstance(args); } catch (IllegalArgumentException e) { throw new ReflectionException("Illegal argument(s) supplied to constructor for class: " + getDeclaringClass().getName(), e); } catch (InstantiationException e) { throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e); } catch (IllegalAccessException e) { throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e); } catch (InvocationTargetException e) { throw new ReflectionException("Exception occurred in constructor for class: " + getDeclaringClass().getName(), e); } }
public <T> T newDialog(Class<T> cls) { String className = cls.getName(); if (registeredDialogs.containsKey(className)) { try { final Class<T> dialogClazz = ClassReflection.forName(registeredDialogs.get(className)); Object dialogObject = ClassReflection.getConstructor(dialogClazz).newInstance(); return (T) dialogObject; } catch (Exception e) { e.printStackTrace(); } } throw new RuntimeException(cls.getName() + "is not registered."); }
protected T newObject () { try { return (T)constructor.newInstance((Object[])null); } catch (Exception ex) { throw new GdxRuntimeException("Unable to create new instance: " + constructor.getDeclaringClass().getName(), ex); } } }
protected Object newInstance (Class type) { try { return ClassReflection.newInstance(type); } catch (Exception ex) { try { // Try a private constructor. Constructor constructor = ClassReflection.getDeclaredConstructor(type); constructor.setAccessible(true); return constructor.newInstance(); } catch (SecurityException ignored) { } catch (ReflectionException ignored) { if (ClassReflection.isAssignableFrom(Enum.class, type)) { if (type.getEnumConstants() == null) type = type.getSuperclass(); return type.getEnumConstants()[0]; } if (type.isArray()) throw new SerializationException("Encountered JSON object when expected array of type: " + type.getName(), ex); else if (ClassReflection.isMemberClass(type) && !ClassReflection.isStaticClass(type)) throw new SerializationException("Class cannot be created (non-static member class): " + type.getName(), ex); else throw new SerializationException("Class cannot be created (missing no-arg constructor): " + type.getName(), ex); } catch (Exception privateConstructorException) { ex = privateConstructorException; } throw new SerializationException("Error constructing instance of class: " + type.getName(), ex); } }
/** Returns an array of {@link Constructor} containing the public constructors of the class represented by the supplied Class. */ static public Constructor[] getConstructors (Class c) { java.lang.reflect.Constructor[] constructors = c.getConstructors(); Constructor[] result = new Constructor[constructors.length]; for (int i = 0, j = constructors.length; i < j; i++) { result[i] = new Constructor(constructors[i]); } return result; }
private Constructor findConstructor (Class<T> type) { try { return ClassReflection.getConstructor(type, (Class[])null); } catch (Exception ex1) { try { Constructor constructor = ClassReflection.getDeclaredConstructor(type, (Class[])null); constructor.setAccessible(true); return constructor; } catch (ReflectionException ex2) { return null; } } }
/** Uses the constructor to create and initialize a new instance of the constructor's declaring class, with the supplied * initialization parameters. */ public Object newInstance (Object... args) throws ReflectionException { try { return constructor.newInstance(args); } catch (IllegalArgumentException e) { throw new ReflectionException("Illegal argument(s) supplied to constructor for class: " + getDeclaringClass().getName(), e); } catch (InstantiationException e) { throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e); } catch (IllegalAccessException e) { throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e); } catch (InvocationTargetException e) { throw new ReflectionException("Exception occurred in constructor for class: " + getDeclaringClass().getName(), e); } }
public <T> T newDialog(Class<T> cls) { String className = cls.getName(); if (registeredDialogs.containsKey(className)) { try { final Class<T> dialogClazz = ClassReflection.forName(registeredDialogs.get(className)); Object dialogObject = ClassReflection.getConstructor(dialogClazz).newInstance(); return (T) dialogObject; } catch (Exception e) { e.printStackTrace(); } } throw new RuntimeException(cls.getName() + "is not registered."); }
protected T newObject () { try { return (T)constructor.newInstance((Object[])null); } catch (Exception ex) { throw new GdxRuntimeException("Unable to create new instance: " + constructor.getDeclaringClass().getName(), ex); } } }
protected Object newInstance (Class type) { try { return ClassReflection.newInstance(type); } catch (Exception ex) { try { // Try a private constructor. Constructor constructor = ClassReflection.getDeclaredConstructor(type); constructor.setAccessible(true); return constructor.newInstance(); } catch (SecurityException ignored) { } catch (ReflectionException ignored) { if (ClassReflection.isAssignableFrom(Enum.class, type)) { if (type.getEnumConstants() == null) type = type.getSuperclass(); return type.getEnumConstants()[0]; } if (type.isArray()) throw new SerializationException("Encountered JSON object when expected array of type: " + type.getName(), ex); else if (ClassReflection.isMemberClass(type) && !ClassReflection.isStaticClass(type)) throw new SerializationException("Class cannot be created (non-static member class): " + type.getName(), ex); else throw new SerializationException("Class cannot be created (missing no-arg constructor): " + type.getName(), ex); } catch (Exception privateConstructorException) { ex = privateConstructorException; } throw new SerializationException("Error constructing instance of class: " + type.getName(), ex); } }
/** Returns a {@link Constructor} that represents the public constructor for the supplied class which takes the supplied * parameter types. */ static public Constructor getConstructor (Class c, Class... parameterTypes) throws ReflectionException { try { return new Constructor(c.getConstructor(parameterTypes)); } catch (SecurityException e) { throw new ReflectionException("Security violation occurred while getting constructor for class: '" + c.getName() + "'.", e); } catch (NoSuchMethodException e) { throw new ReflectionException("Constructor not found for class: " + c.getName(), e); } }