use of org.nutz.lang.inject.InjectBySetter in project nutz by nutzam.
the class JsonEntityTest method test_entity_field_maker.
@Test
public void test_entity_field_maker() {
Json.clearEntityCache();
JENObj obj = new JENObj();
obj.setAge(100);
obj.setName("name");
obj.setObjId(9l);
Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {
@Override
public JsonEntityField make(Mirror<?> mirror, Method method) {
return null;
}
@Override
public JsonEntityField make(Mirror<?> mirror, Field field) {
return null;
}
});
assertEquals("{}", Json.toJson(obj, JsonFormat.compact()));
Json.clearEntityCache();
Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {
@Override
public JsonEntityField make(Mirror<?> mirror, Method method) {
if (method.getName().equals("setName")) {
String fn = Strings.lowerFirst(method.getName().substring(3));
return JsonEntityField.eval("another_name", method.getParameterTypes()[0], mirror.getEjecting(fn), new InjectBySetter(method));
}
return null;
}
@Override
public JsonEntityField make(Mirror<?> mirror, Field field) {
return JsonEntityField.eval("test_" + field.getName(), field.getType(), mirror.getEjecting(field), mirror.getInjecting(field.getName()));
}
});
String json = Json.toJson(obj, JsonFormat.compact());
assertTrue(json.contains("\"test_objId\":9"));
assertTrue(json.contains("\"test_name\":\"name\""));
assertTrue(json.contains("\"test_age\":100"));
assertTrue(json.contains("\"another_name\":\"name\""));
Json.clearEntityCache();
Json.setDefaultFieldMaker(new JsonEntityFieldMakerImpl());
}
use of org.nutz.lang.inject.InjectBySetter in project nutz by nutzam.
the class JsonEntityField method eval.
/**
* 根据名称获取字段实体, 默认以set优先
*/
public static JsonEntityField eval(String name, Method getter, Method setter) {
JsonEntityField jef = new JsonEntityField();
jef.genericType = getter.getGenericReturnType();
jef.name = name;
jef.ejecting = new EjectByGetter(getter);
jef.injecting = new InjectBySetter(setter);
return jef;
}
Aggregations