Пример #1
0
 /**
  * Retrieve the setter for the specified class/field if it exists.
  *
  * @param clazz
  * @param f
  * @return
  */
 public static Method getSetter(Class<?> clazz, Field f) {
   Method setter = null;
   for (Method m : clazz.getMethods()) {
     if (ReflectionUtils.isSetter(m)
         && m.getName().equals(SETTER_PREFIX + StringUtils.capitalize(f.getName()))) {
       setter = m;
       break;
     }
   }
   return setter;
 }
Пример #2
0
 /**
  * Replaces field tokens with the actual value in the fields. The field types must be simple
  * property types
  *
  * @param str
  * @param fields info
  * @param parentMode
  * @param obj
  * @return
  * @throws Siren4JException
  */
 public static String replaceFieldTokens(
     Object obj, String str, List<ReflectedInfo> fields, boolean parentMode)
     throws Siren4JException {
   Map<String, Field> index = new HashMap<String, Field>();
   if (StringUtils.isBlank(str)) {
     return str;
   }
   if (fields != null) {
     for (ReflectedInfo info : fields) {
       Field f = info.getField();
       if (f != null) {
         index.put(f.getName(), f);
       }
     }
   }
   try {
     for (String key : ReflectionUtils.getTokenKeys(str)) {
       if ((!parentMode && !key.startsWith("parent."))
           || (parentMode && key.startsWith("parent."))) {
         String fieldname = key.startsWith("parent.") ? key.substring(7) : key;
         if (index.containsKey(fieldname)) {
           Field f = index.get(fieldname);
           if (f.getType().isEnum() || ArrayUtils.contains(propertyTypes, f.getType())) {
             String replacement = "";
             Object theObject = f.get(obj);
             if (f.getType().isEnum()) {
               replacement = theObject == null ? "" : ((Enum) theObject).name();
             } else {
               replacement = theObject == null ? "" : theObject.toString();
             }
             str = str.replaceAll("\\{" + key + "\\}", Matcher.quoteReplacement("" + replacement));
           }
         }
       }
     }
   } catch (Exception e) {
     throw new Siren4JException(e);
   }
   return str;
 }