简体   繁体   中英

invoking as instance method

what is functionality going as follwing

Class c = Class.forName(handler);
Class partypes[] = new Class[1];
partypes[0] = new String().getClass();
Constructor ct = c.getConstructor(partypes);
Object arglist1[] = new Object[1];
arglist1[0] = address;
Method meth[] = c.getMethods();
Object arglist[] = new Object[7]; 
arglist[0] = new Integer(transid);
arglist[1] = transobj;            
arglist[2] = data_vec;            
arglist[3] = company_name;        
arglist[4] = new Boolean(flag_final_level_approval); 
flag_final_level_approval=true else false
arglist[5] = con;                
arglist[6] = scon;              
boolean found = false;
for(int i=0;i<meth.length;i++) {
    Method m = meth[i];
    if(m.getName().equals(functionName)) {
        result_vec = (Vector)m.invoke(ct.newInstance(arglist1),arglist);
    }
}

That looks like an abuse of reflection to cover up the failure to create a proper interface for two interacting Java components to me. If you provide concrete values for the variables in your snippet it might be possible to do further guessing...

Class c = Class.forName(handler);       // get class object for class with name <handler>
Class partypes[] = new Class[1];    
partypes[0] = new String().getClass();      // get class object for string
Constructor ct = c.getConstructor(partypes);    // get constructor of <handler> with signature <handler>(String)
Object arglist1[] = new Object[1]; 
arglist1[0] = address; 
Method meth[] = c.getMethods();         // get method objects from <handler>
Object arglist[] = new Object[7];       // collect a few params
arglist[0] = new Integer(transid);      // collect a few params
arglist[1] = transobj;              // collect a few params
arglist[2] = data_vec;              // collect a few params
arglist[3] = company_name;          // collect a few params
arglist[4] = new Boolean(flag_final_level_approval); // collect a few params
flag_final_level_approval=true else false   // this won't compile
arglist[5] = con;               // collect a few params
arglist[6] = scon;              // collect a few params
boolean found = false; 
for(int i=0;i<meth.length;i++) {
    Method m = meth[i];
    if(m.getName().equals(functionName)) {  // if method with name <functionName> found
        result_vec = (Vector)m.invoke(ct.newInstance(arglist1),arglist);  // invokes method on ct.NewInstance with arglist as param 
    }

Someone is trying to get a class object by name, create an instance of this class and invoke some method with the params from arglist.

An object representin class called handler is created :

Class c = Class.forName(handler);

Then, we search its constructor with one String argument is located :

Class partypes[] = new Class[1];
partypes[0] = new String().getClass();
Constructor ct = c.getConstructor(partypes);

And used in the for loop.

Finally, the methods are iterated looking for one called functionName which is called on a newly created instance :

result_vec = (Vector)m.invoke(ct.newInstance(arglist1),arglist);

All that would be equivalent to writing (with a language such as groovy )

target = new ${handler}
target.${functionName} ( ${transid}, ${transobj}, ${data_vec}, ${company_name}, ${lag_final_level_approval}, ${con}, ${scon});

And like all my peers says, its clearly an abuse of introspection, as by iterating over methods, one may encounter more than one method with correct name, and try to invoke all of them, collecting various execution exceptions and weird results.

Basically, that looks through all the methods in the class, and any time it finds a match, it creates a new instance (that's the ct.newInstance(arglist1) bit) and then invokes that method (that's the m.invoke(..., arglist) bit).

You may find it easier to understand as:

Object instance = ct.newInstance(arglist1);
result_vec = (Vector) m.invoke(instance, arglist);

(Overall, it's pretty ugly code though...)

Someone is using some heinous reflection to call methods on the handler class.

I'd be curious to know why all the arguments being put so laboriously into that array of arguments couldn't have been passed to a handler instance and simply called.

It looks like someone decided to go with a complex implementation to satisfy a (real or imagined) requirement for ultimate flexibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM