Java generics reflect calling method body type reference problems

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.