Esempio n. 1
0
 public static synchronized void inject(Object obj, String fieldName, Object value) {
   try {
     Method method = ClassKit.getSetterMethod(obj.getClass(), fieldName);
     if (method == null) {
       logger.error(
           "No such method: "
               + ClassKit.generateSetterMethodName(fieldName)
               + ", args: [fieldName="
               + fieldName
               + ", value="
               + value
               + "]");
     }
     method.invoke(obj, value);
   } catch (SecurityException e) {
     logger.error(
         "Catch SecurityException when inject field, args: [fieldName="
             + fieldName
             + ", value="
             + value
             + "]",
         e);
   } catch (IllegalArgumentException e) {
     try {
       logger.error(
           "Catch IllegalArgumentException when inject field, args: [fieldName="
               + fieldName
               + ", value="
               + value
               + "], field's type: "
               + obj.getClass().getDeclaredField(fieldName).getType().getSimpleName()
               + ", value's type: "
               + value.getClass().getSimpleName(),
           e);
     } catch (NoSuchFieldException e1) {
     } catch (SecurityException e1) {
     }
   } catch (IllegalAccessException e) {
     logger.error(
         "Catch IllegalAccessException when inject field, args: [fieldName="
             + fieldName
             + ", value="
             + value
             + "]",
         e);
   } catch (InvocationTargetException e) {
     logger.error(
         "Catch InvocationTargetException when inject field, args: [fieldName="
             + fieldName
             + ", value="
             + value
             + "]",
         e);
   }
 }
Esempio n. 2
0
 public static <T> boolean isValidFieldName(Class<T> clazz, String fieldName) {
   if (clazz == null) {
     throw new IllegalArgumentException("arguement class cannot be null");
   }
   if (fieldName == null || fieldName.length() == 0) {
     throw new IllegalArgumentException("arguement fieldMap cannot be null or empty");
   }
   List<Field> fields = ClassKit.getAllFields(clazz);
   for (Field field : fields) {
     if (field.getName().equals(fieldName)) {
       return true;
     }
   }
   return false;
 }