com.opensymphony.xwork2.conversion.impl
public class: InstantiatingNullHandler [javadoc |
source]
java.lang.Object
com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler
All Implemented Interfaces:
NullHandler
Provided that the key
ReflectionContextState#CREATE_NULL_OBJECTS is in the action context with a value of true (this key is set
only during the execution of the
com.opensymphony.xwork2.interceptor.ParametersInterceptor ), OGNL expressions
that have caused a NullPointerException will be temporarily stopped for evaluation while the system automatically
tries to solve the null references by automatically creating the object.
The following rules are used when handling null references:
- If the property is declared exactly as a Collection or List , then an ArrayList shall be
returned and assigned to the null references.
- If the property is declared as a Map , then a HashMap will be returned and assigned to the null
references.
- If the null property is a simple bean with a no-arg constructor, it will simply be created using the ObjectFactory#buildBean(java.lang.Class, java.util.Map) method.
For example, if a form element has a text field named
person.name and the expression
person evaluates
to null, then this class will be invoked. Because the
person expression evaluates to a
Person class, a
new Person is created and assigned to the null reference. Finally, the name is set on that object and the overall
effect is that the system automatically created a Person object for you, set it by calling setUsers() and then
finally called getUsers().setName() as you would typically expect.
| Field Summary |
|---|
| public static final String | CREATE_NULL_OBJECTS | |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler Detail: |
public Object nullMethodResult(Map<String, Object> context,
Object target,
String methodName,
Object[] args) {
if (LOG.isDebugEnabled()) {
LOG.debug("Entering nullMethodResult ");
}
return null;
}
|
public Object nullPropertyValue(Map<String, Object> context,
Object target,
Object property) {
if (LOG.isDebugEnabled()) {
LOG.debug("Entering nullPropertyValue [target="+target+", property="+property+"]");
}
boolean c = ReflectionContextState.isCreatingNullObjects(context);
if (!c) {
return null;
}
if ((target == null) || (property == null)) {
return null;
}
try {
String propName = property.toString();
Object realTarget = reflectionProvider.getRealTarget(propName, context, target);
Class clazz = null;
if (realTarget != null) {
PropertyDescriptor pd = reflectionProvider.getPropertyDescriptor(realTarget.getClass(), propName);
if (pd == null) {
return null;
}
clazz = pd.getPropertyType();
}
if (clazz == null) {
// can't do much here!
return null;
}
Object param = createObject(clazz, realTarget, propName, context);
reflectionProvider.setValue(propName, context, realTarget, param);
return param;
} catch (Exception e) {
LOG.error("Could not create and/or set value back on to object", e);
}
return null;
}
|
public void setObjectFactory(ObjectFactory fac) {
this.objectFactory = fac;
}
|
public void setObjectTypeDeterminer(ObjectTypeDeterminer det) {
this.objectTypeDeterminer = det;
}
|
public void setReflectionProvider(ReflectionProvider prov) {
this.reflectionProvider = prov;
}
|