Class ObjectMapper<T>
- java.lang.Object
-
- com.identityworksllc.iiq.common.ObjectMapper<T>
-
- Type Parameters:
T
- The type of object being mapped
- Direct Known Subclasses:
SailpointObjectMapper
public class ObjectMapper<T> extends Object
A utility to decode a Map structure into the fields of a POJO object.This makes configurations type-safe and allows code accessing them to be verified.
While similar to Jackson’s own ObjectMapper, this class also natively supports a variety of SailPoint object types. It is intended for use with Custom and Configuration objects.
TYPE MAPPINGS:
This class can coerce the following field types natively:
- Boolean
- String
- Numeric values
- List of strings
- List of nested objects
- Map from string to nested objects
- Date
The
SailpointObjectMapper
subclass (the default) also handles:- IdentitySelector
- Filter
- Script
- A Reference object
- Any SailPointObject type that can be looked up via a string ID or name
Inaccessible fields and extra Map keys will be safely ignored, but a debug message will be logged if debug logging is enabled for this class.
NESTING and ANNOTATIONS:
You must use the @Nested annotation to indicate that a field is to be ObjectMapped itself. If the field is a list of objects or a map, you must specify the type of objects in the list or the map values as the argument to @Nested.
@Nested(SomeType.class) private Map<String, SomeType> map;
@Nested(ListType.class) private List
list; Maps with key types other than strings are not supported. (TODO)
If the field’s type is itself annotated with @
XmlRootElement
(from JAXB), it will be assumed to be @Nested automatically.Fields annotated with
ObjectMapper.Ignore
will always be unmapped. For example, a field that you set in the default constructor may be ignored.If a class is annotated with
ObjectMapper.IgnoreSuper
, all superclass fields will be ignored above that class in the hierarchy. This allows you to extend provided classes without having to worry about the ObjectMapper scanning their fields.By default, a field X is set via the broadest method named setX that is compatible with the field’s type. For example, if a field is of type Collection, a setX that takes a List will be preferred over a setX that takes an ArrayList. You may also specify a different setter method name using @SetterName on the field.
CACHING:
If the second parameter to any of the decode() methods is true, a cached copy of the decoded object will be returned. That is, two calls to decode with an identical Map input will produce the same (==) object output.
The default for the Configuration and Custom decode() methods is to true (yes do use the cache), and for the Map input it is false.
Cached decoded objects will be retained forever.
SHARING:
By default, this class has a strong dependency on its subclass,
SailpointObjectMapper
, in that an instance of that class is always returned fromget(Class)
. However, it is designed so that it can be fairly easily detached with only minimal modifications. It has no other external dependencies except Apache Commons Logging.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ObjectMapper.AfterMapperMethod
The method annotated with this annotation will be invoked after all mapped attributes are set.static interface
ObjectMapper.Aliases
static interface
ObjectMapper.Convertible
Implementing this interface allows a class to coerce itself from one or more input types.protected static class
ObjectMapper.DefaultTypeNamer
Translates from a Class type to a name.static interface
ObjectMapper.Ignore
Annotation to indicate that the given field should be ignored and not mapped.static interface
ObjectMapper.IgnoreSuper
Indicates that the annotated class should be the stopping point for walking up the class hierarchy to find setters.static interface
ObjectMapper.Nested
Annotation to indicate that the given element is a nested instance of the mapped class, such as an identity with a list of identities.static class
ObjectMapper.ObjectMapperException
The exception type thrown by all mapper methodsstatic interface
ObjectMapper.RawMap
static interface
ObjectMapper.SetterMethod
static interface
ObjectMapper.TypeNamer
A functional interface to translate a Class into its name.
-
Constructor Summary
Constructors Constructor Description ObjectMapper(Class<T> targetClass)
Basic constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
convertObject(Object value, Class<?> expectedType)
Converts the given object to the expected type.T
decode(Map<String,Object> map)
Decodes the given Map object into an instance of the mapped type.T
decode(Map<String,Object> map, boolean cache)
Decodes the given Map object into an instance of the mapped type.static <T> SailpointObjectMapper<T>
get(Class<T> type)
Gets a predefined mapper for the given type.Class<T>
getTargetClass()
Gets the target classstatic boolean
isNotNullOrEmpty(String in)
Returns true if the input string is not null or empty.static String
otoa(Object in)
Returns a string representation of the input.static boolean
otob(Object in)
Returns a Boolean reprsentation of the object.static List<String>
otol(Object in)
Transforms an arbitrary object into a List.
-
-
-
Constructor Detail
-
ObjectMapper
public ObjectMapper(Class<T> targetClass)
Basic constructor.You should prefer
get(Class)
to this.- Parameters:
targetClass
- the target class
-
-
Method Detail
-
get
public static <T> SailpointObjectMapper<T> get(Class<T> type)
Gets a predefined mapper for the given type.Mappers will be recalculated when the plugin cache is updated, allowing mapped classes to be changed at runtime.
- Type Parameters:
T
- The type parameter- Parameters:
type
- The type of class to map- Returns:
- A mapper for that type
-
isNotNullOrEmpty
public static boolean isNotNullOrEmpty(String in)
Returns true if the input string is not null or empty.This is a replica of the IIQ version of this class so that this class can be separated from IIQ code if needed.
- Parameters:
in
- The input string- Returns:
- True if the input string is not null or empty
-
otoa
public static String otoa(Object in)
Returns a string representation of the input.If the input is null, returns null. If the input is a string, returns the input. If the input is not a string, returns the input as passed through
String.valueOf(Object)
.- Parameters:
in
- The input object- Returns:
- The input object converted to a string
-
otob
public static boolean otob(Object in)
Returns a Boolean reprsentation of the object.If the object is a Boolean, it will be returned as-is. If the object is a String, the result will be the outcome of
Boolean.parseBoolean(String)
. Otherwise, the result is false.- Parameters:
in
- The input object- Returns:
- The output as described above
-
otol
public static List<String> otol(Object in)
Transforms an arbitrary object into a List.If the input is null, or if none of the below conditions match, the output is null. If the input is a List, it is returned as-is. If the input is another Collection, it is copied into a List and returned. If the input is a String, it is split on commas as a CSV and returned.
This method uses
CommonConstants.FUNC_CSV_PARSER
, so if you copy it outside of a Sailpoint environment, you’ll want to change that.- Parameters:
in
- The input object- Returns:
- The input converted to a list, or null if it could not be converted
-
convertObject
public Object convertObject(Object value, Class<?> expectedType) throws ObjectMapper.ObjectMapperException
Converts the given object to the expected type.If the input is null, a null will be returned. If the input is already compatible with the expected type, the existing object will be returned. If the input cannot be converted, an exception will be thrown.
- Parameters:
value
- The input valueexpectedType
- The expected type of the input- Returns:
- The converted object
- Throws:
ObjectMapper.ObjectMapperException
-
decode
public T decode(Map<String,Object> map) throws ObjectMapper.ObjectMapperException
Decodes the given Map object into an instance of the mapped type.If the introspection code is not initialized, it will be initialized at this time.
If a null map is passed, it will be swapped for an empty map.
Equivalent to decode(map, false)
- Parameters:
map
- The map object to convert- Returns:
- An object of the expected type
- Throws:
ObjectMapper.ObjectMapperException
- if any failure occur
-
decode
public T decode(Map<String,Object> map, boolean cache) throws ObjectMapper.ObjectMapperException
Decodes the given Map object into an instance of the mapped type.If the introspection code is not initialized, it will be initialized at this time.
If a null map is passed, it will be swapped for an empty map.
- Parameters:
map
- The map object to convertcache
- If true, the cached value will be returned if possible- Returns:
- An object of the expected type
- Throws:
ObjectMapper.ObjectMapperException
- if any failure occur
-
getTargetClass
public Class<T> getTargetClass()
Gets the target class- Returns:
- The target class
-
-