All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.BeanContext Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau;

import static org.apache.juneau.Visibility.*;
import static org.apache.juneau.internal.ClassUtils.*;
import static org.apache.juneau.internal.CollectionUtils.*;
import static org.apache.juneau.internal.StringUtils.*;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.*;

import org.apache.juneau.annotation.*;
import org.apache.juneau.http.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.json.*;
import org.apache.juneau.serializer.*;
import org.apache.juneau.transform.*;

/**
 * Core class of the Juneau architecture.
 *
 * 

* This class servers multiple purposes: *

    *
  • * Provides the ability to wrap beans inside {@link Map} interfaces. *
  • * Serves as a repository for metadata on POJOs, such as associated {@link BeanFilter BeanFilters}, * {@link PropertyNamer 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. * *

Bean Contexts
* * Bean contexts are created through the {@link BeanContext#create() BeanContext.create()} and {@link BeanContextBuilder#build()} methods. *
These context objects are read-only, reusable, and thread-safe. * *

* Each bean context maintains a cache of {@link ClassMeta} objects that describe information about classes encountered. * These ClassMeta objects are time-consuming to construct. * Therefore, instances of {@link BeanContext} that share the same "BeanContext.*" property values share * the same cache. This allows for efficient reuse of ClassMeta objects so that the information about * classes only needs to be calculated once. * Because of this, many of the properties defined on the {@link BeanContext} class cannot be overridden on the session. * *

Bean Sessions
* * Whereas BeanContext objects are permanent, unchangeable, cached, and thread-safe, * {@link BeanSession} objects are ephemeral and not thread-safe. * They are meant to be used as quickly-constructed scratchpads for creating bean maps. * {@link BeanMap} objects can only be created through the session. * *
BeanContext configuration properties
* * BeanContexts have several configuration properties that can be used to tweak behavior on how beans are * handled. These are denoted as the static BEAN_* fields on this class. * *

* Some settings (e.g. {@link #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 {@link 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 {@link Object#toString()} method. * *

* Some settings (e.g. {@link #BEAN_beanFieldVisibility}) are used to determine what kinds of properties are * detected on beans. * *

* Some settings (e.g. {@link #BEAN_beanMapPutReturnsOldValue}) change the runtime behavior of bean maps. * *

* Settings are specified using the {@link BeanContextBuilder#set(String, Object)} method and related convenience * methods. * *

Example:
* *

* // Construct a context from scratch. * BeanContext beanContext = BeanContext * .create() * .set(BeanContext.BEAN_beansRequireDefaultConstructor, true) * .notBeanClasses(Foo.class) * .build(); *

* *
Bean Maps
* * {@link BeanMap BeanMaps} are wrappers around Java beans that allow properties to be retrieved and * set using the common {@link Map#put(Object,Object)} and {@link Map#get(Object)} methods. * *

* Bean maps are created in two ways... *

    *
  1. {@link BeanSession#toBeanMap(Object) BeanSession.toBeanMap()} - Wraps an existing bean inside a {@code Map} * wrapper. *
  2. {@link BeanSession#newBeanMap(Class) BeanSession.newBeanMap()} - Create a new bean instance wrapped in a * {@code Map} wrapper. *
* *
Example:
* *

* // A sample bean class * public class Person { * public String getName(); * public void setName(String name); * public int getAge(); * public void setAge(int age); * } * * // Create a new bean session * BeanSession session = BeanContext.DEFAULT.createSession(); * * // Wrap an existing bean in a new bean map * BeanMap<Person> m1 = session.toBeanMap(new Person()); * m1.put("name", "John Smith"); * m1.put("age", 45); * * // Create a new bean instance wrapped in a new bean map * BeanMap<Person> m2 = session.newBeanMap(Person.class); * m2.put("name", "John Smith"); * m2.put("age", 45); * Person p = m2.getBean(); // Get the bean instance that was created. *

* *
See Also:
*
    *
*/ @SuppressWarnings({"unchecked","rawtypes"}) public class BeanContext extends Context { static final String PREFIX = "BeanContext."; /** * Configuration property: Minimum bean class visibility. * *
Property:
*
    *
  • Name: "BeanContext.beanClassVisibility.s" *
  • Data type: String ({@link Visibility}) *
  • Default: "PUBLIC" *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beanClassVisibility(Visibility)} *
    *
* *
Description:
*

* Classes are not considered beans unless they meet the minimum visibility requirements. * *

* For example, if the visibility is PUBLIC and the bean class is protected, then the class * will not be interpreted as a bean class and be serialized as a string. *
Use this setting to reduce the visibility requirement. * *

Example:
*

* // Create a serializer that serializes protected classes. * WriterSerializer s = JsonSerializer * .create() * .beanClassVisibility(PROTECTED) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beanClassVisibility, "PROTECTED") * .build(); *

*/ public static final String BEAN_beanClassVisibility = PREFIX + "beanClassVisibility.s"; /** * Configuration property: Minimum bean constructor visibility. * *
Property:
*
    *
  • Name: "BeanContext.beanConstructorVisibility.s" *
  • Data type: String ({@link Visibility}) *
  • Default: "PUBLIC" *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beanConstructorVisibility(Visibility)} *
    *
* *
Description:
*

* Only look for constructors with the specified minimum visibility. * *

* This setting affects the logic for finding no-arg constructors for bean. *
Normally, only public no-arg constructors are used. *
Use this setting if you want to reduce the visibility requirement. * *

Example:
*

* // Create a serializer that looks for protected no-arg constructors. * WriterSerializer s = JsonSerializer * .create() * .beanConstructorVisibility(PROTECTED) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beanConstructorVisibility, "PROTECTED") * .build(); *

*/ public static final String BEAN_beanConstructorVisibility = PREFIX + "beanConstructorVisibility.s"; /** * Configuration property: Bean dictionary. * *
Property:
*
    *
  • Name: "BeanContext.beanDictionary.lc" *
  • Data type: List<Class> *
  • Default: empty list *
  • Session property: false *
  • Annotations: *
      *
    • {@link Bean#beanDictionary()} *
    • {@link BeanProperty#beanDictionary()} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#beanDictionary(Object...)} *
    • {@link BeanContextBuilder#beanDictionary(Class...)} *
    • {@link BeanContextBuilder#beanDictionary(boolean,Object...)} *
    • {@link BeanContextBuilder#beanDictionaryRemove(Object...)} *
    • {@link BeanFilterBuilder#beanDictionary(Class...)} *
    *
* *
Description:
*

* 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 {@link Bean#typeName() @Bean(typeName)} annotation defined on the bean class. *
For example, if a class Foo has a type-name of "myfoo", then it would end up serialized * as "{_type:'myfoo',...}". * *

* This setting tells the parsers which classes to look for when resolving "_type" attributes. * *

* Values can consist of any of the following types: *

    *
  • Any bean class that specifies a value for {@link Bean#typeName() @Bean(typeName)}. *
  • Any subclass of {@link BeanDictionaryList} containing a collection of bean classes with type name annotations. *
  • Any subclass of {@link BeanDictionaryMap} containing a mapping of type names to classes without type name annotations. *
  • Any array or collection of the objects above. *
* *
Example:
*

* // Create a parser and tell it which classes to try to resolve. * ReaderParser p = JsonParser * .create() * .beanDictionary(Foo.class, Bar.class) * .build(); * * // Same, but use property. * ReaderParser p = JsonParser * .create() * .addTo(BEAN_beanDictionary, Foo.class) * .addTo(BEAN_beanDictionary, Bar.class) * .build(); * * // Instead of by parser, define a bean dictionary on a class through an annotation. * // This applies to all properties on this class and all subclasses. * @Bean(beanDictionary={Foo.class,Bar.class}) * public class MyBean {...} * * // Use the predefined HTML5 bean dictionary which is a BeanDictionaryList. * ReaderParser p = HtmlParser * .create() * .beanDictionary(HtmlBeanDictionary.class) * .build(); *

* *
See Also:
*
    *
*/ public static final String BEAN_beanDictionary = PREFIX + "beanDictionary.lc"; /** * Configuration property: Add to bean dictionary. */ public static final String BEAN_beanDictionary_add = PREFIX + "beanDictionary.lc/add"; /** * Configuration property: Remove from bean dictionary. */ public static final String BEAN_beanDictionary_remove = PREFIX + "beanDictionary.lc/remove"; /** * Configuration property: Minimum bean field visibility. * *
Property:
*
    *
  • Name: "BeanContext.beanFieldVisibility.s" *
  • Data type: String ({@link Visibility}) *
  • Default: "PUBLIC" *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beanFieldVisibility(Visibility)} *
    *
* *
Description:
*

* Only look for bean fields with the specified minimum visibility. * *

* This affects which fields on a bean class are considered bean properties. *
Normally only public fields are considered. *
Use this setting if you want to reduce the visibility requirement. * *

Example:
*

* // Create a serializer that looks for protected fields. * WriterSerializer s = JsonSerializer * .create() * .beanFieldVisibility(PROTECTED) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beanFieldVisibility, "PROTECTED") * .build(); * * // Disable using fields as properties entirely. * WriterSerializer s = JsonSerializer * .create() * .beanFieldVisibility(NONE) * .build(); *

*/ public static final String BEAN_beanFieldVisibility = PREFIX + "beanFieldVisibility.s"; /** * Configuration property: Bean filters. * *
Property:
*
    *
  • Name: "BeanContext.beanFilters.lc" *
  • Data type: List<Class> *
  • Default: empty list *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beanFilters(Object...)} *
    • {@link BeanContextBuilder#beanFilters(Class...)} *
    • {@link BeanContextBuilder#beanFilters(boolean,Object...)} *
    • {@link BeanContextBuilder#beanFiltersRemove(Object...)} *
    *
* *
Description:
*

* This is a programmatic equivalent to the {@link Bean @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: *

    *
  • Any subclass of {@link BeanFilterBuilder}. *
    These must have a public no-arg constructor. *
  • Any bean interfaces. *
    A shortcut for defining a {@link InterfaceBeanFilterBuilder}. *
    Any subclasses of an interface class will only have properties defined on the interface. * All other bean properties will be ignored. *
  • Any array or collection of the objects above. *
* *
Example:
*

* // Create a bean filter for the MyBean class. * public class MyBeanFilter extends BeanFilterBuilder<MyBean> { * * // Must provide a no-arg constructor! * public MyBeanFilter() { * includeProperties("foo,bar,baz"); // The properties we want exposed. * } * } * * // Associate our bean filter with a serializer. * WriterSerializer s = JsonSerializer * .create() * .beanFilters(MyBeanFilter.class) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_beanFilters, MyBeanFilter.class) * .build(); *

* *
See Also:
*
    *
*/ public static final String BEAN_beanFilters = PREFIX + "beanFilters.lc"; /** * Configuration property: Add to bean filters. */ public static final String BEAN_beanFilters_add = PREFIX + "beanFilters.lc/add"; /** * Configuration property: Remove from bean filters. */ public static final String BEAN_beanFilters_remove = PREFIX + "beanFilters.lc/remove"; /** * Configuration property: BeanMap.put() returns old property value. * *
Property:
*
    *
  • Name: "BeanContext.beanMapPutReturnsOldValue.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beanMapPutReturnsOldValue(boolean)} *
    • {@link BeanContextBuilder#beanMapPutReturnsOldValue()} *
    *
* *
Description:
*

* If true, then the {@link BeanMap#put(String,Object) BeanMap.put()} method will return old property * values. *
Otherwise, it returns null. * *

* Disabled by default because it introduces a slight performance penalty during serialization. * *

Example:
*

* // Create a serializer that creates BeanMaps with normal put() behavior. * WriterSerializer s = JsonSerializer * .create() * .beanMapPutReturnsOldValue() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beanMapPutReturnsOldValue, true) * .build(); * * BeanMap<MyBean> bm = s.createSession().toBeanMap(new MyBean()); * bm.put("foo", "bar"); * Object oldValue = bm.put("foo", "baz"); // oldValue == "bar" *

*/ public static final String BEAN_beanMapPutReturnsOldValue = PREFIX + "beanMapPutReturnsOldValue.b"; /** * Configuration property: Minimum bean method visibility. * *
Property:
*
    *
  • Name: "BeanContext.beanMethodVisibility.s" *
  • Data type: String ({@link Visibility}) *
  • Default: "PUBLIC" *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beanMethodVisibility(Visibility)} *
    *
* *
Description:
*

* 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 public getters and setters are considered. *
Use this setting if you want to reduce the visibility requirement. * *

Example:
*

* // Create a serializer that looks for protected getters and setters. * WriterSerializer s = JsonSerializer * .create() * .beanMethodVisibility(PROTECTED) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beanMethodVisibility, "PROTECTED") * .build(); *

*/ public static final String BEAN_beanMethodVisibility = PREFIX + "beanMethodVisibility.s"; /** * Configuration property: Beans require no-arg constructors. * *
Property:
*
    *
  • Name: "BeanContext.beansRequireDefaultConstructor.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beansRequireDefaultConstructor(boolean)} *
    • {@link BeanContextBuilder#beansRequireDefaultConstructor()} *
    *
* *
Description:
*

* If true, a Java class must implement a default no-arg constructor to be considered a bean. *
Otherwise, the bean will be serialized as a string using the {@link Object#toString()} method. * *

* The {@link Bean @Bean} annotation can be used on a class to override this setting when true. * *

Example:
*

* // Create a serializer that ignores beans without default constructors. * WriterSerializer s = JsonSerializer * .create() * .beansRequireDefaultConstructor() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beansRequireDefaultConstructor, true) * .build(); *

*/ public static final String BEAN_beansRequireDefaultConstructor = PREFIX + "beansRequireDefaultConstructor.b"; /** * Configuration property: Beans require Serializable interface. * *
Property:
*
    *
  • Name: "BeanContext.beansRequireSerializable.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beansRequireSerializable(boolean)} *
    • {@link BeanContextBuilder#beansRequireSerializable()} *
    *
* *
Description:
*

* If true, a Java class must implement the {@link Serializable} interface to be considered a bean. *
Otherwise, the bean will be serialized as a string using the {@link Object#toString()} method. * *

* The {@link Bean @Bean} annotation can be used on a class to override this setting when true. * *

Example:
*

* // Create a serializer that ignores beans not implementing Serializable. * WriterSerializer s = JsonSerializer * .create() * .beansRequireSerializable() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beansRequireSerializable, true) * .build(); *

*/ public static final String BEAN_beansRequireSerializable = PREFIX + "beansRequireSerializable.b"; /** * Configuration property: Beans require setters for getters. * *
Property:
*
    *
  • Name: "BeanContext.beansRequireSettersForGetters.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beansRequireSettersForGetters(boolean)} *
    • {@link BeanContextBuilder#beansRequireSettersForGetters()} *
    *
* *
Description:
*

* If true, only getters that have equivalent setters will be considered as properties on a bean. *
Otherwise, they will be ignored. * *

Example:
*

* // Create a serializer that ignores bean properties without setters. * WriterSerializer s = JsonSerializer * .create() * .beansRequireSettersForGetter() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beansRequireSettersForGetters, true) * .build(); *

*/ public static final String BEAN_beansRequireSettersForGetters = PREFIX + "beansRequireSettersForGetters.b"; /** * Configuration property: Beans require at least one property. * *
Property:
*
    *
  • Name: "BeanContext.beansRequireSomeProperties.b" *
  • Data type: Boolean *
  • Default: true *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#beansRequireSomeProperties(boolean)} *
    *
* *
Description:
*

* If true, then a Java class must contain at least 1 property to be considered a bean. *
Otherwise, the bean will be serialized as a string using the {@link Object#toString()} method. * *

* The {@link Bean @Bean} annotation can be used on a class to override this setting when true. * *

Example:
*

* // Create a serializer that serializes beans even if they have zero properties. * WriterSerializer s = JsonSerializer * .create() * .beansRequireSomeProperties(false) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beansRequireSomeProperties, false) * .build(); *

*/ public static final String BEAN_beansRequireSomeProperties = PREFIX + "beansRequireSomeProperties.b"; /** * Configuration property: Bean type property name. * *
Property:
*
    *
  • Name: "BeanContext.beanTypePropertyName.s" *
  • Data type: String *
  • Default: "_type" *
  • Session property: false *
  • Annotations: *
      *
    • {@link Bean#typePropertyName()} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#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. * *

Example:
*

* // Create a serializer that uses 'type' instead of '_type' for dictionary names. * WriterSerializer s = JsonSerializer * .create() * .beanTypePropertyName("type") * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_beanTypePropertyName, "type") * .build(); *

* *
See Also:
*
    *
*/ public static final String BEAN_beanTypePropertyName = PREFIX + "beanTypePropertyName.s"; /** * Configuration property: Debug mode. * *
Property:
*
    *
  • Name: "BeanContext.debug.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: true *
  • Methods: *
      *
    • {@link BeanContextBuilder#debug(boolean)} *
    • {@link BeanContextBuilder#debug()} *
    • {@link BeanSessionArgs#debug(Boolean)} *
    *
* *
Description:
*

* Enables the following additional information during serialization: *

    *
  • * When bean getters throws exceptions, the exception includes the object stack information * in order to determine how that method was invoked. *
  • * Enables {@link Serializer#BEANTRAVERSE_detectRecursions}. *
* *

* Enables the following additional information during parsing: *

    *
  • * When bean setters throws exceptions, the exception includes the object stack information * in order to determine how that method was invoked. *
* *
Example:
*

* // Create a serializer with debug enabled. * WriterSerializer s = JsonSerializer * .create() * .debug() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_debug, true) * .build(); *

*/ public static final String BEAN_debug = PREFIX + "debug.b"; /** * Configuration property: POJO examples. * *
Property:
*
    *
  • Name: "BeanContext.examples.smo" *
  • Data type: Map<String,Object> *
  • Default: {} *
  • Session property: false *
  • Annotations: *
      *
    • {@link Example} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#example(Class,Object)} *
    *
* *
Description:
*

* Specifies an example of the specified class. * *

* Examples are used in cases such as POJO examples in Swagger documents. * *

* Setting applies to specified class and all subclasses. * *

Example:
*

* // Create a serializer that excludes the 'foo' and 'bar' properties on the MyBean class. * WriterSerializer s = JsonSerializer * .create() * .example(MyBean.class, new MyBean().foo("foo").bar(123)) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_examples, MyBean.class.getName(), new MyBean().foo("foo").bar(123)) * .build(); *

* *

* POJO examples can also be defined on classes via the following: *

    *
  • A static field annotated with {@link Example @Example}. *
  • A static method annotated with {@link Example @Example} with zero arguments or one {@link BeanSession} argument. *
  • A static method with name example with no arguments or one {@link BeanSession} argument. *
*/ public static final String BEAN_examples = PREFIX + "examples.smo"; /** * Configuration property: Bean property excludes. * *
Property:
*
    *
  • Name: "BeanContext.excludeProperties.sms" *
  • Data type: Map<String,String> *
  • Default: {} *
  • Session property: false *
  • Annotations: *
      *
    • {@link Bean#excludeProperties()} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#excludeProperties(Class, String)} *
    • {@link BeanContextBuilder#excludeProperties(String, String)} *
    • {@link BeanContextBuilder#excludeProperties(Map)} *
    • {@link BeanFilterBuilder#excludeProperties(String...)} *
    *
* *
Description:
*

* 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 "*" means all bean classes. * *

* For example, {Bean1:'foo,bar'} 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. * *

Example:
*

* // Create a serializer that excludes the 'foo' and 'bar' properties on the MyBean class. * WriterSerializer s = JsonSerializer * .create() * .excludeProperties(MyBean.class, "foo,bar") * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_excludeProperties, MyBean.class.getName(), "foo,bar") * .build(); * * // Alternate using JSON object. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_excludeProperties, "{'org.apache.MyBean':'foo,bar'}") * .build(); *

*/ public static final String BEAN_excludeProperties = PREFIX + "excludeProperties.sms"; /** * Configuration property: Find fluent setters. * *
Property:
*
    *
  • Name: "BeanContext.fluentSetters.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Annotations: *
      *
    • {@link Bean#fluentSetters()} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#fluentSetters(boolean)} *
    • {@link BeanContextBuilder#fluentSetters()} *
    • {@link BeanFilterBuilder#fluentSetters(boolean)} *
    • {@link BeanFilterBuilder#fluentSetters()} *
    *
* *
Description:
*

* When enabled, fluent setters are detected on beans. * *

* Fluent setters must have the following attributes: *

    *
  • Public. *
  • Not static. *
  • Take in one parameter. *
  • Return the bean itself. *
* *
Example:
*

* // Create a serializer that finds fluent setters. * WriterSerializer s = JsonSerializer * .create() * .fluentSetters() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_fluentSetters, true) * .build(); *

*/ public static final String BEAN_fluentSetters = PREFIX + "fluentSetters.b"; /** * Configuration property: Ignore invocation errors on getters. * *
Property:
*
    *
  • Name: "BeanContext.ignoreInvocationExceptionsOnGetters.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#ignoreInvocationExceptionsOnGetters(boolean)} *
    • {@link BeanContextBuilder#ignoreInvocationExceptionsOnGetters()} *
    *
* *
Description:
*

* If true, errors thrown when calling bean getter methods will silently be ignored. *
Otherwise, a {@code BeanRuntimeException} is thrown. * *

Example:
*

* // Create a serializer that ignores bean getter exceptions. * WriterSerializer s = JsonSerializer * .create() * .ingoreInvocationExceptionsOnGetters() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_ignoreInvocationExceptionsOnGetters, true) * .build(); *

*/ public static final String BEAN_ignoreInvocationExceptionsOnGetters = PREFIX + "ignoreInvocationExceptionsOnGetters.b"; /** * Configuration property: Ignore invocation errors on setters. * *
Property:
*
    *
  • Name: "BeanContext.ignoreInvocationExceptionsOnSetters.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#ignoreInvocationExceptionsOnSetters(boolean)} *
    • {@link BeanContextBuilder#ignoreInvocationExceptionsOnSetters()} *
    *
* *
Description:
*

* If true, errors thrown when calling bean setter methods will silently be ignored. *
Otherwise, a {@code BeanRuntimeException} is thrown. * *

Example:
*

* // Create a parser that ignores bean setter exceptions. * ReaderParser p = JsonParser * .create() * .ignoreInvocationExceptionsOnSetters() * .build(); * * // Same, but use property. * ReaderParser p = JsonParser * .create() * .set(BEAN_ignoreInvocationExceptionsOnSetters, true) * .build(); *

*/ public static final String BEAN_ignoreInvocationExceptionsOnSetters = PREFIX + "ignoreInvocationExceptionsOnSetters.b"; /** * Configuration property: Ignore properties without setters. * *
Property:
*
    *
  • Name: "BeanContext.ignorePropertiesWithoutSetters.b" *
  • Data type: Boolean *
  • Default: true *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#ignorePropertiesWithoutSetters(boolean)} *
    *
* *
Description:
*

* If true, trying to set a value on a bean property without a setter will silently be ignored. *
Otherwise, a {@code RuntimeException} is thrown. * *

Example:
*

* // Create a parser that throws an exception if a setter is not found but a getter is. * ReaderParser p = JsonParser * .create() * .ignorePropertiesWithoutSetters(false) * .build(); * * // Same, but use property. * ReaderParser p = JsonParser * .create() * .set(BEAN_ignorePropertiesWithoutSetters, false) * .build(); *

*/ public static final String BEAN_ignorePropertiesWithoutSetters = PREFIX + "ignorePropertiesWithoutSetters.b"; /** * Configuration property: Ignore unknown properties. * *
Property:
*
    *
  • Name: "BeanContext.ignoreUnknownBeanProperties.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#ignoreUnknownBeanProperties(boolean)} *
    • {@link BeanContextBuilder#ignoreUnknownBeanProperties()} *
    *
* *
Description:
*

* If true, trying to set a value on a non-existent bean property will silently be ignored. *
Otherwise, a {@code RuntimeException} is thrown. * *

Example:
*

* // Create a parser that ignores missing bean properties. * ReaderParser p = JsonParser * .create() * .ignoreUnknownBeanProperties() * .build(); * * // Same, but use property. * ReaderParser p = JsonParser * .create() * .set(BEAN_ignoreUnknownBeanProperties, true) * .build(); *

*/ public static final String BEAN_ignoreUnknownBeanProperties = PREFIX + "ignoreUnknownBeanProperties.b"; /** * Configuration property: Ignore unknown properties with null values. * *
Property:
*
    *
  • Name: "BeanContext.ignoreUnknownNullBeanProperties.b" *
  • Data type: Boolean *
  • Default: true *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#ignoreUnknownNullBeanProperties(boolean)} *
    *
* *
Description:
*

* If true, trying to set a null value on a non-existent bean property will silently be ignored. *
Otherwise, a {@code RuntimeException} is thrown. * *

Example:
*

* // Create a parser that throws an exception on an unknown property even if the value being set is null. * ReaderParser p = JsonParser * .create() * .ignoreUnknownNullBeanProperties(false) * .build(); * * // Same, but use property. * ReaderParser p = JsonParser * .create() * .set(BEAN_ignoreUnknownNullBeanProperties, false) * .build(); *

*/ public static final String BEAN_ignoreUnknownNullBeanProperties = PREFIX + "ignoreUnknownNullBeanProperties.b"; /** * Configuration property: Implementation classes. * *
Property:
*
    *
  • Name: "BeanContext.implClasses.smc" *
  • Data type: Map<String,Class> *
  • Default: empty map *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#implClasses(Map)} *
    • {@link BeanContextBuilder#implClass(Class, Class)} *
    *
* *
Description:
*

* 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). * *

Example:
*

* // Create a parser that instantiates MyBeanImpls when parsing MyBeanInterfaces. * ReaderParser p = JsonParser * .create() * .implClass(MyBeanInterface.class, MyBeanImpl.class) * .build(); * * // Same, but use property. * ReaderParser p = JsonParser * .create() * .addTo(BEAN_implClasses, MyBeanInterface.class.getName(), MyBeanImpl.class) * .build(); *

*/ public static final String BEAN_implClasses = PREFIX + "implClasses.smc"; /** * Configuration property: Bean property includes. * *
Property:
*
    *
  • Name: "BeanContext.properties.sms" *
  • Data type: Map<String,String> *
  • Default: {} *
  • Session property: false *
  • Annotations: *
      *
    • {@link Bean#properties()} *
    • {@link BeanProperty#properties()} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#includeProperties(Class, String)} *
    • {@link BeanContextBuilder#includeProperties(String, String)} *
    • {@link BeanContextBuilder#includeProperties(Map)} *
    • {@link BeanFilterBuilder#properties(String...)} *
    *
* *
Description:
*

* 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 "*" means all bean classes. * *

* For example, {Bean1:'foo,bar'} means only serialize the foo and * bar properties on the specified bean. * *

* Setting applies to specified class and all subclasses. * *

Example:
*

* // Create a serializer that includes only the 'foo' and 'bar' properties on the MyBean class. * WriterSerializer s = JsonSerializer * .create() * .includeProperties(MyBean.class, "foo,bar") * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_includeProperties, MyBean.class.getName(), "foo,bar") * .build(); * * // Alternate using JSON object. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_includeProperties, "{'org.apache.MyBean':'foo,bar'}") * .build(); *

*/ public static final String BEAN_includeProperties = PREFIX + "includeProperties.sms"; /** * Configuration property: Locale. * *
Property:
*
    *
  • Name: "BeanContext.locale.s" *
  • Data type: String ({@link Locale}) *
  • Default: null (defaults to {@link Locale#getDefault()}) *
  • Session property: true *
  • Methods: *
      *
    • {@link BeanContextBuilder#locale(Locale)} *
    • {@link BeanSessionArgs#locale(Locale)} *
    *
* *
Description:
*

* Specifies the default locale for serializer and parser sessions. * *

Example:
*

* // Create a serializer that uses the specified locale if it's not passed in through session args. * WriterSerializer s = JsonSerializer * .create() * .locale(Locale.UK) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_locale, Locale.UK) * .build(); * * // Define on session-args instead. * SerializerSessionArgs sessionArgs = new SerializerSessionArgs().locale(Locale.UK); * try (WriterSerializerSession session = s.createSession(sessionArgs)) { * ... * } *

*/ public static final String BEAN_locale = PREFIX + "locale.s"; /** * Configuration property: Media type. * *
Property:
*
    *
  • Name: "BeanContext.mediaType.s" *
  • Data type: String ({@link MediaType}) *
  • Default: null *
  • Session property: true *
  • Methods: *
      *
    • {@link BeanContextBuilder#mediaType(MediaType)} *
    • {@link BeanSessionArgs#mediaType(MediaType)} *
    *
* *
Description:
*

* Specifies the default media type value for serializer and parser sessions. * *

Example:
*

* // Create a serializer that uses the specified media type if it's not passed in through session args. * WriterSerializer s = JsonSerializer * .create() * .mediaType(MediaType.JSON) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_mediaType, MediaType.JSON) * .build(); * * // Define on session-args instead. * SerializerSessionArgs sessionArgs = new SerializerSessionArgs().mediaType(MediaType.JSON); * try (WriterSerializerSession session = s.createSession(sessionArgs)) { * ... * } *

*/ public static final String BEAN_mediaType = PREFIX + "mediaType.s"; /** * Configuration property: Bean class exclusions. * *
Property:
*
    *
  • Name: "BeanContext.notBeanClasses.sc" *
  • Data type: Set<Class> *
  • Default: empty set *
  • Session property: false *
  • Annotations: *
      *
    • {@link BeanIgnore} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#notBeanClasses(Class...)} *
    • {@link BeanContextBuilder#notBeanClasses(Object...)} *
    • {@link BeanContextBuilder#notBeanClasses(boolean, Object...)} *
    • {@link BeanContextBuilder#notBeanClassesRemove(Object...)} *
    *
* *
Description:
*

* 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: *

    *
  • Classes. *
  • Arrays and collections of classes. *
* *
Example:
*

* // Create a serializer that doesn't treat MyBean as a bean class. * WriterSerializer s = JsonSerializer * .create() * .notBeanClasses(MyBean.class) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_notBeanClasses, MyBean.class) * .build(); *

*/ public static final String BEAN_notBeanClasses = PREFIX + "notBeanClasses.sc"; /** * Configuration property: Add to classes that should not be considered beans. */ public static final String BEAN_notBeanClasses_add = PREFIX + "notBeanClasses.sc/add"; /** * Configuration property: Remove from classes that should not be considered beans. */ public static final String BEAN_notBeanClasses_remove = PREFIX + "notBeanClasses.sc/remove"; /** * Configuration property: Bean package exclusions. * *
Property:
*
    *
  • Name: "BeanContext.notBeanPackages.ss" *
  • Data type: Set<String> *
  • Default: *
      *
    • java.lang *
    • java.lang.annotation *
    • java.lang.ref *
    • java.lang.reflect *
    • java.io *
    • java.net *
    • java.nio.* *
    • java.util.* *
    *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#notBeanPackages(Object...)} *
    • {@link BeanContextBuilder#notBeanPackages(String...)} *
    • {@link BeanContextBuilder#notBeanPackages(boolean, Object...)} *
    • {@link BeanContextBuilder#notBeanPackagesRemove(Object...)} *
    *
* *
Description:
*

* When specified, the current list of ignore packages are appended to. * *

* Any classes within these packages will be serialized to strings using {@link Object#toString()}. * *

* Note that you can specify suffix patterns to include all subpackages. * *

* Values can consist of any of the following types: *

    *
  • Strings. *
  • Arrays and collections of strings. *
* *
Example:
*

* // Create a serializer that ignores beans in the specified packages. * WriterSerializer s = JsonSerializer * .create() * .notBeanPackages("org.apache.foo", "org.apache.bar.*") * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_notBeanPackages, "org.apache.foo") * .addTo(BEAN_notBeanPackages, "org.apache.bar.*") * .build(); *

*/ public static final String BEAN_notBeanPackages = PREFIX + "notBeanPackages.ss"; /** * Configuration property: Add to packages whose classes should not be considered beans. */ public static final String BEAN_notBeanPackages_add = PREFIX + "notBeanPackages.ss/add"; /** * Configuration property: Remove from packages whose classes should not be considered beans. */ public static final String BEAN_notBeanPackages_remove = PREFIX + "notBeanPackages.ss/remove"; /** * Configuration property: POJO swaps. * *
Property:
*
    *
  • Name: "BeanContext.pojoSwaps.lo" *
  • Data type: List<Object> *
  • Default: empty list *
  • Session property: false *
  • Annotations: *
      *
    • {@link Swap} *
    • {@link Swaps} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#pojoSwaps(Object...)} *
    • {@link BeanContextBuilder#pojoSwaps(Class...)} *
    • {@link BeanContextBuilder#pojoSwaps(boolean, Object...)} *
    • {@link BeanContextBuilder#pojoSwapsRemove(Object...)} *
    *
* *
Description:
*

* 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 * {@link PojoSwap#forMediaTypes()} or {@link Swap#mediaTypes() @Swap(mediaTypes)} are used to come up with the best match. * *

* Values can consist of any of the following types: *

    *
  • Any subclass of {@link PojoSwap}. *
  • Any instance of {@link PojoSwap}. *
  • Any surrogate class. A shortcut for defining a {@link SurrogateSwap}. *
  • Any array or collection of the objects above. *
* *
Example:
*

* // Sample swap for converting Dates to ISO8601 strings. * public class MyDateSwap extends StringSwap<Date> { * // ISO8601 formatter. * private DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); * * @Override * public String swap(BeanSession session, Date o) { * return format.format(o); * } * * @Override * public Date unswap(BeanSession session, String o, ClassMeta hint) throws Exception { * return format.parse(o); * } * } * * // Sample bean with a Date field. * public class MyBean { * public Date date = new Date(112, 2, 3, 4, 5, 6); * } * * // Create a serializer that uses our date swap. * WriterSerializer s = JsonSerializer * .create() * .pojoSwaps(MyDateSwap.class) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .addTo(BEAN_pojoSwaps, MyDateSwap.class) * .build(); * * // Produces "{date:'2012-03-03T04:05:06-0500'}" * String json = s.serialize(new MyBean()); * * // Create a serializer that uses our date swap. * ReaderParser p = JsonParser * .create() * .pojoSwaps(MyDateSwap.class) * .build(); * * // Use our parser to parse a bean. * MyBean bean = p.parse(json, MyBean.class); *

* *
See Also:
*
    *
*/ public static final String BEAN_pojoSwaps = PREFIX + "pojoSwaps.lo"; /** * Configuration property: Add to POJO swap classes. */ public static final String BEAN_pojoSwaps_add = PREFIX + "pojoSwaps.lo/add"; /** * Configuration property: Remove from POJO swap classes. */ public static final String BEAN_pojoSwaps_remove = PREFIX + "pojoSwaps.lo/remove"; /** * Configuration property: Bean property namer. * *
Property:
*
    *
  • Name: "BeanContext.propertyNamer.c" *
  • Data type: Class<? implements {@link PropertyNamer}> *
  • Default: {@link PropertyNamerDefault} *
  • Session property: false *
  • Annotations: *
      *
    • {@link Bean#propertyNamer()} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#propertyNamer(Class)} *
    • {@link BeanFilterBuilder#propertyNamer(Class)} *
    *
* *
Description:
*

* The class to use for calculating bean property names. * *

* Predefined classes: *

    *
  • {@link PropertyNamerDefault} - Default. *
  • {@link PropertyNamerDLC} - Dashed-lower-case names. *
  • {@link PropertyNamerULC} - Dashed-upper-case names. *
* *
Example:
*

* // Create a serializer that uses Dashed-Lower-Case property names. * // (e.g. "foo-bar-url" instead of "fooBarURL") * WriterSerializer s = JsonSerializer * .create() * .propertyNamer(PropertyNamerDLC.class) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_propertyNamer, PropertyNamerDLC.class) * .build(); *

*/ public static final String BEAN_propertyNamer = PREFIX + "propertyNamer.c"; /** * Configuration property: Sort bean properties. * *
Property:
*
    *
  • Name: "BeanContext.sortProperties.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Annotations: *
      *
    • {@link Bean#sort()} *
    *
  • Methods: *
      *
    • {@link BeanContextBuilder#sortProperties(boolean)} *
    • {@link BeanContextBuilder#sortProperties()} *
    • {@link BeanFilterBuilder#sortProperties(boolean)} *
    • {@link BeanFilterBuilder#sortProperties()} *
    *
* *
Description:
*

* When true, all bean properties will be serialized and access in alphabetical order. *
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 {@link Bean @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. * *

Example:
*

* // Create a serializer that sorts bean properties. * WriterSerializer s = JsonSerializer * .create() * .sortProperties() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_sortProperties, true) * .build(); *

*/ public static final String BEAN_sortProperties = PREFIX + "sortProperties.b"; /** * Configuration property: Time zone. * *
Property:
*
    *
  • Name: "BeanContext.timeZone.s" *
  • Data type: String ({@link TimeZone}) *
  • Default: null *
  • Session property: true *
  • Methods: *
      *
    • {@link BeanContextBuilder#timeZone(TimeZone)} *
    • {@link BeanSessionArgs#timeZone(TimeZone)} *
    *
* *
Description:
*

* Specifies the default timezone for serializer and parser sessions. * *

Example:
*

* // Create a serializer that uses GMT if the timezone is not specified in the session args. * WriterSerializer s = JsonSerializer * .create() * .timeZone(TimeZone.GMT) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_timeZone, TimeZone.GMT) * .build(); * * // Define on session-args instead. * SerializerSessionArgs sessionArgs = new SerializerSessionArgs().timeZone(TimeZone.GMT); * try (WriterSerializerSession ss = JsonSerializer.DEFAULT.createSession(sessionArgs)) { * String json = s.serialize(new MyBean()); * } *

*/ public static final String BEAN_timeZone = PREFIX + "timeZone.s"; /** * Configuration property: Use enum names. * *
Property:
*
    *
  • Name: "BeanContext.useEnumNames.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#useEnumNames()} *
    *
* *
Description:
*

* When enabled, enums are always serialized by name, not using {@link Object#toString()}. * *

Example:
*

* // Create a serializer with debug enabled. * WriterSerializer s = JsonSerializer * .create() * .useEnumNames() * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_useEnumNames, true) * .build(); * * // Enum with overridden toString(). * // Will be serialized as ONE/TWO/THREE even though there's a toString() method.. * public enum Option { * ONE(1), * TWO(2), * THREE(3); * * private int i; * * Option(int i) { * this.i = i; * } * * @Override * public String toString() { * return String.valueOf(i); * } *

*/ public static final String BEAN_useEnumNames = PREFIX + "useEnumNames.b"; /** * Configuration property: Use interface proxies. * *
Property:
*
    *
  • Name: "BeanContext.useInterfaceProxies.b" *
  • Data type: Boolean *
  • Default: true *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#useInterfaceProxies(boolean)} *
    *
* *
Description:
*

* If true, then interfaces will be instantiated as proxy classes through the use of an * {@link InvocationHandler} if there is no other way of instantiating them. *
Otherwise, throws a {@link BeanRuntimeException}. * *

Example:
*

* // Create a parser that doesn't try to make interface proxies. * ReaderParser p = JsonParser * .create() * .useInterfaceProxies(false) * .build(); * * // Same, but use property. * ReaderParser p = JsonParser * .create() * .set(BEAN_useInterfaceProxies, false) * .build(); *

*/ public static final String BEAN_useInterfaceProxies = PREFIX + "useInterfaceProxies.b"; /** * Configuration property: Use Java Introspector. * *
Property:
*
    *
  • Name: "BeanContext.useJavaBeanIntrospector.b" *
  • Data type: Boolean *
  • Default: false *
  • Session property: false *
  • Methods: *
      *
    • {@link BeanContextBuilder#useJavaBeanIntrospector(boolean)} *
    • {@link BeanContextBuilder#useJavaBeanIntrospector()} *
    *
* *
Description:
*

* Using the built-in Java bean introspector will not pick up fields or non-standard getters/setters. *
Most {@link Bean @Bean} annotations will be ignored. * *

Example:
*

* // Create a serializer that only uses the built-in java bean introspector for finding properties. * WriterSerializer s = JsonSerializer * .create() * .useJavaBeanIntrospector(false) * .build(); * * // Same, but use property. * WriterSerializer s = JsonSerializer * .create() * .set(BEAN_useJavaBeanIntrospector, false) * .build(); *

*/ public static final String BEAN_useJavaBeanIntrospector = PREFIX + "useJavaBeanIntrospector.b"; /* * The default package pattern exclusion list. * Any beans in packages in this list will not be considered beans. */ private static final String[] DEFAULT_NOTBEAN_PACKAGES = { "java.lang", "java.lang.annotation", "java.lang.ref", "java.lang.reflect", "java.io", "java.net", "java.nio.*", "java.util.*" }; /* * The default bean class exclusion list. * Anything in this list will not be considered beans. */ private static final Class[] DEFAULT_NOTBEAN_CLASSES = { Map.class, Collection.class, Reader.class, Writer.class, InputStream.class, OutputStream.class, Throwable.class }; // This map is important! // We may have many Context objects that have identical BeanContext properties. // This map ensures that if the BeanContext properties in the Context are the same, // then we reuse the same Class->ClassMeta cache map. // This significantly reduces the number of times we need to construct ClassMeta objects which can be expensive. private static final ConcurrentHashMap> cmCacheCache = new ConcurrentHashMap<>(); /** Default config. All default settings. */ public static final BeanContext DEFAULT = BeanContext.create().build(); /** Default config. All default settings except sort bean properties. */ public static final BeanContext DEFAULT_SORTED = BeanContext.create().sortProperties().build(); private final boolean beansRequireDefaultConstructor, beansRequireSerializable, beansRequireSettersForGetters, beansRequireSomeProperties, beanMapPutReturnsOldValue, useInterfaceProxies, ignoreUnknownBeanProperties, ignoreUnknownNullBeanProperties, ignorePropertiesWithoutSetters, ignoreInvocationExceptionsOnGetters, ignoreInvocationExceptionsOnSetters, useJavaBeanIntrospector, useEnumNames, sortProperties, fluentSetters, debug; private final Visibility beanConstructorVisibility, beanClassVisibility, beanMethodVisibility, beanFieldVisibility; private final Class[] notBeanClasses; private final List> beanDictionaryClasses; private final String[] notBeanPackageNames, notBeanPackagePrefixes; private final BeanFilter[] beanFilters; private final PojoSwap[] pojoSwaps; private final Map examples; private final BeanRegistry beanRegistry; private final Map> implClasses; private final Locale locale; private final TimeZone timeZone; private final MediaType mediaType; private final Map includeProperties, excludeProperties; private final PropertyNamer propertyNamer; private final String beanTypePropertyName; private final int beanHashCode; final Map cmCache; private final ClassMeta cmObject; // Reusable ClassMeta that represents general Objects. private final ClassMeta cmString; // Reusable ClassMeta that represents general Strings. private final ClassMeta cmClass; // Reusable ClassMeta that represents general Classes. /** * Constructor. * *

* Typically only called from {@link ContextBuilder#build(Class)} method. * * @param ps The property store containing the unmodifiable configuration for this bean context. */ public BeanContext(PropertyStore ps) { super(ps); if (ps == null) ps = PropertyStore.DEFAULT; beanHashCode = ps.hashCode("BeanContext"); beansRequireDefaultConstructor = getBooleanProperty(BEAN_beansRequireDefaultConstructor, false); beansRequireSerializable = getBooleanProperty(BEAN_beansRequireSerializable, false); beansRequireSettersForGetters = getBooleanProperty(BEAN_beansRequireSettersForGetters, false); beansRequireSomeProperties = getBooleanProperty(BEAN_beansRequireSomeProperties, true); beanMapPutReturnsOldValue = getBooleanProperty(BEAN_beanMapPutReturnsOldValue, false); useEnumNames = getBooleanProperty(BEAN_useEnumNames, false); useInterfaceProxies = getBooleanProperty(BEAN_useInterfaceProxies, true); ignoreUnknownBeanProperties = getBooleanProperty(BEAN_ignoreUnknownBeanProperties, false); ignoreUnknownNullBeanProperties = getBooleanProperty(BEAN_ignoreUnknownNullBeanProperties, true); ignorePropertiesWithoutSetters = getBooleanProperty(BEAN_ignorePropertiesWithoutSetters, true); ignoreInvocationExceptionsOnGetters = getBooleanProperty(BEAN_ignoreInvocationExceptionsOnGetters, false); ignoreInvocationExceptionsOnSetters = getBooleanProperty(BEAN_ignoreInvocationExceptionsOnSetters, false); useJavaBeanIntrospector = getBooleanProperty(BEAN_useJavaBeanIntrospector, false); sortProperties = getBooleanProperty(BEAN_sortProperties, false); fluentSetters = getBooleanProperty(BEAN_fluentSetters, false); beanTypePropertyName = getStringProperty(BEAN_beanTypePropertyName, "_type"); debug = getBooleanProperty(BEAN_debug, false); beanConstructorVisibility = getProperty(BEAN_beanConstructorVisibility, Visibility.class, PUBLIC); beanClassVisibility = getProperty(BEAN_beanClassVisibility, Visibility.class, PUBLIC); beanMethodVisibility = getProperty(BEAN_beanMethodVisibility, Visibility.class, PUBLIC); beanFieldVisibility = getProperty(BEAN_beanFieldVisibility, Visibility.class, PUBLIC); notBeanClasses = getClassArrayProperty(BEAN_notBeanClasses, DEFAULT_NOTBEAN_CLASSES); propertyNamer = getInstanceProperty(BEAN_propertyNamer, PropertyNamer.class, PropertyNamerDefault.class); List l1 = new LinkedList<>(); List l2 = new LinkedList<>(); for (String s : getArrayProperty(BEAN_notBeanPackages, String.class, DEFAULT_NOTBEAN_PACKAGES)) { if (s.endsWith(".*")) l2.add(s.substring(0, s.length()-2)); else l1.add(s); } notBeanPackageNames = l1.toArray(new String[l1.size()]); notBeanPackagePrefixes = l2.toArray(new String[l2.size()]); LinkedList lbf = new LinkedList<>(); for (Class c : getClassListProperty(BEAN_beanFilters)) { if (isParentClass(BeanFilter.class, c)) lbf.add(newInstance(BeanFilter.class, c)); else if (isParentClass(BeanFilterBuilder.class, c)) lbf.add(newInstance(BeanFilterBuilder.class, c).build()); else lbf.add(new InterfaceBeanFilterBuilder(c).build()); } beanFilters = lbf.toArray(new BeanFilter[0]); LinkedList> lpf = new LinkedList<>(); for (Object o : getListProperty(BEAN_pojoSwaps, Object.class)) { if (o instanceof Class) { Class c = (Class)o; if (isParentClass(PojoSwap.class, c)) lpf.add(newInstance(PojoSwap.class, c)); else if (isParentClass(Surrogate.class, c)) lpf.addAll(SurrogateSwap.findPojoSwaps(c)); else throw new FormattedRuntimeException("Invalid class {0} specified in BeanContext.pojoSwaps property. Must be a subclass of PojoSwap or Surrogate.", c); } else if (o instanceof PojoSwap) { lpf.add((PojoSwap)o); } } pojoSwaps = lpf.toArray(new PojoSwap[lpf.size()]); examples = getMapProperty(BEAN_examples, Object.class); implClasses = getClassMapProperty(BEAN_implClasses); Map m2 = new HashMap<>(); for (Map.Entry e : getMapProperty(BEAN_includeProperties, String.class).entrySet()) m2.put(e.getKey(), StringUtils.split(e.getValue())); includeProperties = unmodifiableMap(m2); m2 = new HashMap<>(); for (Map.Entry e : getMapProperty(BEAN_excludeProperties, String.class).entrySet()) m2.put(e.getKey(), StringUtils.split(e.getValue())); excludeProperties = unmodifiableMap(m2); locale = getInstanceProperty(BEAN_locale, Locale.class, null); timeZone = getInstanceProperty(BEAN_timeZone, TimeZone.class, null); mediaType = getInstanceProperty(BEAN_mediaType, MediaType.class, null); if (! cmCacheCache.containsKey(beanHashCode)) { ConcurrentHashMap cm = new ConcurrentHashMap<>(); cm.putIfAbsent(String.class, new ClassMeta(String.class, this, null, null, findPojoSwaps(String.class), findChildPojoSwaps(String.class), findExample(String.class))); cm.putIfAbsent(Object.class, new ClassMeta(Object.class, this, null, null, findPojoSwaps(Object.class), findChildPojoSwaps(Object.class), findExample(Object.class))); cmCacheCache.putIfAbsent(beanHashCode, cm); } cmCache = cmCacheCache.get(beanHashCode); cmString = cmCache.get(String.class); cmObject = cmCache.get(Object.class); cmClass = cmCache.get(Class.class); beanDictionaryClasses = unmodifiableList(Arrays.asList(getClassArrayProperty(BEAN_beanDictionary))); beanRegistry = new BeanRegistry(this, null); } @Override /* Context */ public BeanContextBuilder builder() { return new BeanContextBuilder(getPropertyStore()); } /** * Instantiates a new clean-slate {@link BeanContextBuilder} object. * *

* This is equivalent to simply calling new BeanContextBuilder(). * * @return A new {@link JsonSerializerBuilder} object. */ public static BeanContextBuilder create() { return new BeanContextBuilder(); } /** * Create a new bean session based on the properties defined on this context. * *

* Use this method for creating sessions if you don't need to override any * properties or locale/timezone currently set on this context. * * @return A new session object. */ @Override /* Context */ public BeanSession createSession() { return createBeanSession(createDefaultSessionArgs()); } /** * Create a new bean session based on the properties defined on this context combined with the specified * runtime args. * *

* Use this method for creating sessions if you don't need to override any * properties or locale/timezone currently set on this context. * * @param args * The session arguments. * @return A new session object. */ public BeanSession createSession(BeanSessionArgs args) { return createBeanSession(args); } @Override /* Context */ public final Session createSession(SessionArgs args) { throw new NoSuchMethodError(); } /** * Same as {@link #createSession(BeanSessionArgs)} except always returns a {@link BeanSession} object unlike {@link #createSession(BeanSessionArgs)} * which is meant to be overridden by subclasses. * * @param args The session arguments. * @return A new session object. */ public final BeanSession createBeanSession(BeanSessionArgs args) { return new BeanSession(this, args); } /** * Same as {@link #createSession()} except always returns a {@link BeanSession} object unlike {@link #createSession()} * which is meant to be overridden by subclasses. * * @return A new session object. */ public final BeanSession createBeanSession() { return new BeanSession(this, createDefaultBeanSessionArgs()); } @Override /* Context */ public BeanSessionArgs createDefaultSessionArgs() { return createDefaultBeanSessionArgs(); } /** * Same as {@link #createDefaultSessionArgs()} except always returns a {@link BeanSessionArgs} unlike * {@link #createDefaultBeanSessionArgs()} which is meant to be overridden by subclasses. * * @return A new session arguments object. */ public final BeanSessionArgs createDefaultBeanSessionArgs() { return new BeanSessionArgs(); } /** * Returns true if the specified bean context shares the same cache as this bean context. * *

* Useful for testing purposes. * * @param bc The bean context to compare to. * @return true if the bean contexts have equivalent settings and thus share caches. */ public final boolean hasSameCache(BeanContext bc) { return bc.cmCache == this.cmCache; } /** * Determines whether the specified class is ignored as a bean class based on the various exclusion parameters * specified on this context class. * * @param c The class type being tested. * @return true if the specified class matches any of the exclusion parameters. */ protected final boolean isNotABean(Class c) { if (c.isArray() || c.isPrimitive() || c.isEnum() || c.isAnnotation()) return true; Package p = c.getPackage(); if (p != null) { for (String p2 : notBeanPackageNames) if (p.getName().equals(p2)) return true; for (String p2 : notBeanPackagePrefixes) if (p.getName().startsWith(p2)) return true; } for (Class exclude : notBeanClasses) if (isParentClass(exclude, c)) return true; return false; } /** * Returns true if the specified object is a bean. * * @param o The object to test. * @return true if the specified object is a bean. false if the bean is null. */ public boolean isBean(Object o) { if (o == null) return false; return getClassMetaForObject(o).isBean(); } /** * Prints meta cache statistics to System.out. */ protected static void dumpCacheStats() { try { int ctCount = 0; for (Map cm : cmCacheCache.values()) ctCount += cm.size(); System.out.println(format("ClassMeta cache: {0} instances in {1} caches", ctCount, cmCacheCache.size())); // NOT DEBUG } catch (Exception e) { e.printStackTrace(); } } /** * Returns the {@link BeanMeta} class for the specified class. * * @param The class type to get the meta-data on. * @param c The class to get the meta-data on. * @return * The {@link BeanMeta} for the specified class, or null if the class is not a bean per the settings on * this context. */ public final BeanMeta getBeanMeta(Class c) { if (c == null) return null; return getClassMeta(c).getBeanMeta(); } /** * Construct a {@code ClassMeta} wrapper around a {@link Class} object. * * @param The class type being wrapped. * @param type The class to resolve. * @return * If the class is not an array, returns a cached {@link ClassMeta} object. * Otherwise, returns a new {@link ClassMeta} object every time. */ public final ClassMeta getClassMeta(Class type) { return getClassMeta(type, true); } /** * Construct a {@code ClassMeta} wrapper around a {@link Class} object. * * @param The class type being wrapped. * @param type The class to resolve. * @param waitForInit * If true, wait for the ClassMeta constructor to finish before returning. * @return * If the class is not an array, returns a cached {@link ClassMeta} object. * Otherwise, returns a new {@link ClassMeta} object every time. */ final ClassMeta getClassMeta(Class type, boolean waitForInit) { // If this is an array, then we want it wrapped in an uncached ClassMeta object. // Note that if it has a pojo swap, we still want to cache it so that // we can cache something like byte[] with ByteArrayBase64Swap. if (type.isArray() && findPojoSwaps(type) == null) return new ClassMeta(type, this, findImplClass(type), findBeanFilter(type), findPojoSwaps(type), findChildPojoSwaps(type), findExample(type)); // This can happen if we have transforms defined against String or Object. if (cmCache == null) return null; ClassMeta cm = cmCache.get(type); if (cm == null) { synchronized (this) { // Make sure someone didn't already set it while this thread was blocked. cm = cmCache.get(type); if (cm == null) cm = new ClassMeta<>(type, this, findImplClass(type), findBeanFilter(type), findPojoSwaps(type), findChildPojoSwaps(type), findExample(type)); } } if (waitForInit) cm.waitForInit(); return cm; } /** * Used to resolve 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. * *

Examples:
*
    *
  • 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. *
* * @param type * The class to resolve. *
Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} * @param args * The type arguments of the class if it's a collection or map. *
Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} *
Ignored if the main type is not a map or collection. * @return The resolved class meta. */ public final ClassMeta getClassMeta(Type type, Type...args) { if (type == null) return null; ClassMeta cm = type instanceof Class ? getClassMeta((Class)type) : resolveClassMeta(type, null); if (args.length == 0) return cm; ClassMeta[] cma = new ClassMeta[args.length+1]; cma[0] = cm; for (int i = 0; i < Array.getLength(args); i++) { Type arg = (Type)Array.get(args, i); cma[i+1] = arg instanceof Class ? getClassMeta((Class)arg) : resolveClassMeta(arg, null); } return (ClassMeta) getTypedClassMeta(cma, 0); } /* * Resolves the 'genericized' class meta at the specified position in the ClassMeta array. */ private ClassMeta getTypedClassMeta(ClassMeta[] c, int pos) { ClassMeta cm = c[pos++]; if (cm.isCollection()) { ClassMeta ce = c.length == pos ? object() : getTypedClassMeta(c, pos); return (ce.isObject() ? cm : new ClassMeta(cm, null, null, ce)); } else if (cm.isMap()) { ClassMeta ck = c.length == pos ? object() : c[pos++]; ClassMeta cv = c.length == pos ? object() : getTypedClassMeta(c, pos); return (ck.isObject() && cv.isObject() ? cm : new ClassMeta(cm, ck, cv, null)); } return cm; } final ClassMeta resolveClassMeta(Type o, Map,Class[]> typeVarImpls) { if (o == null) return null; if (o instanceof ClassMeta) { ClassMeta cm = (ClassMeta)o; // This classmeta could have been created by a different context. // Need to re-resolve it to pick up PojoSwaps and stuff on this context. if (cm.getBeanContext() == this) return cm; if (cm.isMap()) return getClassMeta(cm.innerClass, cm.getKeyType(), cm.getValueType()); if (cm.isCollection()) return getClassMeta(cm.innerClass, cm.getElementType()); return getClassMeta(cm.innerClass); } Class c = resolve(o, typeVarImpls); // This can happen when trying to resolve the "E getFirst()" method on LinkedList, whose type is a TypeVariable // These should just resolve to Object. if (c == null) return object(); ClassMeta rawType = getClassMeta(c); // If this is a Map or Collection, and the parameter types aren't part // of the class definition itself (e.g. class AddressBook extends List), // then we need to figure out the parameters. if (rawType.isMap() || rawType.isCollection()) { ClassMeta[] params = findParameters(o, c); if (params == null) return rawType; if (rawType.isMap()) { if (params.length != 2) return rawType; if (params[0].isObject() && params[1].isObject()) return rawType; return new ClassMeta(rawType, params[0], params[1], null); } if (rawType.isCollection()) { if (params.length != 1) return rawType; if (params[0].isObject()) return rawType; return new ClassMeta(rawType, null, null, params[0]); } } return rawType; } /** * Convert a Type to a Class if possible. * Return null if not possible. */ final Class resolve(Type t, Map,Class[]> typeVarImpls) { if (t instanceof Class) return (Class)t; if (t instanceof ParameterizedType) // A parameter (e.g. . return (Class)((ParameterizedType)t).getRawType(); if (t instanceof GenericArrayType) { // An array parameter (e.g. ). Type gatct = ((GenericArrayType)t).getGenericComponentType(); if (gatct instanceof Class) return Array.newInstance((Class)gatct, 0).getClass(); if (gatct instanceof ParameterizedType) return Array.newInstance((Class)((ParameterizedType)gatct).getRawType(), 0).getClass(); if (gatct instanceof GenericArrayType) return Array.newInstance(resolve(gatct, typeVarImpls), 0).getClass(); return null; } else if (t instanceof TypeVariable) { if (typeVarImpls != null) { TypeVariable tv = (TypeVariable)t; String varName = tv.getName(); int varIndex = -1; Class gc = (Class)tv.getGenericDeclaration(); TypeVariable[] tvv = gc.getTypeParameters(); for (int i = 0; i < tvv.length; i++) { if (tvv[i].getName().equals(varName)) { varIndex = i; } } if (varIndex != -1) { // If we couldn't find a type variable implementation, that means // the type was defined at runtime (e.g. Bean b = new Bean();) // in which case the type is lost through erasure. // Assume java.lang.Object as the type. if (! typeVarImpls.containsKey(gc)) return null; return typeVarImpls.get(gc)[varIndex]; } } } return null; } final ClassMeta[] findParameters(Type o, Class c) { if (o == null) o = c; // Loop until we find a ParameterizedType if (! (o instanceof ParameterizedType)) { loop: do { o = c.getGenericSuperclass(); if (o instanceof ParameterizedType) break loop; for (Type t : c.getGenericInterfaces()) { o = t; if (o instanceof ParameterizedType) break loop; } c = c.getSuperclass(); } while (c != null); } if (o instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType)o; if (! pt.getRawType().equals(Enum.class)) { List> l = new LinkedList<>(); for (Type pt2 : pt.getActualTypeArguments()) { if (pt2 instanceof WildcardType || pt2 instanceof TypeVariable) return null; l.add(resolveClassMeta(pt2, null)); } if (l.isEmpty()) return null; return l.toArray(new ClassMeta[l.size()]); } } return null; } /** * Shortcut for calling {@code getClassMeta(o.getClass())}. * * @param The class of the object being passed in. * @param o The class to find the class type for. * @return The ClassMeta object, or null if {@code o} is null. */ public final ClassMeta getClassMetaForObject(T o) { if (o == null) return null; return (ClassMeta)getClassMeta(o.getClass()); } /** * Used for determining the class type on a method or field where a {@code @BeanProperty} annotation may be present. * * @param The class type we're wrapping. * @param p The property annotation on the type if there is one. * @param t The type. * @param typeVarImpls * Contains known resolved type parameters on the specified class so that we can result * {@code ParameterizedTypes} and {@code TypeVariables}. * Can be null if the information is not known. * @return The new {@code ClassMeta} object wrapped around the {@code Type} object. */ protected final ClassMeta resolveClassMeta(BeanProperty p, Type t, Map,Class[]> typeVarImpls) { ClassMeta cm = resolveClassMeta(t, typeVarImpls); ClassMeta cm2 = cm; if (p != null) { if (p.type() != Object.class) cm2 = resolveClassMeta(p.type(), typeVarImpls); if (cm2.isMap()) { Class[] pParams = (p.params().length == 0 ? new Class[]{Object.class, Object.class} : p.params()); if (pParams.length != 2) throw new FormattedRuntimeException("Invalid number of parameters specified for Map (must be 2): {0}", pParams.length); ClassMeta keyType = resolveType(pParams[0], cm2.getKeyType(), cm.getKeyType()); ClassMeta valueType = resolveType(pParams[1], cm2.getValueType(), cm.getValueType()); if (keyType.isObject() && valueType.isObject()) return cm2; return new ClassMeta<>(cm2, keyType, valueType, null); } if (cm2.isCollection()) { Class[] pParams = (p.params().length == 0 ? new Class[]{Object.class} : p.params()); if (pParams.length != 1) throw new FormattedRuntimeException("Invalid number of parameters specified for Collection (must be 1): {0}", pParams.length); ClassMeta elementType = resolveType(pParams[0], cm2.getElementType(), cm.getElementType()); if (elementType.isObject()) return cm2; return new ClassMeta<>(cm2, null, null, elementType); } return cm2; } return cm; } private ClassMeta resolveType(Type...t) { for (Type tt : t) { if (tt != null) { ClassMeta cm = getClassMeta(tt); if (tt != cmObject) return cm; } } return cmObject; } /** * Returns the {@link PojoSwap} associated with the specified class, or null if there is no POJO swap * associated with the class. * * @param The class associated with the swap. * @param c The class associated with the swap. * @return The swap associated with the class, or null if there is no association. */ private final PojoSwap[] findPojoSwaps(Class c) { // Note: On first if (c != null) { List l = new ArrayList<>(); for (PojoSwap f : pojoSwaps) if (isParentClass(f.getNormalClass(), c)) l.add(f); return l.size() == 0 ? null : l.toArray(new PojoSwap[l.size()]); } return null; } private final Object findExample(Class c) { if (c != null) { Object o = examples.get(c.getName()); if (o != null) return o; Class c2 = findImplClass(c); if (c2 == null) return null; return examples.get(c2.getName()); } return null; } /** * Checks whether a class has a {@link PojoSwap} associated with it in this bean context. * * @param c The class to check. * @return true if the specified class or one of its subclasses has a {@link PojoSwap} associated with it. */ private final PojoSwap[] findChildPojoSwaps(Class c) { if (c == null || pojoSwaps.length == 0) return null; List l = null; for (PojoSwap f : pojoSwaps) { if (isParentClass(c, f.getNormalClass())) { if (l == null) l = new ArrayList<>(); l.add(f); } } return l == null ? null : l.toArray(new PojoSwap[l.size()]); } /** * Returns the {@link BeanFilter} associated with the specified class, or null if there is no bean filter * associated with the class. * * @param The class associated with the bean filter. * @param c The class associated with the bean filter. * @return The bean filter associated with the class, or null if there is no association. */ private final BeanFilter findBeanFilter(Class c) { if (c != null) for (BeanFilter f : beanFilters) if (isParentClass(f.getBeanClass(), c)) return f; return null; } /** * Gets the no-arg constructor for the specified class. * * @param The class to check. * @param c The class to check. * @param v The minimum visibility for the constructor. * @return The no arg constructor, or null if the class has no no-arg constructor. */ protected final Constructor getImplClassConstructor(Class c, Visibility v) { if (implClasses.isEmpty()) return null; Class cc = c; while (cc != null) { Class implClass = implClasses.get(cc.getName()); if (implClass != null) return findNoArgConstructor(implClass, v); for (Class ic : cc.getInterfaces()) { implClass = implClasses.get(ic.getName()); if (implClass != null) return findNoArgConstructor(implClass, v); } cc = cc.getSuperclass(); } return null; } private final Class findImplClass(Class c) { if (implClasses.isEmpty()) return null; Class cc = c; while (cc != null) { Class implClass = implClasses.get(cc.getName()); if (implClass != null) return implClass; for (Class ic : cc.getInterfaces()) { implClass = implClasses.get(ic.getName()); if (implClass != null) return implClass; } cc = cc.getSuperclass(); } return null; } /** * Returns the {@link #BEAN_includeProperties} setting for the specified class. * * @param c The class. * @return The properties to include for the specified class, or null if it's not defined for the class. */ protected String[] getIncludeProperties(Class c) { if (includeProperties.isEmpty()) return null; String[] s = null; for (Iterator> i = ClassUtils.getParentClasses(c, false, true); i.hasNext();) { Class c2 = i.next(); s = includeProperties.get(c2.getName()); if (s != null) return s; s = includeProperties.get(c2.getSimpleName()); if (s != null) return s; } return includeProperties.get("*"); } /** * Returns the {@link #BEAN_excludeProperties} setting for the specified class. * * @param c The class. * @return The properties to exclude for the specified class, or null if it's not defined for the class. */ protected String[] getExcludeProperties(Class c) { if (excludeProperties.isEmpty()) return null; String[] s = null; for (Iterator> i = ClassUtils.getParentClasses(c, false, true); i.hasNext();) { Class c2 = i.next(); s = excludeProperties.get(c2.getName()); if (s != null) return s; s = excludeProperties.get(c2.getSimpleName()); if (s != null) return s; } return excludeProperties.get("*"); } /** * Creates an instance of the specified class. * * @param c * The class to cast to. * @param c2 * The class to instantiate. * Can also be an instance of the class. * @return * The new class instance, or null if the class was null or is abstract or an interface. * @throws * RuntimeException if constructor could not be found or called. */ public T newInstance(Class c, Object c2) { return ClassUtils.newInstance(c, c2); } /** * Creates an instance of the specified class. * * @param c * The class to cast to. * @param c2 * The class to instantiate. * Can also be an instance of the class. * @param fuzzyArgs * Use fuzzy constructor arg matching. *
When true, constructor args can be in any order and extra args are ignored. *
No-arg constructors are also used if no other constructors are found. * @param args * The arguments to pass to the constructor. * @return * The new class instance, or null if the class was null or is abstract or an interface. * @throws * RuntimeException if constructor could not be found or called. */ public T newInstance(Class c, Object c2, boolean fuzzyArgs, Object...args) { return ClassUtils.newInstance(c, c2, fuzzyArgs, args); } /** * Creates an instance of the specified class from within the context of another object. * * @param outer * The outer object. * Can be null. * @param c * The class to cast to. * @param c2 * The class to instantiate. * Can also be an instance of the class. * @param fuzzyArgs * Use fuzzy constructor arg matching. *
When true, constructor args can be in any order and extra args are ignored. *
No-arg constructors are also used if no other constructors are found. * @param args * The arguments to pass to the constructor. * @return * The new class instance, or null if the class was null or is abstract or an interface. * @throws * RuntimeException if constructor could not be found or called. */ public T newInstanceFromOuter(Object outer, Class c, Object c2, boolean fuzzyArgs, Object...args) { return ClassUtils.newInstanceFromOuter(outer, c, c2, fuzzyArgs, args); } /** * Returns a reusable {@link 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.class) but uses a cached copy to * avoid a hashmap lookup. * * @return The {@link ClassMeta} object associated with the Object class. */ protected final ClassMeta object() { return cmObject; } /** * Returns a reusable {@link 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.class) but uses a cached copy to * avoid a hashmap lookup. * * @return The {@link ClassMeta} object associated with the String class. */ protected final ClassMeta string() { return cmString; } /** * Returns a reusable {@link 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.class) but uses a cached copy to * avoid a hashmap lookup. * * @return The {@link ClassMeta} object associated with the String class. */ protected final ClassMeta _class() { return cmClass; } /** * Returns the lookup table for resolving bean types by name. * * @return The lookup table for resolving bean types by name. */ protected final BeanRegistry getBeanRegistry() { return beanRegistry; } //----------------------------------------------------------------------------------------------------------------- // Properties //----------------------------------------------------------------------------------------------------------------- /** * Configuration property: Beans require no-arg constructors. * * @see #BEAN_beansRequireDefaultConstructor * @return * true if a Java class must implement a default no-arg constructor to be considered a bean. *
Otherwise, the bean will be serialized as a string using the {@link Object#toString()} method. */ protected final boolean isBeansRequireDefaultConstructor() { return beansRequireDefaultConstructor; } /** * Configuration property: Beans require Serializable interface. * * @see #BEAN_beansRequireSerializable * @return * true if a Java class must implement the {@link Serializable} interface to be considered a bean. *
Otherwise, the bean will be serialized as a string using the {@link Object#toString()} method. */ protected final boolean isBeansRequireSerializable() { return beansRequireSerializable; } /** * Configuration property: Beans require setters for getters. * * @see #BEAN_beansRequireSettersForGetters * @return * true if only getters that have equivalent setters will be considered as properties on a bean. *
Otherwise, they are ignored. */ protected final boolean isBeansRequireSettersForGetters() { return beansRequireSettersForGetters; } /** * Configuration property: Beans require at least one property. * * @see #BEAN_beansRequireSomeProperties * @return * true if a Java class must contain at least 1 property to be considered a bean. *
Otherwise, the bean is serialized as a string using the {@link Object#toString()} method. */ protected final boolean isBeansRequireSomeProperties() { return beansRequireSomeProperties; } /** * Configuration property: BeanMap.put() returns old property value. * * @see #BEAN_beanMapPutReturnsOldValue * @return * true if the {@link BeanMap#put(String,Object) BeanMap.put()} method will return old property values. *
Otherwise, it returns null. */ protected final boolean isBeanMapPutReturnsOldValue() { return beanMapPutReturnsOldValue; } /** * Configuration property: Use interface proxies. * * @see #BEAN_useInterfaceProxies * @return * true if interfaces will be instantiated as proxy classes through the use of an * {@link InvocationHandler} if there is no other way of instantiating them. */ protected final boolean isUseInterfaceProxies() { return useInterfaceProxies; } /** * Configuration property: Ignore unknown properties. * * @see #BEAN_ignoreUnknownBeanProperties * @return * true if trying to set a value on a non-existent bean property is silently ignored. *
Otherwise, a {@code RuntimeException} is thrown. */ protected final boolean isIgnoreUnknownBeanProperties() { return ignoreUnknownBeanProperties; } /** * Configuration property: Ignore unknown properties with null values. * * @see #BEAN_ignoreUnknownNullBeanProperties * @return * true if trying to set a null value on a non-existent bean property is silently ignored. */ protected final boolean isIgnoreUnknownNullBeanProperties() { return ignoreUnknownNullBeanProperties; } /** * Configuration property: Ignore properties without setters. * *
Otherwise, a {@code RuntimeException} is thrown. * * @see #BEAN_ignorePropertiesWithoutSetters * @return * true if trying to set a value on a bean property without a setter is silently ignored. */ protected final boolean isIgnorePropertiesWithoutSetters() { return ignorePropertiesWithoutSetters; } /** * Configuration property: Ignore invocation errors on getters. * * @see #BEAN_ignoreInvocationExceptionsOnGetters * @return * true if errors thrown when calling bean getter methods are silently ignored. */ protected final boolean isIgnoreInvocationExceptionsOnGetters() { return ignoreInvocationExceptionsOnGetters; } /** * Configuration property: Ignore invocation errors on setters. * * @see #BEAN_ignoreInvocationExceptionsOnSetters * @return * true if errors thrown when calling bean setter methods are silently ignored. */ protected final boolean isIgnoreInvocationExceptionsOnSetters() { return ignoreInvocationExceptionsOnSetters; } /** * Configuration property: Use Java Introspector. * * @see #BEAN_useJavaBeanIntrospector * @return * true if the built-in Java bean introspector should be used for bean introspection. */ protected final boolean isUseJavaBeanIntrospector() { return useJavaBeanIntrospector; } /** * Configuration property: Use enum names. * * @see #BEAN_useEnumNames * @return * true if enums are always serialized by name, not using {@link Object#toString()}. */ protected final boolean isUseEnumNames() { return useEnumNames; } /** * Configuration property: Sort bean properties. * * @see #BEAN_sortProperties * @return * true if all bean properties will be serialized and access in alphabetical order. */ protected final boolean isSortProperties() { return sortProperties; } /** * Configuration property: Find fluent setters. * *

Description:
*

* * @see #BEAN_fluentSetters * @return * true if fluent setters are detected on beans. */ protected final boolean isFluentSetters() { return fluentSetters; } /** * Configuration property: Debug mode. * * @see #BEAN_debug * @return * true if debug mode is enabled. */ protected boolean isDebug() { return debug; } /** * Configuration property: Minimum bean constructor visibility. * * @see #BEAN_beanConstructorVisibility * @return * Only look for constructors with this specified minimum visibility. */ protected final Visibility getBeanConstructorVisibility() { return beanConstructorVisibility; } /** * Configuration property: Minimum bean class visibility. * * @see #BEAN_beanClassVisibility * @return * Classes are not considered beans unless they meet the minimum visibility requirements. */ protected final Visibility getBeanClassVisibility() { return beanClassVisibility; } /** * Configuration property: Minimum bean method visibility. * * @see #BEAN_beanMethodVisibility * @return * Only look for bean methods with this specified minimum visibility. */ protected final Visibility getBeanMethodVisibility() { return beanMethodVisibility; } /** * Configuration property: Minimum bean field visibility. * * * @see #BEAN_beanFieldVisibility * @return * Only look for bean fields with this specified minimum visibility. */ protected final Visibility getBeanFieldVisibility() { return beanFieldVisibility; } /** * Configuration property: Bean dictionary. * * @see #BEAN_beanDictionary * @return * The list of classes that make up the bean dictionary in this bean context. */ protected final List> getBeanDictionaryClasses() { return beanDictionaryClasses; } /** * Configuration property: Locale. * * @see #BEAN_locale * @return * The default locale for serializer and parser sessions. */ protected final Locale getLocale() { return locale; } /** * Configuration property: Time zone. * * @see #BEAN_timeZone * @return * The default timezone for serializer and parser sessions. */ protected final TimeZone getTimeZone() { return timeZone; } /** * Configuration property: Media type. * * @see #BEAN_mediaType * @return * The default media type value for serializer and parser sessions. */ protected final MediaType getMediaType() { return mediaType; } /** * Configuration property: Bean property namer. * * @see #BEAN_propertyNamer * @return * The interface used to calculate bean property names. */ protected final PropertyNamer getPropertyNamer() { return propertyNamer; } /** * Configuration property: Bean type property name. * * @see #BEAN_beanTypePropertyName * @return * 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. */ protected final String getBeanTypePropertyName() { return beanTypePropertyName; } @Override /* Context */ public ObjectMap asMap() { return super.asMap() .append("BeanContext", new ObjectMap() .append("id", System.identityHashCode(this)) .append("beanClassVisibility", beanClassVisibility) .append("beanConstructorVisibility", beanConstructorVisibility) .append("beanDictionaryClasses", beanDictionaryClasses) .append("beanFieldVisibility", beanFieldVisibility) .append("beanFilters", beanFilters) .append("beanMapPutReturnsOldValue", beanMapPutReturnsOldValue) .append("beanMethodVisibility", beanMethodVisibility) .append("beansRequireDefaultConstructor", beansRequireDefaultConstructor) .append("beansRequireSerializable", beansRequireSerializable) .append("beansRequireSettersForGetters", beansRequireSettersForGetters) .append("beansRequireSomeProperties", beansRequireSomeProperties) .append("excludeProperties", excludeProperties) .append("ignoreInvocationExceptionsOnGetters", ignoreInvocationExceptionsOnGetters) .append("ignoreInvocationExceptionsOnSetters", ignoreInvocationExceptionsOnSetters) .append("ignorePropertiesWithoutSetters", ignorePropertiesWithoutSetters) .append("ignoreUnknownBeanProperties", ignoreUnknownBeanProperties) .append("ignoreUnknownNullBeanProperties", ignoreUnknownNullBeanProperties) .append("implClasses", implClasses) .append("includeProperties", includeProperties) .append("locale", locale) .append("mediaType", mediaType) .append("notBeanClasses", notBeanClasses) .append("notBeanPackageNames", notBeanPackageNames) .append("notBeanPackagePrefixes", notBeanPackagePrefixes) .append("pojoSwaps", pojoSwaps) .append("sortProperties", sortProperties) .append("timeZone", timeZone) .append("useEnumNames", useEnumNames) .append("useInterfaceProxies", useInterfaceProxies) .append("useJavaBeanIntrospector", useJavaBeanIntrospector) ); } }