/*
有synthetic標記的field和method是class內部使用的,
正常的源代碼里不會出現synthetic field。小穎編譯工具用的就是jad.
所有反編譯工具都不能保證完全正確地反編譯class。所以你不能要求太多。
下面我給大家介紹一下 synthetic
下面的例子是最常見的 synthetic field
*/
class parent{
public void foo(){
System.out.println("this is foo");
}
class inner{
inner(){
foo();
}
}
}
/*
非static的inner class里面都會有一個this$0的字段保存它的父對象。編譯后的inner class 就像下面這樣:
class parent$inner{
synthetic parent this$0;
parent$inner(parent this$0){
this.this$0 = this$0;
this$0.foo();
}
}
所有父對象的非私有成員都通過 this$0來訪問。
還有許多用到synthetic的地方。比如使用了 assert 關鍵字的class會有一個
synthetic static boolean $assertionsDisabled 字段
使用了assert的地方
assert condition;
在class里被編譯成
if(!$assertionsDisabled && !condition){
throw new AssertionError();
}
還有,在jvm里,所有class的私有成員都不允許在其他類里訪問,包括它的inner class。
在java語言里inner class是可以訪問父類的私有成員的。在class里是用如下的方法實現的:
class parent
{
private int value = 0;
synthetic static int access$000(parent obj){
return value;
}
}
在inner class里通過access$000來訪問value字段。
希望通過上面幾個例子,大家對synthetic 有所了解。
*/
posted on 2007-10-19 11:07
lk 閱讀(833)
評論(0) 編輯 收藏 所屬分類:
j2se