public class BeanSession extends Session
Serializer
or Parser
.
This class is NOT thread safe. It is meant to be discarded after one-time use.
Modifier | Constructor and Description |
---|---|
protected |
BeanSession(BeanContext ctx,
BeanSessionArgs args)
Create a new session using properties specified in the context.
|
Modifier and Type | Method and Description |
---|---|
ClassMeta<Class> |
_class()
Returns a reusable
ClassMeta representation for the class Class . |
ObjectMap |
asMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
void |
checkForWarnings()
Throws a
BeanRuntimeException if any warnings occurred in this session. |
<T> T |
convertToMemberType(Object outer,
Object value,
Class<T> type)
Same as
convertToType(Object, Class) , except used for instantiating inner member classes that must
be instantiated within another class instance. |
<T> T |
convertToMemberType(Object outer,
Object value,
ClassMeta<T> type)
Same as
convertToType(Object, ClassMeta) , except used for instantiating inner member classes that must
be instantiated within another class instance. |
<T> T |
convertToType(Object value,
Class<T> type)
Converts the specified value to the specified class type.
|
<T> T |
convertToType(Object value,
ClassMeta<T> type)
Casts the specified value into the specified type.
|
<T> T |
convertToType(Object value,
Type type,
Type... args)
Same as
convertToType(Object, Class) , but allows for complex data types consisting of collections or maps. |
ClassMeta<Object[]> |
getArgsClassMeta(Type[] classes)
|
<T> BeanMeta<T> |
getBeanMeta(Class<T> c)
Returns the
BeanMeta class for the specified class. |
BeanRegistry |
getBeanRegistry()
Returns the bean registry defined in this bean context defined by
BeanContext.BEAN_beanDictionary . |
String |
getBeanTypePropertyName(ClassMeta cm)
Returns the type property name as defined by
BeanContext.BEAN_beanTypePropertyName . |
<T> ClassMeta<T> |
getClassMeta(Class<T> c)
Returns a
ClassMeta wrapper around a Class object. |
<T> ClassMeta<T> |
getClassMeta(Type type,
Type... args)
Used to resolve
ClassMetas of type Collection and Map that have
ClassMeta values that themselves could be collections or maps. |
<T> ClassMeta<T> |
getClassMetaForObject(T o)
Shortcut for calling
getClassMeta(o.getClass()) . |
Locale |
getLocale()
Returns the locale defined on this session.
|
MediaType |
getMediaType()
Returns the media type specified for this session.
|
StringBuilder |
getStringBuilder()
Creates a reusable
StringBuilder object from an internal pool. |
TimeZone |
getTimeZone()
Returns the timezone defined on this session.
|
boolean |
isBean(Class<?> c)
Determines whether the specified class matches the requirements on this context of being a bean.
|
boolean |
isBean(Object o)
Determines whether the specified object matches the requirements on this context of being a bean.
|
boolean |
isDebug()
Returns the
BeanContext.BEAN_debug setting value for this session. |
boolean |
isIgnoreUnknownBeanProperties()
Bean property getter:
|
<T> T |
newBean(Class<T> c)
Creates a new empty bean of the specified type, except used for instantiating inner member classes that must
be instantiated within another class instance.
|
<T> T |
newBean(Object outer,
Class<T> c)
Same as
newBean(Class) , except used for instantiating inner member classes that must be instantiated
within another class instance. |
<T> BeanMap<T> |
newBeanMap(Class<T> c)
Creates a new
BeanMap object (i.e. |
<T> BeanMap<T> |
newBeanMap(Object outer,
Class<T> c)
Same as
newBeanMap(Class) , except used for instantiating inner member classes that must be instantiated
within another class instance. |
<T> T |
newInstance(Class<T> c,
Object c2)
Creates an instance of the specified class.
|
<T> T |
newInstance(Class<T> c,
Object c2,
boolean fuzzyArgs,
Object... args)
Creates an instance of the specified class.
|
<T> T |
newInstanceFromOuter(Object outer,
Class<T> c,
Object c2,
boolean fuzzyArgs,
Object... args)
Creates an instance of the specified class from within the context of another object.
|
ClassMeta<Object> |
object()
Returns a reusable
ClassMeta representation for the class Object . |
void |
returnStringBuilder(StringBuilder sb)
Returns a
StringBuilder object back into the internal reuse pool. |
ClassMeta<String> |
string()
Returns a reusable
ClassMeta representation for the class String . |
Object |
toArray(ClassMeta<?> type,
Collection<?> list)
Converts the contents of the specified list into an array.
|
<T> BeanMap<T> |
toBeanMap(T o)
Wraps an object inside a
BeanMap object (i.e. |
<T> BeanMap<T> |
toBeanMap(T o,
Class<? super T> c)
|
addToCache, addToCache, addWarning, getClassProperty, getFromCache, getInstanceArrayProperty, getInstanceProperty, getLogger, getProperties, getProperty, getProperty, getProperty, getPropertyKeys, getWarnings, hasWarnings, toString
protected BeanSession(BeanContext ctx, BeanSessionArgs args)
ctx
- The context creating this session object.
The context contains all the configuration settings for this object.args
- Runtime session arguments.public ObjectMap asMap()
Session
public final Locale getLocale()
The locale is determined in the following order:
locale
parameter passed in through constructor.
BeanContext.BEAN_locale
entry in parameter passed in through constructor.
BeanContext.BEAN_locale
setting on bean context.
Locale.getDefault()
.
public final TimeZone getTimeZone()
The timezone is determined in the following order:
timeZone
parameter passed in through constructor.
BeanContext.BEAN_timeZone
entry in parameter passed in through constructor.
BeanContext.BEAN_timeZone
setting on bean context.
public final boolean isDebug()
BeanContext.BEAN_debug
setting value for this session.BeanContext.BEAN_debug
setting value for this session.public final boolean isIgnoreUnknownBeanProperties()
public final <T> T convertToType(Object value, Class<T> type) throws InvalidDataConversionException
See convertToType(Object, ClassMeta)
for the list of valid conversions.
T
- The class type to convert the value to.value
- The value to convert.type
- The class type to convert the value to.InvalidDataConversionException
- If the specified value cannot be converted to the specified type.public final <T> T convertToMemberType(Object outer, Object value, Class<T> type) throws InvalidDataConversionException
convertToType(Object, Class)
, except used for instantiating inner member classes that must
be instantiated within another class instance.T
- The class type to convert the value to.outer
- If class is a member class, this is the instance of the containing class.
Should be value
- The value to convert.type
- The class type to convert the value to.InvalidDataConversionException
- If the specified value cannot be converted to the specified type.public final <T> T convertToType(Object value, ClassMeta<T> type) throws InvalidDataConversionException
If the value isn't an instance of the specified type, then converts the value if possible.
The following conversions are valid:
Convert to type | Valid input value types | Notes |
---|---|---|
A class that is the normal type of a registered PojoSwap .
|
A value whose class matches the transformed type of that registered PojoSwap .
|
|
A class that is the transformed type of a registered PojoSwap .
|
A value whose class matches the normal type of that registered PojoSwap .
|
|
Number (e.g. Integer , Short , Float ,...)
Number. (e.g. Integer. ,
Short. , Float. ,...)
|
Number , String , |
For primitive TYPES , |
Map (e.g. Map , HashMap , TreeMap , ObjectMap )
|
Map
|
If Map is not constructible, a ObjectMap is created.
|
Collection (e.g. List , LinkedList , HashSet , ObjectList )
|
Collection<Object>
Object[]
|
If Collection is not constructible, a ObjectList is created.
|
X[] (array of any type X)
|
List<X>
|
|
X[][] (multi-dimensional arrays)
|
List<List<X>>
List<X[]>
List[]<X>
|
|
Enum
|
String
|
|
Bean |
Map
|
|
String
|
Anything | Arrays are converted to JSON arrays |
Anything with one of the following methods:
|
String
|
|
T
- The class type to convert the value to.value
- The value to be converted.type
- The target object type.InvalidDataConversionException
- If the specified value cannot be converted to the specified type.public final <T> T convertToType(Object value, Type type, Type... args) throws InvalidDataConversionException
convertToType(Object, Class)
, but allows for complex data types consisting of collections or maps.T
- The class type to convert the value to.value
- The value to be converted.type
- The target object type.args
- The target object parameter types.InvalidDataConversionException
- If the specified value cannot be converted to the specified type.public final <T> T convertToMemberType(Object outer, Object value, ClassMeta<T> type) throws InvalidDataConversionException
convertToType(Object, ClassMeta)
, except used for instantiating inner member classes that must
be instantiated within another class instance.T
- The class type to convert the value to.outer
- If class is a member class, this is the instance of the containing class.
Should be value
- The value to convert.type
- The class type to convert the value to.InvalidDataConversionException
- If the specified value cannot be converted to the specified type.public final Object toArray(ClassMeta<?> type, Collection<?> list)
Works on both object and primitive arrays.
In the case of multi-dimensional arrays, the incoming list must contain elements of type n-1 dimension.
i.e. if type
is
then list
must have entries of type
.
type
- The type to convert to. Must be an array type.list
- The contents to populate the array with.public final <T> BeanMap<T> toBeanMap(T o)
BeanMap
object (i.e. a modifiable Map
).
If object is not a true bean, then throws a BeanRuntimeException
with an explanation of why it's not a
bean.
T
- The class of the object being wrapped.o
- The object to wrap in a map interface. Must not be null.public final boolean isBean(Object o)
o
- The object being tested.public final boolean isBean(Class<?> c)
c
- The class being tested.public final <T> BeanMap<T> toBeanMap(T o, Class<? super T> c) throws BeanRuntimeException
BeanMap
object (i.e.: a modifiable Map
) defined as a bean for one of its
class, a super class, or an implemented interface.
If object is not a true bean, throws a BeanRuntimeException
with an explanation of why it's not a bean.
T
- The class of the object being wrapped.o
- The object to wrap in a bean interface. Must not be null.c
- The superclass to narrow the bean properties to. Must not be null.NullPointerException
- If either parameter is null.IllegalArgumentException
- If the specified object is not an an instance of the specified class.BeanRuntimeException
- If specified object is not a bean according to the bean rules specified in this context
class.public final <T> BeanMap<T> newBeanMap(Class<T> c)
BeanMap
object (i.e. a modifiable Map
) of the given class with uninitialized
property values.
If object is not a true bean, then throws a BeanRuntimeException
with an explanation of why it's not a
bean.
T
- The class of the object being wrapped.c
- The name of the class to create a new instance of.public final <T> BeanMap<T> newBeanMap(Object outer, Class<T> c)
newBeanMap(Class)
, except used for instantiating inner member classes that must be instantiated
within another class instance.T
- The class of the object being wrapped.c
- The name of the class to create a new instance of.outer
- If class is a member class, this is the instance of the containing class.
Should be public final <T> T newBean(Class<T> c) throws BeanRuntimeException
T
- The class type of the bean being created.c
- The class type of the bean being created.BeanRuntimeException
- If the specified class is not a valid bean.public final <T> T newBean(Object outer, Class<T> c) throws BeanRuntimeException
newBean(Class)
, except used for instantiating inner member classes that must be instantiated
within another class instance.T
- The class type of the bean being created.c
- The class type of the bean being created.outer
- If class is a member class, this is the instance of the containing class.
Should be BeanRuntimeException
- If the specified class is not a valid bean.public final <T> BeanMeta<T> getBeanMeta(Class<T> c)
BeanMeta
class for the specified class.T
- The class type to get the meta-data on.c
- The class to get the meta-data on.BeanMeta
for the specified class, or public final <T> ClassMeta<T> getClassMeta(Class<T> c)
ClassMeta
wrapper around a Class
object.T
- The class type being wrapped.c
- The class being wrapped.public final <T> ClassMeta<T> getClassMeta(Type type, Type... args)
ClassMetas
of type Collection
and Map
that have
ClassMeta
values that themselves could be collections or maps.
Collection
meta objects are assumed to be followed by zero or one meta objects indicating the
element type.
Map
meta objects are assumed to be followed by zero or two meta objects indicating the key and value
types.
The array can be arbitrarily long to indicate arbitrarily complex data structures.
getClassMeta(String.class );
- A normal type.
getClassMeta(List.class );
- A list containing objects.
getClassMeta(List.class , String.class );
- A list containing strings.
getClassMeta(LinkedList.class , String.class );
- A linked-list containing
strings.
getClassMeta(LinkedList.class , LinkedList.class , String.class );
-
A linked-list containing linked-lists of strings.
getClassMeta(Map.class );
- A map containing object keys/values.
getClassMeta(Map.class , String.class , String.class );
- A map
containing string keys/values.
getClassMeta(Map.class , String.class , List.class , MyBean.class );
-
A map containing string keys and values of lists containing beans.
type
- The class to resolve.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
public final ClassMeta<Object[]> getArgsClassMeta(Type[] classes)
Type
objects, returns a ClassMeta
representing those arguments.
Constructs a new meta on each call.
classes
- The array of classes to get class metas for.ClassMeta
object corresponding to the classes. Never public final <T> ClassMeta<T> getClassMetaForObject(T o)
getClassMeta(o.getClass())
.T
- The class of the object being passed in.o
- The class to find the class type for.o
is public final String getBeanTypePropertyName(ClassMeta cm)
BeanContext.BEAN_beanTypePropertyName
.cm
- The class meta of the type we're trying to resolve the type name for.
Can be public final BeanRegistry getBeanRegistry()
BeanContext.BEAN_beanDictionary
.public <T> T newInstance(Class<T> c, Object c2)
c
- The class to cast to.c2
- The class to instantiate.
Can also be an instance of the class.RuntimeException
- if constructor could not be found or called.public <T> T newInstance(Class<T> c, Object c2, boolean fuzzyArgs, Object... args)
c
- The class to cast to.c2
- The class to instantiate.
Can also be an instance of the class.fuzzyArgs
- Use fuzzy constructor arg matching.
args
- The arguments to pass to the constructor.RuntimeException
- if constructor could not be found or called.public <T> T newInstanceFromOuter(Object outer, Class<T> c, Object c2, boolean fuzzyArgs, Object... args)
outer
- The outer object.
Can be c
- The class to cast to.c2
- The class to instantiate.
Can also be an instance of the class.fuzzyArgs
- Use fuzzy constructor arg matching.
args
- The arguments to pass to the constructor.RuntimeException
- if constructor could not be found or called.public final StringBuilder getStringBuilder()
StringBuilder
object from an internal pool.
String builders are returned to the pool by calling returnStringBuilder(StringBuilder)
.
public final void returnStringBuilder(StringBuilder sb)
StringBuilder
object back into the internal reuse pool.sb
- The string builder to return to the pool. No-op if public final ClassMeta<Object> object()
ClassMeta
representation for the class Object
.
This ClassMeta
is often used to represent "any object type" when an object type is not known.
This method is identical to calling getClassMeta(Object.
but uses a cached copy to
avoid a hashmap lookup.
ClassMeta
object associated with the Object
class.public final ClassMeta<String> string()
ClassMeta
representation for the class String
.
This ClassMeta
is often used to represent key types in maps.
This method is identical to calling getClassMeta(String.
but uses a cached copy to
avoid a hashmap lookup.
ClassMeta
object associated with the String
class.public final ClassMeta<Class> _class()
ClassMeta
representation for the class Class
.
This ClassMeta
is often used to represent key types in maps.
This method is identical to calling getClassMeta(Class.
but uses a cached copy to
avoid a hashmap lookup.
ClassMeta
object associated with the String
class.public final MediaType getMediaType()
For example,
public void checkForWarnings()
Session
BeanRuntimeException
if any warnings occurred in this session.checkForWarnings
in class Session
Copyright © 2018 Apache. All rights reserved.