예제 #1
0
  /**
   * 通过反射,获得定义Class时声明的父类的泛型参数的类型. 如public UserDao extends HibernateDao<User,Long>
   *
   * @param clazz clazz The class to introspect
   * @param index the Index of the generic ddeclaration,start from 0.
   * @return the index generic declaration, or Object.class if cannot be determined
   */
  @SuppressWarnings("unchecked")
  public static Class getSuperClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
      VALog.w(clazz.getSimpleName() + "'s superclass not ParameterizedType");
      return Object.class;
    }

    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

    if (index >= params.length || index < 0) {
      VALog.w(
          "Index: "
              + index
              + ", Size of "
              + clazz.getSimpleName()
              + "'s Parameterized Type: "
              + params.length);
      return Object.class;
    }
    if (!(params[index] instanceof Class)) {
      VALog.w(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
      return Object.class;
    }
    return (Class) params[index];
  }
예제 #2
0
  /** 直接设置对象属性值,无视private/protected修饰符,不经过setter函数. */
  public static void setFieldValue(
      final Object object, final String fieldName, final Object value) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null)
      throw new IllegalArgumentException(
          "Could not find field [" + fieldName + "] on target [" + object + "]");

    makeAccessible(field);

    try {
      field.set(object, value);
    } catch (IllegalAccessException e) {
      VALog.e("不可能抛出的异常:{}" + e.getMessage());
    }
  }
예제 #3
0
  /** 直接读取对象属性值,无视private/protected修饰符,不经过getter函数. */
  public static Object getFieldValue(final Object object, final String fieldName) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null)
      throw new IllegalArgumentException(
          "Could not find field [" + fieldName + "] on target [" + object + "]");

    makeAccessible(field);

    Object result = null;
    try {
      result = field.get(object);
    } catch (IllegalAccessException e) {
      VALog.e("不可能抛出的异常{}" + e.getMessage());
    }
    return result;
  }