コード例 #1
0
 /**
  * Method called to create a "default instance" of the bean, currently only needed for obtaining
  * default field values which may be used for suppressing serialization of fields that have "not
  * changed".
  *
  * @param fixAccess If true, method is allowed to fix access to the default constructor (to be
  *     able to call non-public constructor); if false, has to use constructor as is.
  * @return Instance of class represented by this descriptor, if suitable default constructor was
  *     found; null otherwise.
  */
 public Object instantiateBean(boolean fixAccess) {
   AnnotatedConstructor ac = _classInfo.getDefaultConstructor();
   if (ac == null) {
     return null;
   }
   if (fixAccess) {
     ac.fixAccess();
   }
   try {
     return ac.getAnnotated().newInstance();
   } catch (Exception e) {
     Throwable t = e;
     while (t.getCause() != null) {
       t = t.getCause();
     }
     if (t instanceof Error) throw (Error) t;
     if (t instanceof RuntimeException) throw (RuntimeException) t;
     throw new IllegalArgumentException(
         "Failed to instantiate bean of type "
             + _classInfo.getAnnotated().getName()
             + ": ("
             + t.getClass().getName()
             + ") "
             + t.getMessage(),
         t);
   }
 }
コード例 #2
0
 /**
  * Method that can be called to locate a single-arg constructor that takes specified exact type
  * (will not accept supertype constructors)
  *
  * @param argTypes Type(s) of the argument that we are looking for
  */
 public Constructor<?> findSingleArgConstructor(Class<?>... argTypes) {
   for (AnnotatedConstructor ac : _classInfo.getConstructors()) {
     // This list is already filtered to only include accessible
     /* (note: for now this is a redundant check; but in future
      * that may change; thus leaving here for now)
      */
     if (ac.getParameterCount() == 1) {
       Class<?> actArg = ac.getParameterClass(0);
       for (Class<?> expArg : argTypes) {
         if (expArg == actArg) {
           return ac.getAnnotated();
         }
       }
     }
   }
   return null;
 }