java怎么获取反射方法
作者:水悠悠予安
时间:2023-04-30
浏览:0
1、获取要反射的方法获取反射方法时,有两个方法,getMethod和getDeclaredMethod。classClass{@CallerSensitivepublicMethodgetMethod(Stringname,Class...parameterTypes)throwsNoSuchMethodException,SecurityException{Objects.requireNonNull(name);SecurityManagersm=System.getSecurityManager()
本文目录
1、获取要反射的方法
获取反射方法时,有两个方法,getMethod 和 getDeclaredMethod。
class Class {
@CallerSensitive
public Method getMethod(String name, Class>... parameterTypes)
throws NoSuchMethodException, SecurityException {
Objects.requireNonNull(name);
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// 1. 检查方法权限
checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
}
// 2. 获取方法
Method method = getMethod0(name, parameterTypes);
if (method == null) {
throw new NoSuchMethodException(methodToString(name, parameterTypes));
}
// 3. 返回方法的拷贝
return getReflectionFactory().copyMethod(method);
}
@CallerSensitive
public Method getDeclaredMethod(String name, Class>... parameterTypes)
throws NoSuchMethodException, SecurityException {
Objects.requireNonNull(name);
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// 1. 检查方法是权限
checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
}
// 2. 获取方法
Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
if (method == null) {
throw new NoSuchMethodException(methodToString(name, parameterTypes));
}
// 3. 返回方法的拷贝
return getReflectionFactory().copyMethod(method);
}
}2、在Java5中,提供了for-each循环,从而简化了对数组和集合的循环。Fore-each循环允许您遍历数组而不需要保留传统for循环中的索引,也不需要在使用迭代器时调用while循环中的hasNext方法和next方法来遍历集合。
double[] values = ...;
for(double value : values) {
// TODO: 处理value
}
List valueList = ...;
for(Double value : valueList) {
// TODO: 处理value
} 3、得到当前方法的名字
String methodName = Thread.currentThread().getStackTrace()[
1
].getMethodName();
作者最新文章
立刻MV如何查看立刻MV官方操作指引文档
2026-11-02 14:05
抖音创作平台官网登录入口
2026-11-02 10:38
Xshell连接SSH失败怎么办?5步排查网络、代理与认证问题
2026-10-30 14:30
樱花动漫官方正版官网入口在哪_樱花动漫网址2027最新分享
2026-10-28 19:21
AO3永久可用网址分享 ao3实时访问入口
2026-10-28 13:16
上一篇:
MySQL存储引擎怎么理解
下一篇:
Java中的Predicate是什么?
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































