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);
}
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;
}
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"));
}
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);
}
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());
}
Aggregations