ニュースレターに登録する更新プログラムが自動的にブログやRSSフィードを受信する!

保護またはプライベートクラスを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"); } } 呼び出している package net.nothing2hide.test; public class ClassWithMethodsToHack { private void sayHello(String name){ System.out.println("Hello " + name + "! You're calling a private method"); } } 

私はすでに大きくするために使用する高度な反射がsayHello 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(); } } }  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(); } } }  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(); } } } * @ 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(); } } } 公開 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(); } } } / /で 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(); } } } ( ) 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(); } } }  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(); } } } 



Nothing2Hide © 2006すべての権利を保有。

ライセンス | 免責事項

閉じる
メールを送る