org.assertj.core.util.introspection
Class FieldSupport

java.lang.Object
  extended by org.assertj.core.util.introspection.FieldSupport

public class FieldSupport
extends Object

Utility methods for fields access.

Author:
Joel Costigliola

Constructor Summary
FieldSupport(boolean allowExtractingPrivateFields)
          Build a new FieldSupport
 
Method Summary
<T> T
fieldValue(String fieldName, Class<T> fieldClass, Object target)
          Return the value of field from a target object.
<T> List<T>
fieldValues(String fieldName, Class<T> fieldClass, Iterable<?> target)
          Returns a List containing the values of the given field name, from the elements of the given Iterable.
<T> List<T>
fieldValues(String fieldName, Class<T> fieldClass, Object[] target)
          Returns a List containing the values of the given field name, from the elements of the given Iterable.
 List<Object> fieldValues(String fieldName, Iterable<?> target)
           
static FieldSupport instance()
          Returns the singleton instance of this class.
static void setAllowExtractingPrivateFields(boolean allowExtractingPrivateFields)
          Globally set whether IterableAssert#extracting(String) and ObjectArrayAssert#extracting(String) should be allowed to extract private fields, if not and they try it fails with exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FieldSupport

public FieldSupport(boolean allowExtractingPrivateFields)
Build a new FieldSupport

Parameters:
allowExtractingPrivateFields - wether to read private fields or not.
Method Detail

instance

public static FieldSupport instance()
Returns the singleton instance of this class.

Returns:
the singleton instance of this class.

setAllowExtractingPrivateFields

public static void setAllowExtractingPrivateFields(boolean allowExtractingPrivateFields)
Globally set whether IterableAssert#extracting(String) and ObjectArrayAssert#extracting(String) should be allowed to extract private fields, if not and they try it fails with exception.

Parameters:
allowExtractingPrivateFields - allow private fields extraction. Default true.

fieldValues

public <T> List<T> fieldValues(String fieldName,
                               Class<T> fieldClass,
                               Iterable<?> target)
Returns a List containing the values of the given field name, from the elements of the given Iterable. If the given Iterable is empty or null, this method will return an empty List. This method supports nested fields (e.g. "address.street.number").

Parameters:
fieldName - the name of the field. It may be a nested field. It is left to the clients to validate for null or empty.
fieldClass - the expected type of the given field.
target - the given Iterable.
Returns:
an Iterable containing the values of the given field name, from the elements of the given Iterable.
Throws:
IntrospectionError - if an element in the given Iterable does not have a field with a matching name.

fieldValues

public List<Object> fieldValues(String fieldName,
                                Iterable<?> target)

fieldValues

public <T> List<T> fieldValues(String fieldName,
                               Class<T> fieldClass,
                               Object[] target)
Returns a List containing the values of the given field name, from the elements of the given Iterable. If the given Iterable is empty or null, this method will return an empty List. This method supports nested fields (e.g. "address.street.number").

Parameters:
fieldName - the name of the field. It may be a nested field. It is left to the clients to validate for null or empty.
fieldClass - the expected type of the given field.
target - the given Iterable.
Returns:
an Iterable containing the values of the given field name, from the elements of the given Iterable.
Throws:
IntrospectionError - if an element in the given Iterable does not have a field with a matching name.

fieldValue

public <T> T fieldValue(String fieldName,
                        Class<T> fieldClass,
                        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
 

Parameters:
fieldName - the name of the field. It may be a nested field. It is left to the clients to validate for null or empty.
target - the given object
clazz - type of field
Returns:
a the values of the given field name
Throws:
IntrospectionError - if the given target does not have a field with a matching name.


Copyright © 2013–2015 AssertJ. All rights reserved.