Object result = method.invoke(target, args);这部是干嘛的
时间: 2024-04-02 16:34:47 浏览: 12
这段代码中的Object result = method.invoke(target, args);实际上是执行了代理对象的方法,也就是调用了target对象(即HelloImpl实例)的sayHello方法,并将其返回值赋给了result变量。
在invoke方法中,method参数表示被代理对象的方法,target参数表示被代理对象本身,args参数表示被代理对象方法的参数列表。method.invoke(target, args)的作用是执行被代理对象的方法,参数target指定了该方法是在哪个对象上调用的,args指定了该方法的参数列表。执行完成后,将该方法的返回值赋给了result变量,最后返回该变量。
相关问题
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class HelloProxy implements InvocationHandler { private Object target; public HelloProxy(Object target) { this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Before method " + method.getName()); Object result = method.invoke(target, args); System.out.println("After method " + method.getName()); return result; } public static void main(String[] args) { Hello hello = new HelloImpl(); Hello proxy = (Hello) Proxy.newProxyInstance(hello.getClass().getClassLoader(), hello.getClass().getInterfaces(), new HelloProxy(hello)); proxy.sayHello("world"); } } class HelloImpl implements Hello { public void sayHello(String name) { System.out.println("Hello, " + name); } }
根据这段代码,运行proxy.sayHello("world")的输出将会是:
Before method sayHello
Hello, world
After method sayHello
这是因为这段代码使用了Java动态代理机制,在HelloProxy类的invoke方法中对代理对象的sayHello方法进行了特定的处理。在代理对象的sayHello方法被调用之前会输出"Before method sayHello",在代理对象的sayHello方法被调用之后会输出"After method sayHello"。同时,sayHello方法的实现"Hello, world"也会被输出,因为在invoke方法中调用了target对象(即HelloImpl实例)的sayHello方法。
请帮忙检查一下代码中的错误:package com.enmo.mdi.distributedcomputing.distributed; import java.util.function.Function; import java.lang.reflect.*; public class Algorithm { public Algorithm() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { } private Double sum(Double... args) { Double num = 0.0; for (Double d: args){ num += d; } return num; } private String add(String... args) { StringBuilder str = new StringBuilder(); for (String s: args){ str.append(s); } return str.toString(); } public static Object callMethod(String name, Object... args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class<?> clazz = Class.forName("com.enmo.mdi.distributedcomputing.distributed.Algorithm"); // 获取方法的Method对象 Method method = clazz.getDeclaredMethod(name, Double.class); method.setAccessible(true); // 调用该方法 return method.invoke(null,args); } public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { // sum Double result = (Double) callMethod("sum", 1,2,3,4,5); System.out.println(result); String result1 = (String) callMethod("add", "1", "2", '3'); System.out.println(result1); } }
代码中的问题是:
1. 在调用callMethod()方法时,传入的参数类型不匹配,应该将数字1,2,3,4,5修改为1.0,2.0,3.0,4.0,5.0,即将整型转换为浮点型。对于字符串"3",应该将其改为字符'3'。
2. 在调用callMethod()方法时,获取方法的Method对象时,应该使用Double[].class和String[].class而不是Double.class和String.class,因为参数是可变的数组。
修改后的代码如下:
```java
package com.enmo.mdi.distributedcomputing.distributed;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Algorithm {
public Algorithm() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
}
private Double sum(Double... args) {
Double num = 0.0;
for (Double d: args){
num += d;
}
return num;
}
private String add(String... args) {
StringBuilder str = new StringBuilder();
for (String s: args){
str.append(s);
}
return str.toString();
}
public static Object callMethod(String name, Object... args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> clazz = Class.forName("com.enmo.mdi.distributedcomputing.distributed.Algorithm");
// 获取方法的Method对象
Method method = clazz.getDeclaredMethod(name, Double[].class);
method.setAccessible(true);
// 调用该方法
return method.invoke(null, (Object) args);
}
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// sum
Double result = (Double) callMethod("sum", 1.0, 2.0, 3.0, 4.0, 5.0);
System.out.println(result);
String result1 = (String) callMethod("add", "1", "2", "3");
System.out.println(result1);
}
}
```