Exemplo n.º 1
0
  /**
   * Method called to check if we can use the passed method or constructor (wrt access restriction
   * -- public methods can be called, others usually not); and if not, if there is a work-around for
   * the problem.
   */
  public static void checkAndFixAccess(Member member) {
    // We know all members are also accessible objects...
    AccessibleObject ao = (AccessibleObject) member;

    /* 14-Jan-2009, tatu: It seems safe and potentially beneficial to
     *   always to make it accessible (latter because it will force
     *   skipping checks we have no use for...), so let's always call it.
     */
    // if (!ao.isAccessible()) {
    try {
      ao.setAccessible(true);
    } catch (SecurityException se) {
      /* 17-Apr-2009, tatu: Related to [JACKSON-101]: this can fail on
       *    platforms like EJB and Google App Engine); so let's
       *    only fail if we really needed it...
       */
      if (!ao.isAccessible()) {
        Class<?> declClass = member.getDeclaringClass();
        throw new IllegalArgumentException(
            "Can not access "
                + member
                + " (from class "
                + declClass.getName()
                + "; failed to set access: "
                + se.getMessage());
      }
    }
    // }
  }
  /**
   * Construct a variable
   *
   * @param aType the type
   * @param aName the name
   * @param aValue the value
   */
  public Variable(Class<?> aType, String aName, Object aValue) {
    type = aType;
    name = aName;
    value = aValue;
    fields = new ArrayList<Field>();

    // find all fields if we have a class type except we don't expand strings and null values

    if (!type.isPrimitive() && !type.isArray() && !type.equals(String.class) && value != null) {
      // get fields from the class and all superclasses
      for (Class<?> c = value.getClass(); c != null; c = c.getSuperclass()) {
        Field[] fs = c.getDeclaredFields();
        AccessibleObject.setAccessible(fs, true);

        // get all nonstatic fields
        for (Field f : fs) if ((f.getModifiers() & Modifier.STATIC) == 0) fields.add(f);
      }
    }
  }