援引从一个受保护的或私人的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(); } } } 仍然没有任何评论'
发表评论
关闭
- 社会网络
- 发送电子邮件



























