Java Class.getDeclaredFields Examples

Java Class.getDeclaredFields - 30 examples found. These are the top rated real world Java examples of Class.getDeclaredFields extracted from open source projects. You can rate examples to help us improve the quality of examples.
  protected static boolean matchesSegment(Field field, String segmentTag) {
    Class<?> clz = field.getType();

    if (Collection.class.isAssignableFrom(clz)) {
      clz = getCollectionType(field);
    }

    if (clz.isAnnotationPresent(EDISegment.class)) {
      EDISegment es = clz.getAnnotation(EDISegment.class);
      if (StringUtils.equals(segmentTag, es.tag())) {
        return true;
      }
    } else if (clz.isAnnotationPresent(EDISegmentGroup.class)) {
      EDISegmentGroup esg = clz.getAnnotation(EDISegmentGroup.class);

      String ediSegmentGroupTag = esg.header();

      if (StringUtils.isNotBlank(ediSegmentGroupTag)) {
        if (StringUtils.equals(segmentTag, ediSegmentGroupTag)) {
          return true;
        }
      } else {
        // get the segement group's first field, and recurse.
        if (clz.getDeclaredFields().length > 0) {
          return matchesSegment(clz.getDeclaredFields()[0], segmentTag);
        }
      }
    }

    return false;
  }
  /**
   * Handles case-insensitive lookups Note: Tests for plural form of fieldName, e.g. when the field
   * is a collection
   *
   * @param objClass
   * @param fieldName
   * @return
   */
  public static Field findField(Class objClass, String fieldName) {

    LOG.debug("Looking for field " + fieldName + " in " + objClass.getName());
    Field result = null;

    Field[] fields = objClass.getDeclaredFields();
    for (Field f : fields) {

      if (f.getName().compareToIgnoreCase(fieldName) == 0) {

        result = f;
        break;
      }
    }

    // Handle collections
    if (result == null) {

      fields = objClass.getDeclaredFields();
      for (Field f : fields) {

        if (f.getName().compareToIgnoreCase(fieldName + "s") == 0) {

          result = f;
          break;
        }
      }
    }

    return result;
  }
  protected static String getSegmentTag(Field field, boolean inner) {
    Class<?> clz = field.getType();

    if (Collection.class.isAssignableFrom(clz)) {
      clz = getCollectionType(field);
    }

    if (clz.isAnnotationPresent(EDISegment.class)) {
      EDISegment es = clz.getAnnotation(EDISegment.class);
      return es.tag();
    } else if (clz.isAnnotationPresent(EDISegmentGroup.class)) {
      EDISegmentGroup esg = clz.getAnnotation(EDISegmentGroup.class);

      String ediSegmentGroupTag = esg.header();

      if (!inner && StringUtils.isNotBlank(ediSegmentGroupTag)) {
        return ediSegmentGroupTag;
      } else {
        // get the segement group's first field, and recurse.
        if (clz.getDeclaredFields().length > 0) {
          return getSegmentTag(clz.getDeclaredFields()[0], false);
        }
      }
    }

    return null;
  }
Example #4
0
  /**
   * copy
   *
   * @param dest
   * @param orig
   * @param ignoreNull 忽略null,即orig为空的字段不做copy
   * @author Joeson
   */
  public static void copy(Object dest, Object orig, boolean ignoreNull) {
    if (null == dest || null == orig) {
      return;
    }

    Class destClz = dest.getClass();
    Class origClz = orig.getClass();

    Field[] destFields = destClz.getDeclaredFields();
    Field[] origFields = origClz.getDeclaredFields();

    for (Field origField : origFields) {
      try {
        String fieldName = origField.getName();
        origField.setAccessible(true);
        if (!ignoreNull || null != origField.get(orig)) {
          for (Field destField : destFields) {
            if (destField.getName().equals(origField.getName())
                && destField.getType().equals(origField.getType())) {
              destField.setAccessible(true);
              Object o = origField.get(orig);
              BeanUtils.copyProperty(dest, fieldName, o);
              break;
            }
          }
        }
      } catch (Exception e) {
        logger.error("copyNonNull exception.", e);
        throw new RuntimeException("copyNonNull exception.." + e.getMessage());
      }
    }
  }
  /**
   * This class returns a ClassStatsHolder object which holds statistics about a class
   *
   * @param classname
   * @return
   */
  private ClassStatsHolder getStatistics(String classname) {
    ClassStatsHolder ret = null;
    try {
      // get methods, constructors etc
      Class theClass = Class.forName(classname);
      Integer numFields = theClass.getDeclaredFields().length;
      Method[] methods = theClass.getDeclaredMethods();
      Integer members = (theClass.getDeclaredFields().length) + methods.length;
      Constructor[] constructors = theClass.getDeclaredConstructors();

      // create ararys of method names an number of parameters
      String[] totalMethods = new String[methods.length];
      Integer[] paramaters = new Integer[methods.length];
      for (int i = 0; i < methods.length; i++) {
        totalMethods[i] = methods[i].getName();
        paramaters[i] = methods[i].getParameterTypes().length;
      }

      ret =
          new ClassStatsHolder(
              classname, totalMethods, paramaters, members, constructors, numFields);
    } catch (ClassNotFoundException e) {
      e.printStackTrace(); // will never be reached if recursion is done
    }

    return ret;
  }
Example #6
0
  /**
   * 创建一个对象实例 该实例的值由一对象的相同属性赋值
   *
   * @param className 要创建实例的类全名
   * @param object 用于赋值的对象
   * @return
   */
  public Object fill(Class clazz, Object object) {

    try {
      // 通过类名得到实例
      Class instanceClass = clazz;
      Object instance = instanceClass.newInstance();

      Class objectClass = object.getClass();

      // 分别得到两个类声明的属性
      Field[] instanceFields = instanceClass.getDeclaredFields();
      Field[] objectFields = objectClass.getDeclaredFields();

      // 得到两个对象相同的属性
      Field instanceField;
      Field objectField;
      int instanceLength = instanceFields.length;
      for (int i = 0; i < instanceLength; i++) {

        instanceField = instanceFields[i];

        int objectLength = objectFields.length;
        for (int j = 0; j < objectLength; j++) {

          objectField = objectFields[j];

          // 遇到相同的属性 就把赋值对象的值 赋予要创建的实例
          if (instanceField.getName().equals(objectField.getName())) {

            // 关掉安全检查 使得能访问到非公有属性
            instanceField.setAccessible(true);
            objectField.setAccessible(true);

            // 把赋值对象的值 赋予要创建的实例
            instanceField.set(instance, objectField.get(object));
          }
        }
      }

      return instance;

    } catch (InstantiationException e) {
      // TODO 自动生成 catch 块
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO 自动生成 catch 块
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      // TODO 自动生成 catch 块
      e.printStackTrace();
    } catch (SecurityException e) {
      // TODO 自动生成 catch 块
      e.printStackTrace();
    }

    return null;
  }