Ejemplo n.º 1
0
  private MutableTreeNode populateAttributes(CavityDBObject obj) {
    DefaultMutableTreeNode tree = new DefaultMutableTreeNode("attrs");
    Class cls = obj.getClass();
    for (Field f : cls.getFields()) {
      int mod = f.getModifiers();
      if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
        String fieldName = f.getName();
        try {
          Object value = f.get(obj);
          tree.add(
              new DefaultMutableTreeNode(String.format("%s=%s", fieldName, String.valueOf(value))));

        } catch (IllegalAccessException e) {
          // do nothing.
        }
      }
    }
    return tree;
  }
  /**
   * 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);
      }
    }
  }