Invoking from a protected or private class java: sure that is impossible?
Advertisement
The correct answer is: "depends on the presence or absence of a security manager."
Indeed, through the reflection is actually possible to go to force the constraints of the Java language without excessive difficulty, ammenochè is not just a security manager, the only actor able to really manage and eventually stop these kinds of forces.
But we are now the example that is worth more than many disquisition. Given the following class:
package net.nothing2hide.test; public class ClassWithMethodsToHack { private void sayHello(String name){ System.out.println("Hello " + name + "! You're calling a private method"); } } we want to demonstrate how easily invoke the method from private sayHello ", as I have already advanced to do so heavily use the reflection:
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(); } } } Still no comment '
Comments RSS feed for this post. TrackBack URI
Leave a comment
Nothing2Hide © 2006 All rights reserved.
License | Disclaimer
Close
- Social Web
- Send e-mail







































