SSRFTestUtility.java 文件源码

java
阅读 23 收藏 0 点赞 0 评论 0

项目:lib-openssrf-test 作者:
/**
 * Query the Class type to find a WITH setter method for the indicated field.
 * <p>
 * @param type  the class type
 * @param field the field
 * @return
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException
 */
private static Object getEnumEntry(Class<?> type, Field field) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  /**
   * Inspect the WITH setter to determine if the field expects an enumerated
   * input.
   */
  Method with = SSRFUtility.findWithEnumMethod(type, field);
  if (with != null) {
    for (Class<?> parameterType : with.getParameterTypes()) {
      if (parameterType.isEnum()) {
        String fieldClassName = parameterType.toString().replace("class ", "").trim();
        Class<?> fieldClass = Class.forName(fieldClassName); // throws ClassNotFoundException
        /**
         * Get a random instance of the enumerated class type.
         */
        Enum fieldInstance = (Enum) fieldClass.getEnumConstants()[new Random().nextInt(fieldClass.getEnumConstants().length)];
        /**
         * If the instance has an XmlEnumValue annotation then return the XML
         * value.
         */
        XmlEnumValue xmlEnumValue = fieldInstance.getClass().getAnnotation(XmlEnumValue.class);
        if (xmlEnumValue != null) {
          return xmlEnumValue.value();
        }
        /**
         * Otherwise try to call the value getter.
         */
        for (Method method : fieldClass.getDeclaredMethods()) {
          if (method.getName().equals("value")) {
            return method.invoke(fieldInstance);
          }
        }
        /**
         * Finally just return the numerated instance name.
         */
        return fieldInstance.name();
      }
    }
  }
  /**
   * No enumerated instance found. Return null.
   */
  return null;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号