예제 #1
0
파일: Json.java 프로젝트: antionio/libgdx
 private Object newInstance(Type type) {
   try {
     return type.newInstance();
   } catch (Exception ex) {
     try {
       // Try a private constructor.
       Constructor constructor = type.getDeclaredConstructor();
       constructor.setAccessible(true);
       return constructor.newInstance();
     } catch (SecurityException ignored) {
     } catch (NoSuchMethodException ignored) {
       if (type.isArray())
         throw new SerializationException(
             "Encountered JSON object when expected array of type: " + type.getName(), ex);
       else if (type.isMemberClass() && !type.isStatic())
         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);
   }
 }
예제 #2
0
 /**
  * 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();
   } catch (IllegalArgumentException e) {
     throw new ReflectionException(
         "Illegal argument(s) supplied to constructor for class: " + getDeclaringClass().getName(),
         e);
   }
 }
예제 #3
0
 public void setAccessible(boolean accessible) {
   constructor.setAccessible(accessible);
 }
예제 #4
0
 public boolean isAccessible() {
   return constructor.isAccessible();
 }
예제 #5
0
 /** Returns the Class object representing the class or interface that declares the constructor. */
 public Class getDeclaringClass() {
   return constructor.getEnclosingType().getClassOfType();
 }