questions about the Thrift Service Automation test from a writing automation test tool
First, let's show you a code .
Java Code Preservation Code
Public static void Test (listlist) {
System.out.println (list.get (0));
System.out.println (list.get (0) + 123);
System.out.println (list.get (0). GetClass ());
}
Public static void BugTest1 () {
Australian Health Care http://www.eget.com.au
Guoyi, http://www.tdhzp.com
number Daquan http://www.haomad.com
Linyi buy http://www.shoudashou.com
String json = "[50004172,926513792]";
For (Method m:thriftgenutils.class.getmethods ()) {
if ("Test". Equals (M.getname ())) {
For (Class pclass:m.getparametertypes ()) {
Object argcasted = Json.strtoobj (Json, pClass);
try {
M.invoke (New Thriftgenutils (), argcasted);
} catch (Illegalaccessexception e) {
e.printstacktrace ();
} catch (InvocationTargetException e) {
e.printstacktrace ();
}
}
}
}
}
Operation BugTest1
Questions:
1 argcasted Verbose type is list, in which type is the generic detail type? Why?
2 can invoke succeed?
3 If Invoke succeeds, what type is the generic detail type of the list? Can test run successfully? If you can't run successfully, which line is wrong? What is the fault of the report? Why?
Answer:
1 integer,m.getparametertypes () gets to the generic type that does not include the verbose type, Also that is json.strtoob call of cent pclass only pass the list,json transformation of cent only know is data type, tacit conversion to integer, floating point to double.
2 is
3 Integer, not successful, to 219-row Report type transformation An anomalous Integer cannot be transformed to a long type because of a generic type erasure. (Generic type erase: http://blog.csdn.net/caihaijiang/article/details/6403349)
the current solution is to get to the generic type and the detail type, passing objectmapper.gettypefactory (). Constructparametrictype (clazz,genericclazzes) To get javatype and change it to object object.
after the transformation, complete the relevant code as follows
Java Code Preservation Code
method = null;
listarglist = new arraylist<> ();
For (Method m:clientclazz.getmethods ()) {
if (!methodname.equals (M.getname ())) {continue;}
method = m;
type[] types = M.getgenericparametertypes ();
class[] classes = M.getparametertypes ();
int i = 0;
For (Type c:types) {
int index = i++;
String arg = Args[index];
if (! ( C instanceof Parameterizedtype)) {
String jsonstr = json.objtostr (ARG);
Arglist.add (Json.strtoobj (Jsonstr,classes[index));
continue;
}
Parameterizedtype pt = (parameterizedtype) C;
Objectmapper mapper = new Objectmapper ();
class[] genericclazzes = new class[pt.getactualtypearguments (). length];
int j = 0;
For (Type type:pt.getActualTypeArguments ()) {
Genericclazzes[j + +] = (Class) type;
}
Javatype javatype = Mapper.gettypefactory (). Constructparametrictype (classes[index],genericclazzes);
Object argcasted = Mapper.readvalue (arg,javatype);
Arglist.add (argcasted);
}
Break ;
}
if (Method! = null) {
res = Method.invoke (Obj,arglist.toarray ());
}
Java generics reflect calling method body type reference problems