Search in sources :

Example 1 with NutType

use of org.nutz.lang.util.NutType in project nutz by nutzam.

the class GenericTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws SecurityException, NoSuchFieldException {
    String str = "jk";
    String mobil = "13123132321";
    String json = "{'loginName' :'user1','body' :{'mobile' :'" + mobil + "'}, 'str':'" + str + "'}";
    Type type = new NutType(JsonRequest2.class, Employee2.class, String.class);
    JsonRequest2<Employee2, String> request = (JsonRequest2<Employee2, String>) Json.fromJson(type, json);
    assertEquals(request.body.mobile, mobil);
    assertEquals(request.str, str);
}
Also used : NutType(org.nutz.lang.util.NutType) Type(java.lang.reflect.Type) NutType(org.nutz.lang.util.NutType) Test(org.junit.Test)

Example 2 with NutType

use of org.nutz.lang.util.NutType in project nutz by nutzam.

the class Lang method getGenericsType.

/**
     * 当一个类使用<T,K>来定义泛型时,本方法返回类的一个字段的具体类型。
     *
     * @param me
     * @param type
     */
public static Type getGenericsType(Mirror<?> me, Type type) {
    Type[] types = me.getGenericsTypes();
    Type t = type;
    if (type instanceof TypeVariable && types != null && types.length > 0) {
        Type[] tvs = me.getType().getTypeParameters();
        for (int i = 0; i < tvs.length; i++) {
            if (type.equals(tvs[i])) {
                type = me.getGenericsType(i);
                break;
            }
        }
    }
    if (!type.equals(t)) {
        return type;
    }
    if (types != null && types.length > 0 && type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        if (pt.getActualTypeArguments().length >= 0) {
            NutType nt = new NutType();
            nt.setOwnerType(pt.getOwnerType());
            nt.setRawType(pt.getRawType());
            Type[] tt = new Type[pt.getActualTypeArguments().length];
            for (int i = 0; i < tt.length; i++) {
                tt[i] = types[i];
            }
            nt.setActualTypeArguments(tt);
            return nt;
        }
    }
    return type;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) NutType(org.nutz.lang.util.NutType) GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) NutType(org.nutz.lang.util.NutType) TypeVariable(java.lang.reflect.TypeVariable)

Example 3 with NutType

use of org.nutz.lang.util.NutType in project nutz by nutzam.

the class GenericTest method receiveJsonRequest4Record.

@Test
@SuppressWarnings("unchecked")
public void receiveJsonRequest4Record() {
    Record record = new Record();
    record.put("a", "a");
    record.put("b", 1);
    JsonRequest<Record> request = new JsonRequest<Record>();
    request.setBody(record);
    request.setLoginName("user1");
    request.setPassword("1234");
    request.setVersion("1101");
    request.setUserType(JsonRequest.USER_TYPE_EMPLOYEE);
    String json = Json.toJson(request);
    // System.out.println(json);
    request = (JsonRequest<Record>) Json.fromJson(new NutType(request.getClass(), record.getClass()), json);
// System.out.println(request.getBody().getString("a"));
// System.out.println(request.getBody().getInt("b"));
}
Also used : NutType(org.nutz.lang.util.NutType) Record(org.nutz.dao.entity.Record) Test(org.junit.Test)

Example 4 with NutType

use of org.nutz.lang.util.NutType in project nutz by nutzam.

the class GenericTest method test2.

@SuppressWarnings("unchecked")
@Test
public void test2() {
    String str = "jk";
    String mobil = "13123132321";
    String json = "{'loginName' :'user1','body' :{'mobile' :'" + mobil + "'}, 'str':{'mobile' :'" + str + "'}}";
    Type type = new NutType(JsonRequest2.class, Employee2.class, Employee2.class);
    JsonRequest2<Employee2, Employee2> request = ((JsonRequest2<Employee2, Employee2>) Json.fromJson(type, json));
    assertEquals(request.body.mobile, mobil);
    assertEquals(request.str.mobile, str);
}
Also used : NutType(org.nutz.lang.util.NutType) Type(java.lang.reflect.Type) NutType(org.nutz.lang.util.NutType) Test(org.junit.Test)

Example 5 with NutType

use of org.nutz.lang.util.NutType in project nutz by nutzam.

the class GenericTest method receiveJsonRequest.

@Test
@SuppressWarnings("unchecked")
public void receiveJsonRequest() {
    Employee user = new Employee();
    user.setMobile("13123132321");
    JsonRequest<Employee> request = new JsonRequest<Employee>();
    request.setBody(user);
    request.setLoginName("user1");
    request.setPassword("1234");
    request.setVersion("1101");
    String json = Json.toJson(request);
    // System.out.println(json);
    request = (JsonRequest<Employee>) Json.fromJson(new NutType(request.getClass(), Employee.class), json);
// System.out.println(request.getBody().getMobile());
}
Also used : NutType(org.nutz.lang.util.NutType) Test(org.junit.Test)

Aggregations

NutType (org.nutz.lang.util.NutType)5 Test (org.junit.Test)4 Type (java.lang.reflect.Type)3 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 TypeVariable (java.lang.reflect.TypeVariable)1 WildcardType (java.lang.reflect.WildcardType)1 Record (org.nutz.dao.entity.Record)1