Java Code Examples for org.apache.commons.lang3.reflect.FieldUtils#getAllFieldsList()

The following examples show how to use org.apache.commons.lang3.reflect.FieldUtils#getAllFieldsList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeigherHelpers.java    From kafka-workers with Apache License 2.0 6 votes vote down vote up
/**
 * This method for the given class computes estimated deep size of an instance of this class.
 * It counts all non-static fields assuming they are not null. Arrays are counted as empty however.
 * When a field is of an abstract or interface type no specific implementation is considered in computations.
 *
 * @param clazz class for which instance size has to be computed
 * @return estimated size in bytes the instance of the given class takes on heap
 */
public static int estimateInstanceSize(Class<?> clazz) {
    checkState(!clazz.isPrimitive());
    int shallowSize  = headerSize(clazz);
    int fieldsInstanceSize = 0;
    for (Field field : FieldUtils.getAllFieldsList(clazz)) {
        if (!isStatic(field.getModifiers())) {
            Class<?> fieldType = field.getType();
            int fieldSize = fieldSize(fieldType);
            shallowSize += fieldSize;
            if (!fieldType.isPrimitive() && !fieldType.isEnum()) {
                fieldsInstanceSize += estimateInstanceSize(fieldType);
            }
        }
    }

    int padding = padding(shallowSize);
    return shallowSize + padding + fieldsInstanceSize;
}
 
Example 2
Source File: CheckCore.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void checkListFieldContainerTableName(List<Class<?>> classes) throws Exception {
	for (Class<?> cls : classes) {
		List<Field> fields = FieldUtils.getAllFieldsList(cls);
		for (Field field : fields) {
			if (List.class.isAssignableFrom(field.getType())) {
				ContainerTable containerTable = field.getAnnotation(ContainerTable.class);
				if (null != containerTable) {
					String name = FieldUtils.readStaticField(cls, "TABLE", true).toString()
							+ JpaObject.ContainerTableNameMiddle + field.getName();
					if (!StringUtils.equals(name, containerTable.name())) {
						System.err.println(
								String.format("checkListFieldContainerTableName error: class: %s, field: %s.",
										cls.getName(), field.getName()));
					}
				}
			}
		}
	}
}
 
Example 3
Source File: CheckCore.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void checkColumnName(List<Class<?>> classes) throws Exception {
	for (Class<?> cls : classes) {
		List<Field> fields = FieldUtils.getAllFieldsList(cls);
		for (Field field : fields) {
			Column col = field.getAnnotation(Column.class);
			if (null != col) {
				if (!StringUtils.equals(JpaObject.ColumnNamePrefix + field.getName(), col.name())) {
					System.err.println(String.format("checkColumnName error: class: %s, field: %s.", cls.getName(),
							field.getName()));
				}
			}
		}
	}
}
 
Example 4
Source File: CheckCore.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void checkLobIndex(List<Class<?>> classes) throws Exception {
	for (Class<?> cls : classes) {
		List<Field> fields = FieldUtils.getAllFieldsList(cls);
		for (Field field : fields) {
			Lob lob = field.getAnnotation(Lob.class);
			Index index = field.getAnnotation(Index.class);
			if ((null != lob) && (null != index)) {
				System.err.println(String.format("checkLobIndex error: class: %s, field: %s.", cls.getName(),
						field.getName()));
			}
		}
	}
}
 
Example 5
Source File: ITJUnitUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public static void initClasses(Class<?>... classes) throws Throwable {
  for (Class<?> cls : classes) {
    for (Field field : FieldUtils.getAllFieldsList(cls)) {
      if (Consumers.class.isAssignableFrom(field.getType())
          || GateRestTemplate.class.isAssignableFrom(field.getType())
          || ITSCBRestTemplate.class.isAssignableFrom(field.getType())) {
        Object target = FieldUtils.readStaticField(field, true);
        MethodUtils.invokeMethod(target, "init");
      }
    }
  }
}
 
Example 6
Source File: FieldUtil.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * 获得 <code>klass</code> 排除某些 <code>excludeFieldNames</code> 之后的字段list.
 * 
 * <h3>说明:</h3>
 * <blockquote>
 * <ol>
 * <li>是所有字段的值(<b>不是属性</b>)</li>
 * <li>自动过滤私有并且静态的字段,比如 LOGGER serialVersionUID</li>
 * </ol>
 * </blockquote>
 * 
 * @param klass
 *            the klass
 * @param excludeFieldNames
 *            需要排除的field names,如果传递过来是null或者empty 那么不会判断
 * @return 如果 <code>obj</code> 是null,抛出 {@link NullPointerException}<br>
 *         如果 <code>excludeFieldNames</code> 是null或者empty,解析所有的field<br>
 *         如果 {@link FieldUtils#getAllFieldsList(Class)} 是null或者empty,返回 {@link Collections#emptyList()}<br>
 *         如果 <code>obj</code>没有字段或者字段都被参数 <code>excludeFieldNames</code> 排除掉了,返回 {@link Collections#emptyMap()}<br>
 * @see FieldUtils#getAllFieldsList(Class)
 * @since 1.7.1
 */
public static List<Field> getAllFieldList(final Class<?> klass,String...excludeFieldNames){
    Validate.notNull(klass, "klass can't be null!");
    //---------------------------------------------------------------

    //获得给定类的所有声明字段 {@link Field},包括所有的parents,包括 public/protect/private/inherited...
    List<Field> fieldList = FieldUtils.getAllFieldsList(klass);
    if (isNullOrEmpty(fieldList)){
        return emptyList();
    }

    //---------------------------------------------------------------
    Predicate<Field> excludeFieldPredicate = BeanPredicateUtil.containsPredicate("name", excludeFieldNames);
    Predicate<Field> staticPredicate = new Predicate<Field>(){

        @Override
        public boolean evaluate(Field field){
            int modifiers = field.getModifiers();
            // 私有并且静态 一般是log 或者  serialVersionUID
            boolean isStatic = Modifier.isStatic(modifiers);

            String pattern = "[{}.{}],modifiers:[{}]{}";
            LOGGER.trace(pattern, klass.getSimpleName(), field.getName(), modifiers, isStatic ? " [isStatic]" : EMPTY);
            return isStatic;
        }
    };
    return selectRejected(fieldList, PredicateUtils.orPredicate(excludeFieldPredicate, staticPredicate));
}