JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 技巧文件  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 private is no more private
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-04-15 00:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Without using JNI; you could use the java reflect API to access private, protected, or package access methods.

Here is a quick example that shows you how to do it:

1
2
3
4
5
6
package b;
public class B {
    protected void protected_method()  { System.out.println("protected"); }
    private void private_method()      { System.out.println("private");   }
    void package_method()              { System.out.println("package");   }
}


Here is the class A in the package a try to access class B in the package b:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package a;
  
import b.B;
import java.lang.reflect.*;
 
public class A {
    public static void main(String[] args) {
        try {
            B b = new B();
            //b.protected_method(); // ==> compile error
            //b.private_method();   // ==> compile error
            //b.package_method();   // ==> compile error
            
            Method protected_method = B.class.getDeclaredMethod("protected_method", new Class[] {});
            protected_method.setAccessible(true);
            protected_method.invoke(b, new Object[] {});
 
            Method private_method = B.class.getDeclaredMethod("private_method", new Class[] {});
            private_method.setAccessible(true);
            private_method.invoke(b, new Object[] {});
 
            Method package_method = B.class.getDeclaredMethod("package_method", new Class[] {});
            package_method.setAccessible(true);
            package_method.invoke(b, new Object[] {});            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Using reflection, you could access any methods you want!
==> private is no more private. Wink
(不過, 私人的東西最好不要亂拿, 要不然, 後果自負吧.)


T55555 edited on 2004-04-15 00:55
reply to postreply to post
作者 Re:private is no more private [Re:T55555]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2004-04-15 01:17 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
T55555 wrote:
Without using JNI; you could use the java reflect API to access private, protected, or package access methods.


這裡有一篇離題的 thread,講的是同樣的事:
http://www.javaworld.com.tw/jute/post/view?bid=25&id=3694&sty=3&age=0&tpg=1&ppg=1#3694
(寫這一篇的該打屁股 Big Smile)

以及,我的 Qoo~~~
http://www.javaworld.com.tw/jute/post/view?bid=25&id=11164&sty=3&age=0&tpg=1&ppg=1#11164

祭出 JNI,final 都不 final 了,更甭提 private member。


Duncan edited on 2004-04-15 01:48
reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:private is no more private [Re:T55555]
Biologic

生物學下的產物



發文: 524
積分: 4
於 2004-04-15 09:42 user profilesend a private message to usersend email to Biologicreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
setAccessible 會呼叫 SecurityManager 比較權限
"ReflectPermission("suppressAccessChecks")".

只要啟動 security manager 且不受於你 ReflectPermission("suppressAccessChecks") 的權限, 你也沒有辦法使用....

JNI 的 binary library loading 一樣有受控管的.


reply to postreply to post
» JWorld@TW »  Java 技巧文件

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8