java怎么获取反射方法
作者:水悠悠予安
时间:2023-04-27
来源:互联网
浏览: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();
作者最新文章
灵活计算器
2026-09-16 17:45
苹果折叠屏iPhone预计售价是多少
2026-09-14 13:44
OpenAI GPT-6 Astra 自主通关《传送门》:技术原理与实验成本解析
2026-09-08 19:08
苹果与铠侠签署NAND长期供应协议:3-5年长约与不设价格上限背后的供应链战略
2026-09-08 16:58
PDF转PPT操作指南:在线、本地与批量转换及结果核对
2026-09-04 15:04
上一篇:
MySQL存储引擎怎么理解
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















