[英]How to make Class.getMethod() throw a SecurityException
我有一个实用工具方法来查找特定字段的对象的getter方法(使用反射):
public static Method findGetter(final Object object, final String fieldName) {
if( object == null ) {
throw new NullPointerException("object should not be null");
} else if( fieldName == null ) {
throw new NullPointerException("fieldName should not be null");
}
String getterName = getMethodNameForField(GET_PREFIX, fieldName);
Class<?> clazz = object.getClass();
try {
return clazz.getMethod(getterName);
} catch(final NoSuchMethodException e) {
// exception handling omitted
} catch(final SecurityException e) {
// exception handling omitted
}
}
我想编写一个涵盖SecurityException场景的单元测试,但是如何让getMethod抛出SecurityException呢?
javadoc声明getMethod
将抛出SecurityException
如果存在安全管理器s,并且满足以下任何条件:
调用s.checkMemberAccess(this,Member.PUBLIC)拒绝访问该方法
调用者的类加载器与当前类的类加载器的祖先或祖先不同,并且调用s.checkPackageAccess()拒绝访问此类的包
我宁愿通常触发异常,而不是诉诸于模拟框架。
System.setSecurityManager(new SecurityManager(){
@Override
public void checkMemberAccess(Class<?> clazz, int which) {
throw new SecurityException("Not allowed")
}
@Override
public void checkPermission(Permission perm) {
// allow resetting the SM
}
});
ClassTest.class.getMethod("foo");
请记住在finally
块中调用System.setSecurityManager(null)
以恢复原始状态。
至于在常规环境中造成这种情况,很少会出现这种情况。
非公共方法将导致NoSuchMethodException
,因为getMethod
仅专门查找公共方法。 getDeclaredMethod
将找到该方法,但允许返回私有/受保护的方法。
唯一真正的方法是,正如javadoc所说,有一个受安全管理器保护的独立包,我猜测只能用未签名的applet或类似的权限来完成。 (我自己从来没有碰到这个,所以我不完全确定)
最好的办法是通过覆盖安全管理器强制为特定方法抛出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.