All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yuweix.kuafu.sequence.utils.FieldUtil Maven / Gradle / Ivy

The newest version!
package com.yuweix.kuafu.sequence.utils;


import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author yuwei
 */
public abstract class FieldUtil {
	private static SoftReference>> FIELD_REF;
	private static final Object fieldLock = new Object();

	private static Map> getFieldMap() {
		Map> map = null;
		if (FIELD_REF == null || (map = FIELD_REF.get()) == null) {
			synchronized (fieldLock) {
				if (FIELD_REF == null || (map = FIELD_REF.get()) == null) {
					map = new ConcurrentHashMap<>();
					FIELD_REF = new SoftReference<>(map);
				}
			}
		}
		return map;
	}
	public static List getAllFieldsList(Class clz) {
		String className = clz.getName();
		Map> map = getFieldMap();
		List fList = map.get(className);
		if (fList == null) {
			fList = getAllFieldsList0(clz);
			map.put(className, fList);
		}
		return fList;
	}
	/**
	 * Gets all fields of the given class and its parents (if any).
	 * @return
	 */
	private static List getAllFieldsList0(Class clz) {
		final List allFields = new ArrayList<>();
		Class currentClass = clz;
		while (currentClass != null) {
			allFields.addAll(Arrays.asList(currentClass.getDeclaredFields()));
			currentClass = currentClass.getSuperclass();
		}
		return allFields;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy