[英]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. 这似乎是滥用反射来掩盖未能为我创建两个交互Java组件的适当接口的失败。 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. 有人试图按名称获取类对象,创建此类的实例,并使用arglist中的参数调用某些方法。
An object representin class called handler
is created : 创建了一个称为
handler
的对象表示类:
Class c = Class.forName(handler);
Then, we search its constructor with one String argument is located : 然后,我们使用一个String参数查找其构造函数:
Class partypes[] = new Class[1];
partypes[0] = new String().getClass();
Constructor ct = c.getConstructor(partypes);
And used in the for loop. 并在for循环中使用。
Finally, the methods are iterated looking for one called functionName
which is called on a newly created instance : 最后,对方法进行迭代,以查找一个在新创建的实例上调用的名为
functionName
:
result_vec = (Vector)m.invoke(ct.newInstance(arglist1),arglist);
All that would be equivalent to writing (with a language such as groovy ) 所有这些都等同于写作(使用诸如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). 基本上,它将遍历该类中的所有方法,并在找到匹配项时创建一个新实例(即
ct.newInstance(arglist1)
位),然后调用该方法(即m.invoke(..., arglist)
位)。
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. 有人正在使用令人发指的反射来调用
handler
类上的方法。
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. 似乎有人决定采用复杂的实现方式来满足(实际或想象中的)最终灵活性的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.