Class ObjectMapper<T>
java.lang.Object
ninja.leaping.configurate.objectmapping.ObjectMapper<T>
- Type Parameters:
T
- The type to work with
public class ObjectMapper<T> extends Object
This is the object mapper. It handles conversion between configuration nodes and
fields annotated with
Setting
in objects.
Values in the node not used by the mapped object will be preserved.-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ObjectMapper.BoundInstance
Represents an object mapper bound to a certain instance of the objectprotected static class
ObjectMapper.FieldData
Holder for field-specific information -
Constructor Summary
Constructors Modifier Constructor Description protected
ObjectMapper(TypeToken<T> type)
Create a new object mapper of a given typeprotected
ObjectMapper(Class<T> type)
Deprecated. -
Method Summary
Modifier and Type Method Description ObjectMapper.BoundInstance
bind(T instance)
Return a view on this mapper that is bound to a single object instanceObjectMapper.BoundInstance
bindToNew()
Returns a view on this mapper that is bound to a newly created object instanceboolean
canCreateInstances()
Returns whether this object mapper can create new object instances.protected void
collectFields(Map<String,ObjectMapper.FieldData> cachedFields, TypeToken<? super T> clazz)
protected void
collectFields(Map<String,ObjectMapper.FieldData> cachedFields, Class<? super T> clazz)
Deprecated.UsecollectFields(Map, TypeToken)
insteadprotected T
constructObject()
Create a new instance of an object of the appropriate type.static <T> ObjectMapper<T>
forClass(@NonNull Class<T> clazz)
Create a new object mapper that can work with objects of the given class using theDefaultObjectMapperFactory
.static <T> ObjectMapper.BoundInstance
forObject(@NonNull T obj)
Creates a new object mapper bound to the given object.static <T> ObjectMapper.BoundInstance
forObject(TypeToken<T> type, @NonNull T obj)
Creates a new object mapper bound to the given object.static <T> ObjectMapper<T>
forType(@NonNull TypeToken<T> type)
Create a new object mapper that can work with objects of the given type using theDefaultObjectMapperFactory
.Class<T>
getMappedType()
Deprecated.UsegetType()
to be aware of parameterized typesTypeToken<T>
getType()
protected boolean
isLegacy()
Deprecated.Backwards compatibility measure, to be removed in 4.0
-
Constructor Details
-
ObjectMapper
Deprecated.UseObjectMapper(TypeToken)
instead to support parameterized typesCreate a new object mapper of a given type. The given type must not be an interface.- Parameters:
type
- The class this object mapper will work with- Throws:
ObjectMappingException
- When errors occur discovering fields in the class
-
ObjectMapper
Create a new object mapper of a given type- Parameters:
type
- The type this object mapper will work with- Throws:
ObjectMappingException
- When errors occur discovering fields in the class
-
-
Method Details
-
forClass
Create a new object mapper that can work with objects of the given class using theDefaultObjectMapperFactory
.- Type Parameters:
T
- The type- Parameters:
clazz
- The type of object- Returns:
- An appropriate object mapper instance. May be shared with other users.
- Throws:
ObjectMappingException
- If invalid annotated fields are presented
-
forType
public static <T> ObjectMapper<T> forType(@NonNull TypeToken<T> type) throws ObjectMappingExceptionCreate a new object mapper that can work with objects of the given type using theDefaultObjectMapperFactory
.- Type Parameters:
T
- The type- Parameters:
type
- The type of object- Returns:
- An appropriate object mapper instance. May be shared with other users.
- Throws:
ObjectMappingException
- If invalid annotated fields are presented
-
forObject
public static <T> ObjectMapper.BoundInstance forObject(@NonNull T obj) throws ObjectMappingExceptionCreates a new object mapper bound to the given object. CAUTION Generic type information will be lost when creating a mapper. Provide a TypeToken to avoid this- Type Parameters:
T
- The object type- Parameters:
obj
- The object- Returns:
- An appropriate object mapper instance.
- Throws:
ObjectMappingException
- when an object is provided that is not suitable for object mapping. Reasons may include but are not limited to:- Not annotated with
ConfigSerializable
annotation - Invalid field types
- Not annotated with
-
forObject
public static <T> ObjectMapper.BoundInstance forObject(TypeToken<T> type, @NonNull T obj) throws ObjectMappingExceptionCreates a new object mapper bound to the given object.- Type Parameters:
T
- The object type- Parameters:
type
- generic type of objectobj
- The object- Returns:
- An appropriate object mapper instance.
- Throws:
ObjectMappingException
- when an object is provided that is not suitable for object mapping. Reasons may include but are not limited to:- Not annotated with
ConfigSerializable
annotation - Invalid field types
- Specified type is an interface
- Not annotated with
-
isLegacy
Deprecated.Backwards compatibility measure, to be removed in 4.0Method to determine if this ObjectMapper instance needs to maintain pre-3.7 behaviour. Override this if you are ready to take advantage of new 3.7 changes and also will be providing a subclass of ObjectMapper that overrides the old collectFields- Returns:
- true to get legacy, less generic-aware treatment
-
collectFields
@Deprecated protected void collectFields(Map<String,ObjectMapper.FieldData> cachedFields, Class<? super T> clazz) throws ObjectMappingExceptionDeprecated.UsecollectFields(Map, TypeToken)
insteadGather fields from a class, without having calculated types present- Parameters:
cachedFields
- map to contribute fields toclazz
- active class to scan- Throws:
ObjectMappingException
- when an error occurs
-
collectFields
protected void collectFields(Map<String,ObjectMapper.FieldData> cachedFields, TypeToken<? super T> clazz) throws ObjectMappingException- Throws:
ObjectMappingException
-
constructObject
Create a new instance of an object of the appropriate type. This method is not responsible for any population.- Returns:
- The new object instance
- Throws:
ObjectMappingException
- If constructing a new instance was not possible
-
canCreateInstances
Returns whether this object mapper can create new object instances. This may be false if the provided class has no zero-argument constructors.- Returns:
- Whether new object instances can be created
-
bind
Return a view on this mapper that is bound to a single object instance- Parameters:
instance
- The instance to bind to- Returns:
- A view referencing this mapper and the bound instance
-
bindToNew
Returns a view on this mapper that is bound to a newly created object instance- Returns:
- Bound mapper attached to a new object instance
- Throws:
ObjectMappingException
- If the object could not be constructed correctly- See Also:
bind(Object)
-
getMappedType
Deprecated.UsegetType()
to be aware of parameterized typesGet the mapped class.- Returns:
- class
-
getType
-
ObjectMapper(TypeToken)
instead to support parameterized types