|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--dynclass.BeanCreator
BeanCreator provides methods for representing a Map as a JavaBean.
The createBeanFromMap()
method performs the entire conversion
in a single step. This is the most convenient way to perform conversion;
however it is not the fastest, and may constribute to memory retention
(i.e. leaks) in some cases - see below.
There is also a two-step Map-to-JavaBean process that is
more efficient and eliminates memory concerns, which can be used when
the set of properties on the generated bean is known in advance. First,
create the JavaBeans Class with createClassForProperties()
.
Cache this class for the life of the application (e.g. in a static
variable). Then, when a JavaBean with this property set is needed, call
createBean()
with a Map reflecting the property values.
Example:
// Class variable:
private static final Class userBeanClass = BeanCreator.createClassForProperties(new String[] { "userName", "email" });
// ...
Map map = ...; // create map with values for "userName" and "email"
Object bean = BeanCreator.createBean(userBeanClass, map);
Memory issue: BeanCreator will not retain references to
Classes it generates, since this would prevent garbage collection of
the Class instances. However, many third-party JavaBeans libraries
keep static caches of Classes they have analyzed, and do not provide for
releasing cached resources. This will not be noticable in short-lived
applications, but long-running servers could be affected. If you are
concerned about this, use createClassFromPropertySet()
to
generate a fixed set of Classes, and instantiate them with
createBean()
.
Property names: Any String can be used as a property name, even if it is not a valid Java identifier. If BeanCreator encounters a property name that does not follow the usual JavaBeans casing conventions (or contains non-identifier characters), BeanCreator will generate a BeanInfo class for the bean class. This is transparent to clients and bean inspection tools.
Method Summary | |
static Object |
createBean(Class beanClass,
Map propertyMap)
Create a JavaBean with the given class. |
static Object |
createBeanFromMap(Map map)
Generate a object with get and set methods for every key in the map that can be translated into a JavaBeans property name. |
static Class |
createClassForProperties(Set keySet)
Create a JavaBeans class from the given property set. |
static Class |
createClassForProperties(String[] props)
Create a JavaBeans class from the given property list. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static Object createBeanFromMap(Map map) throws Exception
Generate a object with get and set methods for every key in the map that can be translated into a JavaBeans property name. See class documentation for possible memory issues.
public static Class createClassForProperties(String[] props)
public static Class createClassForProperties(Set keySet)
createBean
.public static Object createBean(Class beanClass, Map propertyMap) throws Exception
beanClass
- a JavaBeans class generated by BeanCreator.createClassForProperties()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |