×

method invoke invoke

Java反射invoke怎么用?如何把method.invoke后的返回值转成数组或集合类型

admin admin 发表于2022-07-11 20:11:40 浏览108 评论0

抢沙发发表评论

Java反射invoke怎么用

在Java反射中使用Invoke,让我给您一个例子,您可以看到它(每个句子中都有评论)

导入java.lang.reflect.Method;

导入java.lang.reflect.constructor;

公共类S {

公共空隙测试(){

system.out.println(“测试”);

}

公共静态void main(字符串args)抛出异常{

//获取S类

class clazz = class.forname(“ s”);

//获取类S类的默认否-CONDRUCT方法

constructor con = clazz.getDeclaredConstructor();

//创建一个s -type实例对象

S S = s(s)con.newinstance();

/第三

方法m = clzz.getDeclaredMethod(“测试”);

//在S级对象中调用公共成员方法TST TST

M.Invoke(s);

}

}

如何把method.invoke后的返回值转成数组或集合类型

object array = method.invoke(obj,新对象{});int length = java.lang.reflect.Array.getLength(数组);列表列表= new ArrayList();对于(int i = 0;一世-invoke

java Method invoke 抛出异常


InvocationTargetException异常由Method.invoke(obj, args...)方法抛出。当被调用的方法的内部抛出了异常而没有被捕获时,将由此异常接收。
示例:
package com.zzj.test.reflect;

public class Reflect {
public void run(int i) throws ZeroException {
B b = new B();
b.run(i);
}
}

class B {
public void run(int i) throws ZeroException {
if (i 《 0) {
throw new ZeroException(“参数不能小于零!“);
}
System.out.println(“参数:“ + i);

}
}

class ZeroException extends Exception {
private static final long serialVersionUID = 1L;

private String detailMessage;

public ZeroException(String detailMessage) {
this.detailMessage = detailMessage;
}

public String getMessage() {
return detailMessage;
}
}
测试:
package com.zzj.test.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
public static void main(String args) {
try {
Class《?》 clazz = Class.forName(“com.zzj.test.reflect.Reflect“);
Method method = clazz.getMethod(“run“, int.class);
method.invoke(clazz.newInstance(), -1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println(“此处接收被调用方法内部未被捕获的异常“);
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
输出:
此处接收被调用方法内部未被捕获的异常
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.zzj.test.reflect.Test.main(Test.java:11)
Caused by: com.zzj.test.reflect.ZeroException: 参数不能小于零!
at com.zzj.test.reflect.B.run(Reflect.java:13)
at com.zzj.test.reflect.Reflect.run(Reflect.java:6)
... 5 more
也可以直接打印目标异常:
package com.zzj.test.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
public static void main(String args) {
try {
Class《?》 clazz = Class.forName(“com.zzj.test.reflect.Reflect“);
Method method = clazz.getMethod(“run“, int.class);
method.invoke(clazz.newInstance(), -1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println(“此处接收被调用方法内部未被捕获的异常“);
Throwable t = e.getTargetException();// 获取目标异常
t.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
输出:
此处接收被调用方法内部未被捕获的异常
com.zzj.test.reflect.ZeroException: 参数不能小于零!
at com.zzj.test.reflect.B.run(Reflect.java:13)
at com.zzj.test.reflect.Reflect.run(Reflect.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.zzj.test.reflect.Test.main(Test.java:11)
-method invoke