public class BeanContext extends Context
This class servers multiple purposes:
Map
interfaces.
BeanFilters
,
PropertyNamers
, etc... which are used to tailor how POJOs are serialized and parsed.
All serializers and parsers extend from this context so that they can handle POJOs using a common framework.
BeanContext.create()
and BeanContextBuilder.build()
methods.
Each bean context maintains a cache of ClassMeta
objects that describe information about classes encountered.
These ClassMeta
objects are time-consuming to construct.
Therefore, instances of BeanContext
that share the same ClassMeta
objects so that the information about
classes only needs to be calculated once.
Because of this, many of the properties defined on the BeanContext
class cannot be overridden on the session.
BeanContext
objects are permanent, unchangeable, cached, and thread-safe,
BeanSession
objects are ephemeral and not thread-safe.
They are meant to be used as quickly-constructed scratchpads for creating bean maps.
BeanMap
objects can only be created through the session.
BeanContexts
have several configuration properties that can be used to tweak behavior on how beans are
handled. These are denoted as the static
Some settings (e.g. BEAN_beansRequireDefaultConstructor
) are used to differentiate between bean
and non-bean classes.
Attempting to create a bean map around one of these objects will throw a BeanRuntimeException
.
The purpose for this behavior is so that the serializers can identify these non-bean classes and convert them to
plain strings using the Object.toString()
method.
Some settings (e.g. BEAN_beanFieldVisibility
) are used to determine what kinds of properties are
detected on beans.
Some settings (e.g. BEAN_beanMapPutReturnsOldValue
) change the runtime behavior of bean maps.
Settings are specified using the BeanContextBuilder.set(String, Object)
method and related convenience
methods.
BeanMaps
are wrappers around Java beans that allow properties to be retrieved and
set using the common Map.put(Object,Object)
and Map.get(Object)
methods.
Bean maps are created in two ways...
BeanSession.toBeanMap()
- Wraps an existing bean inside a Map
wrapper.
BeanSession.newBeanMap()
- Create a new bean instance wrapped in a
Map
wrapper.
Modifier and Type | Field and Description |
---|---|
static String |
BEAN_beanClassVisibility
Configuration property: Minimum bean class visibility.
|
static String |
BEAN_beanConstructorVisibility
Configuration property: Minimum bean constructor visibility.
|
static String |
BEAN_beanDictionary
Configuration property: Bean dictionary.
|
static String |
BEAN_beanDictionary_add
Configuration property: Add to bean dictionary.
|
static String |
BEAN_beanDictionary_remove
Configuration property: Remove from bean dictionary.
|
static String |
BEAN_beanFieldVisibility
Configuration property: Minimum bean field visibility.
|
static String |
BEAN_beanFilters
Configuration property: Bean filters.
|
static String |
BEAN_beanFilters_add
Configuration property: Add to bean filters.
|
static String |
BEAN_beanFilters_remove
Configuration property: Remove from bean filters.
|
static String |
BEAN_beanMapPutReturnsOldValue
Configuration property: BeanMap.put() returns old property value.
|
static String |
BEAN_beanMethodVisibility
Configuration property: Minimum bean method visibility.
|
static String |
BEAN_beansRequireDefaultConstructor
Configuration property: Beans require no-arg constructors.
|
static String |
BEAN_beansRequireSerializable
Configuration property: Beans require Serializable interface.
|
static String |
BEAN_beansRequireSettersForGetters
Configuration property: Beans require setters for getters.
|
static String |
BEAN_beansRequireSomeProperties
Configuration property: Beans require at least one property.
|
static String |
BEAN_beanTypePropertyName
Configuration property: Bean type property name.
|
static String |
BEAN_debug
Configuration property: Debug mode.
|
static String |
BEAN_excludeProperties
Configuration property: Bean property excludes.
|
static String |
BEAN_ignoreInvocationExceptionsOnGetters
Configuration property: Ignore invocation errors on getters.
|
static String |
BEAN_ignoreInvocationExceptionsOnSetters
Configuration property: Ignore invocation errors on setters.
|
static String |
BEAN_ignorePropertiesWithoutSetters
Configuration property: Ignore properties without setters.
|
static String |
BEAN_ignoreUnknownBeanProperties
Configuration property: Ignore unknown properties.
|
static String |
BEAN_ignoreUnknownNullBeanProperties
Configuration property: Ignore unknown properties with null values.
|
static String |
BEAN_implClasses
Configuration property: Implementation classes.
|
static String |
BEAN_includeProperties
Configuration property: Bean property includes.
|
static String |
BEAN_locale
Configuration property: Locale.
|
static String |
BEAN_mediaType
Configuration property: Media type.
|
static String |
BEAN_notBeanClasses
Configuration property: Bean class exclusions.
|
static String |
BEAN_notBeanClasses_add
Configuration property: Add to classes that should not be considered beans.
|
static String |
BEAN_notBeanClasses_remove
Configuration property: Remove from classes that should not be considered beans.
|
static String |
BEAN_notBeanPackages
Configuration property: Bean package exclusions.
|
static String |
BEAN_notBeanPackages_add
Configuration property: Add to packages whose classes should not be considered beans.
|
static String |
BEAN_notBeanPackages_remove
Configuration property: Remove from packages whose classes should not be considered beans.
|
static String |
BEAN_pojoSwaps
Configuration property: POJO swaps.
|
static String |
BEAN_pojoSwaps_add
Configuration property: Add to POJO swap classes.
|
static String |
BEAN_pojoSwaps_remove
Configuration property: Remove from POJO swap classes.
|
static String |
BEAN_propertyNamer
Configuration property: Bean property namer.
|
static String |
BEAN_sortProperties
Configuration property: Sort bean properties.
|
static String |
BEAN_timeZone
Configuration property: TimeZone.
|
static String |
BEAN_useInterfaceProxies
Configuration property: Use interface proxies.
|
static String |
BEAN_useJavaBeanIntrospector
Configuration property: Use Java Introspector.
|
static BeanContext |
DEFAULT
Default config.
|
static BeanContext |
DEFAULT_SORTED
Default config.
|
Constructor and Description |
---|
BeanContext(PropertyStore ps)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected 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.
|
BeanContextBuilder |
builder()
Creates a builder from this context object.
|
static BeanContextBuilder |
create()
Instantiates a new clean-slate
BeanContextBuilder object. |
BeanSession |
createBeanSession()
Same as
createSession() except always returns a BeanSession object unlike createSession()
which is meant to be overridden by subclasses. |
BeanSession |
createBeanSession(BeanSessionArgs args)
Same as
createSession(BeanSessionArgs) except always returns a BeanSession object unlike createSession(BeanSessionArgs)
which is meant to be overridden by subclasses. |
BeanSessionArgs |
createDefaultBeanSessionArgs()
Same as
createDefaultSessionArgs() except always returns a BeanSessionArgs unlike
createDefaultBeanSessionArgs() which is meant to be overridden by subclasses. |
BeanSessionArgs |
createDefaultSessionArgs()
Defines default session arguments used when calling the
Context.createSession() method. |
BeanSession |
createSession()
Create a new bean session based on the properties defined on this context.
|
BeanSession |
createSession(BeanSessionArgs args)
Create a new bean session based on the properties defined on this context combined with the specified
runtime args.
|
Session |
createSession(SessionArgs args)
Create a new session based on the properties defined on this context combined with the specified
runtime args.
|
protected static void |
dumpCacheStats()
Prints meta cache statistics to
System.out . |
<T> BeanMeta<T> |
getBeanMeta(Class<T> c)
Returns the
BeanMeta class for the specified class. |
protected BeanRegistry |
getBeanRegistry()
Returns the bean registry defined in this bean context defined by
BEAN_beanDictionary . |
protected String |
getBeanTypePropertyName()
Returns the type property name as defined by
BEAN_beanTypePropertyName . |
<T> ClassMeta<T> |
getClassMeta(Class<T> type)
Construct 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()) . |
String[] |
getExcludeProperties(Class<?> c)
Returns the
BEAN_excludeProperties setting for the specified class. |
protected <T> Constructor<? extends T> |
getImplClassConstructor(Class<T> c,
Visibility v)
Gets the no-arg constructor for the specified class.
|
String[] |
getIncludeProperties(Class<?> c)
Returns the
BEAN_includeProperties setting for the specified class. |
boolean |
hasSameCache(BeanContext bc)
Returns
|
boolean |
isBean(Object o)
Returns
|
protected boolean |
isNotABean(Class<?> c)
Determines whether the specified class is ignored as a bean class based on the various exclusion parameters
specified on this context class.
|
<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.
|
protected ClassMeta<Object> |
object()
Returns a reusable
ClassMeta representation for the class Object . |
protected <T> ClassMeta<T> |
resolveClassMeta(BeanProperty p,
Type t,
Map<Class<?>,Class<?>[]> typeVarImpls)
Used for determining the class type on a method or field where a
@BeanProperty annotation may be present. |
protected ClassMeta<String> |
string()
Returns a reusable
ClassMeta representation for the class String . |
equals, getArrayProperty, getArrayProperty, getBooleanProperty, getClassArrayProperty, getClassArrayProperty, getClassArrayProperty, getClassListProperty, getClassListProperty, getClassMapProperty, getClassMapProperty, getClassProperty, getClassSetProperty, getClassSetProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, hashCode, toString
public static final String BEAN_beanClassVisibility
String
(Visibility
)
Classes are not considered beans unless they meet the minimum visibility requirements.
For example, if the visibility is PUBLIC
and the bean class is
Use this setting to reduce the visibility requirement.
public static final String BEAN_beanConstructorVisibility
String
(Visibility
)
Only look for constructors with the specified minimum visibility.
This setting affects the logic for finding no-arg constructors for bean.
Normally, only
Use this setting if you want to reduce the visibility requirement.
public static final String BEAN_beanDictionary
List<Class>
The list of classes that make up the bean dictionary in this bean context.
A dictionary is a name/class mapping used to find class types during parsing when they cannot be inferred
through reflection.
The names are defined through the @Bean.typeName()
annotation defined on the bean class.
For example, if a class Foo
has a type-name of
This setting tells the parsers which classes to look for when resolving
Values can consist of any of the following types:
@Bean.typeName()
.
BeanDictionaryList
containing a collection of bean classes with type name annotations.
BeanDictionaryMap
containing a mapping of type names to classes without type name annotations.
public static final String BEAN_beanDictionary_add
public static final String BEAN_beanDictionary_remove
public static final String BEAN_beanFieldVisibility
String
(Visibility
)
Only look for bean fields with the specified minimum visibility.
This affects which fields on a bean class are considered bean properties.
Normally only
Use this setting if you want to reduce the visibility requirement.
public static final String BEAN_beanFilters
List<Class>
This is a programmatic equivalent to the @Bean
annotation.
It's useful when you want to use the @Bean
annotation functionality, but you don't have the ability to alter
the bean classes.
Values can consist of any of the following types:
BeanFilterBuilder
.
InterfaceBeanFilterBuilder
.
public static final String BEAN_beanFilters_add
public static final String BEAN_beanFilters_remove
public static final String BEAN_beanMapPutReturnsOldValue
Boolean
If BeanMap.put()
method will return old property
values.
Otherwise, it returns
Disabled by default because it introduces a slight performance penalty during serialization.
public static final String BEAN_beanMethodVisibility
String
(Visibility
)
Only look for bean methods with the specified minimum visibility.
This affects which methods are detected as getters and setters on a bean class.
Normally only
Use this setting if you want to reduce the visibility requirement.
public static final String BEAN_beansRequireDefaultConstructor
Boolean
If
Otherwise, the bean will be serialized as a string using the Object.toString()
method.
The @Bean
annotation can be used on a class to override this setting when
public static final String BEAN_beansRequireSerializable
Boolean
If Serializable
interface to be considered a bean.
Otherwise, the bean will be serialized as a string using the Object.toString()
method.
The @Bean
annotation can be used on a class to override this setting when
public static final String BEAN_beansRequireSettersForGetters
Boolean
If
Otherwise, they will be ignored.
public static final String BEAN_beansRequireSomeProperties
Boolean
If
Otherwise, the bean will be serialized as a string using the Object.toString()
method.
The @Bean
annotation can be used on a class to override this setting when
public static final String BEAN_beanTypePropertyName
String
This specifies the name of the bean property used to store the dictionary name of a bean type so that the parser knows the data type to reconstruct.
public static final String BEAN_debug
Boolean
Enables the following additional information during serialization:
Serializer.SERIALIZER_detectRecursions
.
Enables the following additional information during parsing:
public static final String BEAN_excludeProperties
Map<String,String>
{}
Specifies to exclude the specified list of properties for the specified bean class.
The keys are either fully-qualified or simple class names, and the values are comma-delimited lists of property
names.
The key
For example, {Bean1:
means don't serialize the foo
and
bar
properties on any beans whose simple class name is Bean1
.
Setting applies to specified class and all subclasses.
public static final String BEAN_ignoreInvocationExceptionsOnGetters
Boolean
If
Otherwise, a BeanRuntimeException
is thrown.
public static final String BEAN_ignoreInvocationExceptionsOnSetters
Boolean
If
Otherwise, a BeanRuntimeException
is thrown.
public static final String BEAN_ignorePropertiesWithoutSetters
Boolean
If
Otherwise, a RuntimeException
is thrown.
public static final String BEAN_ignoreUnknownBeanProperties
Boolean
If
Otherwise, a RuntimeException
is thrown.
public static final String BEAN_ignoreUnknownNullBeanProperties
Boolean
If
Otherwise, a RuntimeException
is thrown.
public static final String BEAN_implClasses
Map<String,Class>
For interfaces and abstract classes this method can be used to specify an implementation class for the interface/abstract class so that instances of the implementation class are used when instantiated (e.g. during a parse).
public static final String BEAN_includeProperties
Map<String,String>
{}
Specifies the set and order of names of properties associated with the bean class.
The keys are either fully-qualified or simple class names, and the values are comma-delimited lists of property
names.
The key
For example, {Bean1:
means only serialize the foo
and
bar
properties on the specified bean.
Setting applies to specified class and all subclasses.
public static final String BEAN_locale
String
(Locale
)
Locale.getDefault()
)
public static final String BEAN_mediaType
String
(MediaType
)
Specifies a default media type value for serializer and parser sessions.
public static final String BEAN_notBeanClasses
Set<Class>
List of classes that should not be treated as beans even if they appear to be bean-like.
Not-bean classes are converted to Strings
during serialization.
Values can consist of any of the following types:
public static final String BEAN_notBeanClasses_add
public static final String BEAN_notBeanClasses_remove
public static final String BEAN_notBeanPackages
Set<String>
java.lang
java.lang.annotation
java.lang.ref
java.lang.reflect
java.io
java.net
java.nio.*
java.util.*
When specified, the current list of ignore packages are appended to.
Any classes within these packages will be serialized to strings using Object.toString()
.
Note that you can specify suffix patterns to include all subpackages.
Values can consist of any of the following types:
public static final String BEAN_notBeanPackages_add
public static final String BEAN_notBeanPackages_remove
public static final String BEAN_pojoSwaps
List<Class>
POJO swaps are used to "swap out" non-serializable classes with serializable equivalents during serialization, and "swap in" the non-serializable class during parsing.
An example of a POJO swap would be a Calendar
object that gets swapped out for an ISO8601 string.
Multiple POJO swaps can be associated with a single class.
When multiple swaps are applicable to the same class, the media type pattern defined by
PojoSwap.forMediaTypes()
or @Swap.mediaTypes()
are used to come up with the best match.
Values can consist of any of the following types:
PojoSwap
.
SurrogateSwap
.
public static final String BEAN_pojoSwaps_add
public static final String BEAN_pojoSwaps_remove
public static final String BEAN_propertyNamer
Class<? implements PropertyNamer
>
PropertyNamerDefault
The class to use for calculating bean property names.
Predefined classes:
PropertyNamerDefault
- Default.
PropertyNamerDLC
- Dashed-lower-case names.
PropertyNamerULC
- Dashed-upper-case names.
public static final String BEAN_sortProperties
Boolean
When
Otherwise, the natural order of the bean properties is used which is dependent on the JVM vendor.
On IBM JVMs, the bean properties are ordered based on their ordering in the Java file.
On Oracle JVMs, the bean properties are not ordered (which follows the official JVM specs).
This property is disabled by default so that IBM JVM users don't have to use @Bean
annotations
to force bean properties to be in a particular order and can just alter the order of the fields/methods
in the Java file.
public static final String BEAN_timeZone
String
(TimeZone
)
public static final String BEAN_useInterfaceProxies
Boolean
If InvocationHandler
if there is no other way of instantiating them.
Otherwise, throws a BeanRuntimeException
.
public static final String BEAN_useJavaBeanIntrospector
Boolean
Using the built-in Java bean introspector will not pick up fields or non-standard getters/setters.
Most @Bean
annotations will be ignored.
public static final BeanContext DEFAULT
public static final BeanContext DEFAULT_SORTED
public BeanContext(PropertyStore ps)
Typically only called from ContextBuilder.build(Class)
method.
ps
- The property store containing the unmodifiable configuration for this bean context.public BeanContextBuilder builder()
Context
Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
public static BeanContextBuilder create()
BeanContextBuilder
object.
This is equivalent to simply calling
.
JsonSerializerBuilder
object.public BeanSession createSession()
Use this method for creating sessions if you don't need to override any properties or locale/timezone currently set on this context.
createSession
in class Context
public BeanSession createSession(BeanSessionArgs args)
Use this method for creating sessions if you don't need to override any properties or locale/timezone currently set on this context.
args
- The session arguments.public final Session createSession(SessionArgs args)
Context
Use this method for creating sessions if you don't need to override any properties or locale/timezone currently set on this context.
createSession
in class Context
args
- The session arguments.public final BeanSession createBeanSession(BeanSessionArgs args)
createSession(BeanSessionArgs)
except always returns a BeanSession
object unlike createSession(BeanSessionArgs)
which is meant to be overridden by subclasses.args
- The session arguments.public final BeanSession createBeanSession()
createSession()
except always returns a BeanSession
object unlike createSession()
which is meant to be overridden by subclasses.public BeanSessionArgs createDefaultSessionArgs()
Context
Context.createSession()
method.createDefaultSessionArgs
in class Context
public final BeanSessionArgs createDefaultBeanSessionArgs()
createDefaultSessionArgs()
except always returns a BeanSessionArgs
unlike
createDefaultBeanSessionArgs()
which is meant to be overridden by subclasses.public final boolean hasSameCache(BeanContext bc)
Useful for testing purposes.
bc
- The bean context to compare to.protected final boolean isNotABean(Class<?> c)
c
- The class type being tested.public boolean isBean(Object o)
o
- The object to test.protected static void dumpCacheStats()
System.out
.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> type)
ClassMeta
wrapper around a Class
object.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 <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 protected final <T> ClassMeta<T> resolveClassMeta(BeanProperty p, Type t, Map<Class<?>,Class<?>[]> typeVarImpls)
@BeanProperty
annotation may be present.T
- The class type we're wrapping.p
- The property annotation on the type if there is one.t
- The type.typeVarImpls
- Contains known resolved type parameters on the specified class so that we can result
ParameterizedTypes
and TypeVariables
.
Can be ClassMeta
object wrapped around the Type
object.protected final String getBeanTypePropertyName()
BEAN_beanTypePropertyName
.protected final BeanRegistry getBeanRegistry()
BEAN_beanDictionary
.protected final <T> Constructor<? extends T> getImplClassConstructor(Class<T> c, Visibility v)
T
- The class to check.c
- The class to check.v
- The minimum visibility for the constructor.public String[] getIncludeProperties(Class<?> c)
BEAN_includeProperties
setting for the specified class.c
- The class.public String[] getExcludeProperties(Class<?> c)
BEAN_excludeProperties
setting for the specified class.c
- The class.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.protected 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.protected 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.protected 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.Copyright © 2018 Apache. All rights reserved.