示例#1
0
 @SuppressWarnings({"rawtypes", "unchecked"})
 public static <X> X buildMockObject(Class<X> clazz) {
   X x = null;
   try {
     x = clazz.newInstance();
     for (Method method : clazz.getDeclaredMethods()) {
       String mn = method.getName();
       if (mn.startsWith("set")) {
         Class[] parameters = method.getParameterTypes();
         if (parameters.length == 1) {
           Method getMethod =
               MethodUtils.getAccessibleMethod(clazz, "get" + mn.substring(3), null);
           if (getMethod != null) {
             if (getMethod.getName().equals("getId")) {
               continue;
             }
             Object value = null;
             Class parameter = parameters[0];
             if (parameter.isAssignableFrom(String.class)) {
               Column column = getMethod.getAnnotation(Column.class);
               int columnLength = 32;
               if (column != null && column.length() < columnLength) {
                 columnLength = column.length();
               }
               Size size = getMethod.getAnnotation(Size.class);
               if (size != null && size.min() < columnLength) {
                 columnLength = size.min();
               }
               value = RandomStringUtils.randomAlphabetic(columnLength);
             } else if (parameter.isAssignableFrom(Date.class)) {
               value = new Date();
             } else if (parameter.isAssignableFrom(BigDecimal.class)) {
               value = new BigDecimal(new Random().nextDouble());
             } else if (parameter.isAssignableFrom(Integer.class)) {
               value = new Random().nextInt();
             } else if (parameter.isAssignableFrom(Boolean.class)) {
               value = new Random().nextBoolean();
             } else if (parameter.isEnum()) {
               Method m = parameter.getDeclaredMethod("values", null);
               Object[] result = (Object[]) m.invoke(parameter.getEnumConstants()[0], null);
               value = result[new Random().nextInt(result.length)];
             }
             if (value != null) {
               MethodUtils.invokeMethod(x, mn, value);
               logger.debug("{}={}", method.getName(), value);
             }
           }
         }
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return x;
 }
示例#2
0
 /** {@inheritDoc} */
 public void apply(HSSFCell cell, HSSFCellStyle cellStyle, Map<String, String> style) {
   for (String pos : new String[] {TOP, RIGHT, BOTTOM, LEFT}) {
     String posName = StringUtils.capitalize(pos.toLowerCase());
     // color
     String colorAttr = BORDER + "-" + pos + "-" + COLOR;
     HSSFColor poiColor = CssUtils.parseColor(cell.getSheet().getWorkbook(), style.get(colorAttr));
     if (poiColor != null) {
       try {
         MethodUtils.invokeMethod(cellStyle, "set" + posName + "BorderColor", poiColor.getIndex());
       } catch (Exception e) {
         log.error("Set Border Color Error Caused.", e);
       }
     }
     // width
     int width = CssUtils.getInt(style.get(BORDER + "-" + pos + "-" + WIDTH));
     String styleAttr = BORDER + "-" + pos + "-" + STYLE;
     String styleValue = style.get(styleAttr);
     short shortValue = -1;
     // empty or solid
     if (StringUtils.isBlank(styleValue) || "solid".equals(styleValue)) {
       if (width > 2) {
         shortValue = CellStyle.BORDER_THICK;
       } else if (width > 1) {
         shortValue = CellStyle.BORDER_MEDIUM;
       } else {
         shortValue = CellStyle.BORDER_THIN;
       }
     } else if (ArrayUtils.contains(new String[] {NONE, HIDDEN}, styleValue)) {
       shortValue = CellStyle.BORDER_NONE;
     } else if (DOUBLE.equals(styleValue)) {
       shortValue = CellStyle.BORDER_DOUBLE;
     } else if (DOTTED.equals(styleValue)) {
       shortValue = CellStyle.BORDER_DOTTED;
     } else if (DASHED.equals(styleValue)) {
       if (width > 1) {
         shortValue = CellStyle.BORDER_MEDIUM_DASHED;
       } else {
         shortValue = CellStyle.BORDER_DASHED;
       }
     }
     // border style
     if (shortValue != -1) {
       try {
         MethodUtils.invokeMethod(cellStyle, "setBorder" + posName, shortValue);
       } catch (Exception e) {
         log.error("Set Border Style Error Caused.", e);
       }
     }
   }
 }
 private static void overridePendingTransition(
     final Activity activity, int enterAnim, int exitAnim) {
   try {
     MethodUtils.invokeMethod(activity, "overridePendingTransition", enterAnim, exitAnim);
   } catch (Exception e) {
     Log.e(Settings.tag, "cannot call overridePendingTransition", e);
   }
 }
  /**
   * @param method the method
   * @param searchTopMethod search for top most overridden method
   * @return the corresponding element name
   */
  public static String getElementName(Method method, boolean searchTopMethod) {
    Method topMethod = method;

    if (searchTopMethod) {
      // Get top most method declaration
      Set<Method> hierarchy = MethodUtils.getOverrideHierarchy(method, Interfaces.INCLUDE);
      topMethod = IterableUtils.get(hierarchy, hierarchy.size() - 1);
    }

    // Get element name from method
    return getElementName(topMethod);
  }
示例#5
0
  /**
   * 获取对象的属性值
   *
   * @param data 对象
   * @param filed 属性名
   * @return 如果属性是枚举, 则调用枚举的 getValue 方法, 如果是日期, 则格式化, 否则返回此属性值, 异常则返回 空字符串
   */
  public static String handleGetFiled(Object data, String filed) {
    if (data == null || StringUtils.isBlank(filed)) return StringUtils.EMPTY;

    try {
      filed = filed.trim();
      String method = "get" + filed.substring(0, 1).toUpperCase() + filed.substring(1);
      Object value = MethodUtils.invokeMethod(data, method);

      if (value == null) return StringUtils.EMPTY;
      // 如果是枚举, 则调用其 getValue 方法
      if (value.getClass().isEnum())
        return handleValue(MethodUtils.invokeMethod(value, "getValue"));
      // 如果是日期类型, 则格式化
      if (value instanceof Date)
        return handleTrim(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) value));

      return handleValue(value);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
      return StringUtils.EMPTY;
    }
  }
示例#6
0
  @Override
  public boolean update(T t) {
    StringBuilder sql = new StringBuilder("update ").append(getTable()).append(" set ");

    List<Object> params = new LinkedList<>();

    try {
      BeanInfo beanInfo = Introspector.getBeanInfo(clazz, Object.class);
      PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
      for (PropertyDescriptor pd : propertyDescriptors) {
        String propertyName = pd.getName();
        if ("id".equals(propertyName)) {
          continue;
        }
        Method getterMethod = pd.getReadMethod();
        Object value = getterMethod.invoke(t);

        Class<?> propertyType = pd.getPropertyType();
        if (propertyType.isAnnotationPresent(Entry.class)) {
          propertyName = propertyName + "_id";
          value = MethodUtils.invokeMethod(value, "getId");
        }
        sql.append(propertyName).append("=?,");

        params.add(value);
      }
      sql.deleteCharAt(sql.length() - 1).append(" where id = ?");
      params.add(MethodUtils.invokeMethod(t, "getId"));

    } catch (IntrospectionException
        | IllegalAccessException
        | IllegalArgumentException
        | InvocationTargetException
        | NoSuchMethodException e) {
      e.printStackTrace();
    }

    return update(sql.toString(), params.toArray());
  }
示例#7
0
  public static EnumCode valueOf(Class<?> cls, String code) {
    if (StringUtils.isEmpty(code)) {
      return null;
    }

    try {
      String codeKey = getCodeKey(cls, code);
      if (enums.containsKey(codeKey)) {
        return enums.get(codeKey);
      }
      EnumCode enumCode = (EnumCode) MethodUtils.invokeExactStaticMethod(cls, "valueOf", code);
      return enums.put(codeKey, enumCode);
    } catch (Exception e) {
      return null;
    }
  }
  /**
   * @param type the class of the filter
   * @return the descriptor of the filter
   * @throws IncompatibleFilterException when several methods/events are incompatibles
   */
  private FilterDescriptor createDescriptor(Class<?> type) throws IncompatibleFilterException {
    // Proxy "loose" various reflection informations (like method parameter names)
    if (Proxy.isProxyClass(type)) {
      return getFilterDescriptor(type.getInterfaces());
    } else {
      FilterDescriptor descriptor = new FilterDescriptor();

      for (Method method : type.getMethods()) {
        // Get top most method declaration
        Set<Method> hierarchy = MethodUtils.getOverrideHierarchy(method, Interfaces.INCLUDE);
        Method topMethod = IterableUtils.get(hierarchy, hierarchy.size() - 1);

        // Get element name from method
        String elementName = getElementName(topMethod);

        // If a name can be found, continue
        if (elementName != null) {
          addElement(elementName, descriptor, topMethod);
        }
      }

      return descriptor;
    }
  }