Enum FieldSupport
- java.lang.Object
-
- java.lang.Enum<FieldSupport>
-
- org.assertj.core.util.introspection.FieldSupport
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<FieldSupport>
public enum FieldSupport extends java.lang.Enum<FieldSupport>
Utility methods for fields access.
-
-
Enum Constant Summary
Enum Constants Enum Constant Description COMPARISON
EXTRACTION
EXTRACTION_OF_PUBLIC_FIELD_ONLY
-
Field Summary
Fields Modifier and Type Field Description private boolean
allowUsingPrivateFields
private static java.lang.String
BOOLEAN
private static java.lang.String
BYTE
private static java.lang.String
CHAR
private static java.lang.String
DOUBLE
private static java.lang.String
FLOAT
private static java.lang.String
INT
private static java.lang.String
LONG
private static java.lang.String
SEPARATOR
private static java.lang.String
SHORT
-
Constructor Summary
Constructors Modifier Constructor Description private
FieldSupport(boolean allowUsingPrivateFields)
Build a newFieldSupport
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static FieldSupport
comparison()
Returns the instance dedicated to comparison of fields.static FieldSupport
extraction()
Returns the instance dedicated to extraction of fields.<T> T
fieldValue(java.lang.String fieldName, java.lang.Class<T> fieldClass, java.lang.Object target)
Return the value of field from a target object.<T> java.util.List<T>
fieldValues(java.lang.String fieldName, java.lang.Class<T> fieldClass, java.lang.Iterable<?> target)
Returns a
containing the values of the given field name, from the elements of the givenList
.Iterable
<T> java.util.List<T>
fieldValues(java.lang.String fieldName, java.lang.Class<T> fieldClass, java.lang.Object[] target)
Returns a
containing the values of the given field name, from the elements of the givenList
.Iterable
java.util.List<java.lang.Object>
fieldValues(java.lang.String fieldName, java.lang.Iterable<?> target)
boolean
isAllowedToRead(java.lang.reflect.Field field)
boolean
isAllowedToUsePrivateFields()
private boolean
isNestedField(java.lang.String fieldName)
private java.lang.String
nextFieldNameFrom(java.lang.String fieldNameChain)
private java.lang.String
popFieldNameFrom(java.lang.String fieldNameChain)
private <T> T
readSimpleField(java.lang.String fieldName, java.lang.Class<T> clazz, java.lang.Object target)
void
setAllowUsingPrivateFields(boolean allowUsingPrivateFields)
Sets whether the use of private fields is allowed.private <T> java.util.List<T>
simpleFieldValues(java.lang.String fieldName, java.lang.Class<T> clazz, java.lang.Iterable<?> target)
static FieldSupport
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.static FieldSupport[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
EXTRACTION
public static final FieldSupport EXTRACTION
-
EXTRACTION_OF_PUBLIC_FIELD_ONLY
public static final FieldSupport EXTRACTION_OF_PUBLIC_FIELD_ONLY
-
COMPARISON
public static final FieldSupport COMPARISON
-
-
Field Detail
-
CHAR
private static final java.lang.String CHAR
- See Also:
- Constant Field Values
-
BOOLEAN
private static final java.lang.String BOOLEAN
- See Also:
- Constant Field Values
-
DOUBLE
private static final java.lang.String DOUBLE
- See Also:
- Constant Field Values
-
FLOAT
private static final java.lang.String FLOAT
- See Also:
- Constant Field Values
-
LONG
private static final java.lang.String LONG
- See Also:
- Constant Field Values
-
INT
private static final java.lang.String INT
- See Also:
- Constant Field Values
-
SHORT
private static final java.lang.String SHORT
- See Also:
- Constant Field Values
-
BYTE
private static final java.lang.String BYTE
- See Also:
- Constant Field Values
-
SEPARATOR
private static final java.lang.String SEPARATOR
- See Also:
- Constant Field Values
-
allowUsingPrivateFields
private boolean allowUsingPrivateFields
-
-
Constructor Detail
-
FieldSupport
private FieldSupport(boolean allowUsingPrivateFields)
Build a newFieldSupport
- Parameters:
allowUsingPrivateFields
- whether to read private fields or not.
-
-
Method Detail
-
values
public static FieldSupport[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (FieldSupport c : FieldSupport.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static FieldSupport valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is null
-
extraction
public static FieldSupport extraction()
Returns the instance dedicated to extraction of fields.- Returns:
- the instance dedicated to extraction of fields.
-
comparison
public static FieldSupport comparison()
Returns the instance dedicated to comparison of fields.- Returns:
- the instance dedicated to comparison of fields.
-
isAllowedToUsePrivateFields
public boolean isAllowedToUsePrivateFields()
-
setAllowUsingPrivateFields
public void setAllowUsingPrivateFields(boolean allowUsingPrivateFields)
Sets whether the use of private fields is allowed. If a method tries to extract/compare private fields and is not allowed to, it will fail with an exception.- Parameters:
allowUsingPrivateFields
- allow private fields extraction and comparison. Defaulttrue
.
-
fieldValues
public <T> java.util.List<T> fieldValues(java.lang.String fieldName, java.lang.Class<T> fieldClass, java.lang.Iterable<?> target)
Returns a
containing the values of the given field name, from the elements of the givenList
. If the givenIterable
Iterable
is empty ornull
, this method will return an emptyList
. This method supports nested fields (e.g. "address.street.number").- Type Parameters:
T
- the type of the extracted elements.- Parameters:
fieldName
- the name of the field. It may be a nested field. It is left to the clients to validate fornull
or empty.fieldClass
- the expected type of the given field.target
- the givenIterable
.- Returns:
- an
Iterable
containing the values of the given field name, from the elements of the givenIterable
. - Throws:
IntrospectionError
- if an element in the givenIterable
does not have a field with a matching name.
-
fieldValues
public java.util.List<java.lang.Object> fieldValues(java.lang.String fieldName, java.lang.Iterable<?> target)
-
fieldValues
public <T> java.util.List<T> fieldValues(java.lang.String fieldName, java.lang.Class<T> fieldClass, java.lang.Object[] target)
Returns a
containing the values of the given field name, from the elements of the givenList
. If the givenIterable
Iterable
is empty ornull
, this method will return an emptyList
. This method supports nested fields (e.g. "address.street.number").- Type Parameters:
T
- the type of the extracted elements.- Parameters:
fieldName
- the name of the field. It may be a nested field. It is left to the clients to validate fornull
or empty.fieldClass
- the expected type of the given field.target
- the givenIterable
.- Returns:
- an
Iterable
containing the values of the given field name, from the elements of the givenIterable
. - Throws:
IntrospectionError
- if an element in the givenIterable
does not have a field with a matching name.
-
simpleFieldValues
private <T> java.util.List<T> simpleFieldValues(java.lang.String fieldName, java.lang.Class<T> clazz, java.lang.Iterable<?> target)
-
popFieldNameFrom
private java.lang.String popFieldNameFrom(java.lang.String fieldNameChain)
-
nextFieldNameFrom
private java.lang.String nextFieldNameFrom(java.lang.String fieldNameChain)
-
isNestedField
private boolean isNestedField(java.lang.String fieldName)
-
fieldValue
public <T> T fieldValue(java.lang.String fieldName, java.lang.Class<T> fieldClass, java.lang.Object target)
Return the value of field from a target object.Return null if field is nested and one of the nested value is null, ex :
fieldValue(race.name, String.class, frodo); // will return null if frodo.race is null
- Type Parameters:
T
- the type of the extracted value.- Parameters:
fieldName
- the name of the field. It may be a nested field. It is left to the clients to validate fornull
or empty.target
- the given objectfieldClass
- type of field- Returns:
- the value of the given field name
- Throws:
IntrospectionError
- if the given target does not have a field with a matching name.
-
readSimpleField
private <T> T readSimpleField(java.lang.String fieldName, java.lang.Class<T> clazz, java.lang.Object target)
-
isAllowedToRead
public boolean isAllowedToRead(java.lang.reflect.Field field)
-
-