use of ognl.NullHandler in project struts by apache.
the class OgnlUtilTest method testCanSetADependentObject.
public void testCanSetADependentObject() {
String dogName = "fido";
OgnlRuntime.setNullHandler(Owner.class, new NullHandler() {
public Object nullMethodResult(Map map, Object o, String s, Object[] objects) {
return null;
}
public Object nullPropertyValue(Map map, Object o, Object o1) {
String methodName = o1.toString();
String getter = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method[] methods = o.getClass().getDeclaredMethods();
System.out.println(getter);
for (Method method : methods) {
String name = method.getName();
if (getter.equals(name) && (method.getParameterTypes().length == 1)) {
Class<?> clazz = method.getParameterTypes()[0];
try {
Object param = clazz.newInstance();
method.invoke(o, param);
return param;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return null;
}
});
Owner owner = new Owner();
Map<String, Object> context = ognlUtil.createDefaultContext(owner);
Map<String, Object> props = new HashMap<>();
props.put("dog.name", dogName);
ognlUtil.setProperties(props, owner, context);
assertNotNull("expected Ognl to create an instance of Dog", owner.getDog());
assertEquals(dogName, owner.getDog().getName());
}
Aggregations