订阅通讯或简易信息聚合饲料的接收自动更新博客!

援引从一个受保护的或私人的Java :肯定是不可能的?



正确答案是: “取决于是否存在一个安全经理。 ”

事实上,通过反射实际上是有可能去武力的限制, Java语言没有过多的困难, ammenochè不仅是一个安全经理,唯一的演员能够真正管理并最终停止这种类型的部队。

但是,我们现在的例子是,价值超过许多本届。 鉴于以下类别:

 package net.nothing2hide.test; public class ClassWithMethodsToHack { private void sayHello(String name){ System.out.println("Hello " + name + "! You're calling a private method"); } } 

我们要轻松地演示了如何调用该方法从私人sayHello “ ,因为我已经先进这样做的大量使用反映:

 package net.nothing2hide.test; public class PrivateMethodInvocationTest { /** * This sample class demonstrates how to * invoke a protected method on another class * * @param args */ public static void main(String[] args) { String myName = "Davide"; Method m; try { //Let'access the private method //by reflection. You could do the same //with a protected method m = ClassWithMethodsToHack.class.getDeclaredMethod( "sayHello", new Class[]{String.class}); if (m != null) { //Let's force the java language access //checks on the reflected method. Note //that if a security manager is defined //this workaround could not be permitted //and a SecurityException could be raised m.setAccessible(true); //And finally let's invoke the protected method m.invoke(new ClassWithMethodsToHack(), new Object[]{myName}); } } catch (Exception e) { e.printStackTrace(); } } } 


你喜欢本文? 立即注册获得更新或新闻文章:
订阅RSS源 写信给RSS种子


仍然没有任何评论'

评论RSS资讯提供这一职位。 引用的URI

发表评论

兼容: href="" title=""> 的<a <abbr title=""> <acronym title="">的<b> <blockquote cite=""> <cite>的<code> <del datetime="">的<em> <i> <q cite=""> <strike>的<strong>


Nothing2Hide © 2006保留所有权利。

许可 |免责条款

关闭
发送电子邮件