Search in sources :

Example 1 with InjectBySetter

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());
}
Also used : JsonEntityField(org.nutz.json.entity.JsonEntityField) Field(java.lang.reflect.Field) JENObj(org.nutz.json.meta.JENObj) JsonEntityField(org.nutz.json.entity.JsonEntityField) InjectBySetter(org.nutz.lang.inject.InjectBySetter) JsonEntityFieldMakerImpl(org.nutz.json.impl.JsonEntityFieldMakerImpl) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with InjectBySetter

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;
}
Also used : EjectByGetter(org.nutz.lang.eject.EjectByGetter) InjectBySetter(org.nutz.lang.inject.InjectBySetter)

Aggregations

InjectBySetter (org.nutz.lang.inject.InjectBySetter)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Test (org.junit.Test)1 JsonEntityField (org.nutz.json.entity.JsonEntityField)1 JsonEntityFieldMakerImpl (org.nutz.json.impl.JsonEntityFieldMakerImpl)1 JENObj (org.nutz.json.meta.JENObj)1 EjectByGetter (org.nutz.lang.eject.EjectByGetter)1