java如何获取要反射的方法
作者:ClearCorner
时间:2023-04-26
浏览:0
获取要反射的方法获取反射方法时,有两个方法,getMethod和getDeclaredMethod。classClass{@CallerSensitivepublicMethodgetMethod(Stringname,Class...parameterTypes)throwsNoSuchMethodException,SecurityException{Objects.requireNonNull(name);SecurityManagersm=System.getSecurityManager();i
本文目录
获取要反射的方法
获取反射方法时,有两个方法,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);
}
}
作者最新文章
火山引擎语音服务控制台 火山引擎语音控制台入口在哪里
2026-11-27 14:06
番茄音乐我喜欢的歌单及收藏歌曲查询
2026-11-25 12:18
美图秀秀滤镜效果异常如何解决
2026-11-25 12:15
保存《百度网盘》共享资源的方法
2026-11-25 12:10
百度网盘下载失败1252017解决方法
2026-11-25 12:08
上一篇:
MySQL和Oracle的不同点有哪些
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多


































