You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Julio C. Rivera" <ju...@ya.com> on 2003/08/30 13:26:16 UTC
Nulls in OGNL expressions
Hi!
I have a problem using OGNL.
When I access to, for instance, expr = prop1.prop2.prop3 I want
if (prop1 == null) then expr == null
if ((prop1 != null) && (prop2 == null)) then expr == null
if ((prop1 != null) && (prop2 != null) && (prop3 == null)) then expr ==
null
In a nutshell, if any property in the graph is null, then it returns null
(NEVER a NullPointException).
I have a NullHandler like the following:
public class DefaultNullHandler extends ObjectNullHandler {
public Object nullPropertyValue(Map context,Object target,Object
property) {
return new HashMap();
}
public java.lang.Object nullMethodResult(Map context,Object
target,String methodName,List args) {
return new HashMap();
}
}
It works fine if I ask for prop1.prop2 (and prop1 is null), but if I do
that, when I ask for prop1 property, and prop1 is null, I get a empty Map,
when I would like to get a null.
A NullHandler like the following solves my problem:
public class DefaultNullHandler extends ObjectNullHandler {
public Object nullPropertyValue(Map context,Object target,Object
property) {
if (property is the last property in the expression chain) <-- HOW
CAN I DO IT?
return null
else return new HashMap();
}
public java.lang.Object nullMethodResult(Map context,Object
target,String methodName,List args) {
if (methodName is the last method in the expression chain) <-- HOW
CAN I DO IT?
return null
else return new HashMap();
}
}
Any suggestion?
( I think would be useful a "NeverNullPointerExceptionNullHandler" in OGNL.
I think its a common need.)
Thanks in advance.
Julio.