Does the MVEL Null-Safe operator work on methods? -
i have question regarding mvel null-safe (?
) operator.
say have following java class:
public class traveler { private set<string> visitedcountries; public set<string> getvisitedcountries() { return visitedcountries; } }
if have mvel expression this:
traveler.visitedcountries.contains("france")
i nullpointerexception
if visitedcountries
field null. around this, can use null-safe operator:
traveler.?visitedcountries.contains("france")
if visitedcountries
null, expression evaluates null instead of throwing npe.
my question this: null-safe operator work on method invocations? example:
traveler.getvisitedcountries().contains("france")
will throw nullpointerexception
if getvisitedcountries()
returns null.
but happens if put in null-safe operator? following if field visitedcountries
null?
traveler.?getvisitedcountries().contains("france")
as turns out, expression
traveler.?getvisitedcountries().contains("france")
does observe null-safe operator. evaluate null here. unit test:
@test public void testmvelnullsafeonmethod() { traveler traveler = new traveler(); // traveler visitedcountries field null string expression = "traveler.getvisitedcountries().contains(\"france\")"; serializable exp = mvel.compileexpression(expression); map<string, object> map = new hashmap<string, object>(); map.put("traveler", traveler); boolean response = (boolean) mvel.executeexpression(exp, map); asserttrue(response == null || !response); }
Comments
Post a Comment