
org.kasource.commons.reflection.util.FieldUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ka-commons-reflection Show documentation
Show all versions of ka-commons-reflection Show documentation
Common utilities and classes for reflection
package org.kasource.commons.reflection.util;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import org.kasource.commons.reflection.filter.fields.FieldFilter;
public final class FieldUtils {
private FieldUtils() { }
/**
* Returns a set of all fields matching the supplied filter
* declared in the clazz class.
*
* @param clazz The class to inspect.
* @param filter Filter to use.
*
* @return All matching fields declared by the clazz class.
**/
public static Set getDeclaredFields(Class> clazz, FieldFilter filter) {
Set fields = new HashSet();
Field[] allFields = clazz.getDeclaredFields();
for(Field field : allFields) {
if(filter.passFilter(field)) {
fields.add(field);
}
}
return fields;
}
/**
* Returns a set of all fields matching the supplied filter
* declared in the target class or any of its super classes.
*
* @param filter Filter to use.
*
* @return All matching fields declared by the target class.
**/
public static Set getFields(Class> target, FieldFilter filter) {
Class> clazz = target;
Set fields = getDeclaredFields(clazz, filter);
while((clazz = clazz.getSuperclass()) != null) {
fields.addAll(getDeclaredFields(clazz, filter));
}
return fields;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy