public final class BeanUtils extends Object
Field shorthand utility class used to collect fields from classes meeting Java Bean restrictions/requirements.
With this utility class you can perform field lookups, by combining lookup restriction criteria.
Example
"find all fields on a class Apple, not looking at its super classes, which should be protected, have a getter method, but not a setter
method"
FieldUtils.collectFields(Apple.class, Apple.class, EnumSet.of(Visibility.PROTECTED),
EnumSet.of(BeanRestriction.YES_GETTER, BeanRestriction.NO_SETTER));
| Modifier and Type | Class and Description |
|---|---|
static class |
BeanUtils.BeanRestriction
Indicates whether a field needs a Bean setter or getter, exactly none or any combination thereof.
|
static class |
BeanUtils.Visibility
Determines what visibility modifiers a field is allowed to have in
collectFields(Class, Class, EnumSet, EnumSet). |
| Constructor and Description |
|---|
BeanUtils() |
| Modifier and Type | Method and Description |
|---|---|
static @NotNull LinkedHashMap<Class<?>,List<FieldWrapper>> |
collectFields(Class<?> _class,
Class<?> boundaryMarker,
EnumSet<BeanUtils.Visibility> visibility,
EnumSet<BeanUtils.BeanRestriction> beanRestrictions)
Returns a pool of
Field wrappers including optional relevant setter/getter methods, collected from the given class tested
against the given visibility and Bean restriction requirements. |
static Object |
invokeBeanGetter(Object o,
String fieldName)
Calls the getter for the first field in the inheritance chain that matches given fieldName.
|
static Object |
invokeBeanSetter(Object o,
String fieldName,
Object value)
Calls the setter for the first field in the inheritance chain that matches given fieldName.
|
static boolean |
isBeanMethod(Method method,
Class<?> boundaryMarker,
EnumSet<BeanUtils.Visibility> visibility)
Verifies is a given method occurs as setter or getter in the declaring class chain.
|
static boolean |
isBeanMethod(Method method,
Class<?> boundaryMarker,
EnumSet<BeanUtils.Visibility> visibility,
boolean checkBeanLikeForInterfaces) |
static boolean |
methodIsBeanlike(Method method)
Determines if the method could be a bean method by looking just at its name, parameters and presence of return type.
|
public static boolean isBeanMethod(Method method, Class<?> boundaryMarker, EnumSet<BeanUtils.Visibility> visibility)
Note that this is a strict lookup and interface methods are not considered bean methods. To include interfaces and their methods,
use isBeanMethod(Method, Class, EnumSet, boolean) with checkBeanLikeForInterfaces set to true.
Lookup can be configured to check only against specific visibility.
method - The method to match against getters/setters of a certain visibilityboundaryMarker - The last class> or interface implementing class that methods are matched against. Can
be used to prevent matching methods on a super class.visibility - A set of visibility requirements (ie. BeanUtils.Visibility.PROTECTED indicates a *field* for which getter/setter are checked
is allowed to have protected visibility). Note: the visibility modifiers for methods are ignored.public static boolean isBeanMethod(Method method, Class<?> boundaryMarker, EnumSet<BeanUtils.Visibility> visibility, boolean checkBeanLikeForInterfaces)
isBeanMethod(Method, Class, EnumSet), but may consider methods declared on interfaces as well.public static boolean methodIsBeanlike(Method method)
@NotNull public static @NotNull LinkedHashMap<Class<?>,List<FieldWrapper>> collectFields(Class<?> _class, Class<?> boundaryMarker, EnumSet<BeanUtils.Visibility> visibility, EnumSet<BeanUtils.BeanRestriction> beanRestrictions)
Field wrappers including optional relevant setter/getter methods, collected from the given class tested
against the given visibility and Bean restriction requirements.
The returned fields are mapped against the classes they were found on, since field names can be declared multiple times with the same name.
_class - The class (and chain) to harvest fields from.boundaryMarker - The last class> or interface implementing class that fields are collected from. Can
be used to prevent finding fields on a super class.visibility - A set of visibility requirements (ie. BeanUtils.Visibility.PROTECTED indicates a field is allowed to have
protected visibility).beanRestrictions - A set of Bean restriction requirements indicating a field should or shouldn't have a setter, getter or both.meetsVisibilityRequirements(Field, EnumSet),
resolveBeanProperty(Field, EnumSet)public static Object invokeBeanSetter(Object o, String fieldName, Object value)
Copyright © 2019. All rights reserved.