淺談Java編程中的synthetic關(guān)鍵字
java synthetic關(guān)鍵字。有synthetic標(biāo)記的field和method是class內(nèi)部使用的,正常的源代碼里不會(huì)出現(xiàn)synthetic field。小穎編譯工具用的就是jad.所有反編譯工具都不能保證完全正確地反編譯class。所以你不能要求太多。
下面我給大家介紹一下synthetic
下面的例子是最常見(jiàn)的synthetic field
Java代碼
class parent {
public void foo() {
}
class inner {
inner() {
foo();
}
}
}
非static的inner class里面都會(huì)有一個(gè)this$0的字段保存它的父對(duì)象。編譯后的inner class 就像下面這樣:
Java代碼
class parent$inner{
synthetic parent this$0;
parent$inner(parent this$0)
{
this.this$0 = this$0;
this$0.foo();
}
}
所有父對(duì)象的非私有成員都通過(guò) this$0來(lái)訪問(wèn)。
還有許多用到synthetic的地方。比如使用了assert 關(guān)鍵字的class會(huì)有一個(gè)synthetic static boolean $assertionsDisabled 字段
使用了assert的地方
assert condition;
在class里被編譯成
Java代碼
if(!$assertionsDisabled && !condition){
throw new AssertionError();
}
還有,在jvm里,所有class的私有成員都不允許在其他類(lèi)里訪問(wèn),包括它的inner class。在java語(yǔ)言里inner class是可以訪問(wèn)父類(lèi)的私有成員的。在class里是用如下的方法實(shí)現(xiàn)的:
Java代碼
class parent{
private int value = 0;
synthetic static int access$000(parent obj)
{
return value;
}
}
在inner class里通過(guò)access$000來(lái)訪問(wèn)value字段。
synthetic的概念
According to the JVM Spec: "A class member that does not appear in the source code must be marked using a Synthetic attribute." Also, "The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces."
I know that nested classes are sometimes implemented using synthetic fields and synthetic contructors, e.g. an inner class may use a synthetic field to save a reference to its outer class instance, and it may generate a synthetic contructor to set that field correctly. I'm not sure if it Java still uses synthetic constructors or methods for this, but I'm pretty sure I did see them used in the past. I don't know why they might need synthetic classes here. On the other hand, something like RMI or java.lang.reflect.Proxy should probably create synthetic classes, since those classes don't actually appear in source code. I just ran a test where Proxy did not create a synthetic instance, but I believe that's probably a bug.
Hmm, we discussed this some time ago back here. It seems like Sun is just ignoring this synthetic attribute, for classes at least, and we should too.
注意上文的第一處黑體部分,一個(gè)類(lèi)的復(fù)合屬性表示他支持嵌套的類(lèi)或者接口。
注意上文的第二處黑體部分,說(shuō)明符合這個(gè)概念就是OO思想中的類(lèi)的復(fù)合,也就是只要含有其它類(lèi)的引用即為復(fù)合。
總結(jié)
以上就是本文關(guān)于Java編程中的synthetic關(guān)鍵字的全部?jī)?nèi)容,希望對(duì)大家能有所幫助。感謝大家對(duì)本站的支持。
相關(guān)文章
8個(gè)Spring事務(wù)失效場(chǎng)景詳解
相信大家對(duì)Spring種事務(wù)的使用并不陌生,但是你可能只是停留在基礎(chǔ)的使用層面上。今天,我們就簡(jiǎn)單來(lái)說(shuō)下Spring事務(wù)的原理,然后總結(jié)一下spring事務(wù)失敗的場(chǎng)景,并提出對(duì)應(yīng)的解決方案,需要的可以參考一下2022-12-12
Springboot使用異步請(qǐng)求提高系統(tǒng)的吞吐量詳解
這篇文章主要介紹了Springboot使用異步請(qǐng)求提高系統(tǒng)的吞吐量詳解,和同步請(qǐng)求相對(duì),異步不需要等待響應(yīng),隨時(shí)可以發(fā)送下一次請(qǐng)求,如果是同步請(qǐng)求,需要將信息填寫(xiě)完整,再發(fā)送請(qǐng)求,服務(wù)器響應(yīng)填寫(xiě)是否正確,再做修改,需要的朋友可以參考下2023-08-08
JavaSE實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的示例代碼
這篇博客是在學(xué)習(xí)了一部分Java基礎(chǔ)語(yǔ)法之后的練習(xí)項(xiàng)目,通過(guò)這個(gè)小項(xiàng)目的練習(xí),對(duì)Java中的類(lèi)和對(duì)象,抽象類(lèi)和接口等進(jìn)行熟悉理解??旄S小編一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java中?SLF4J和Logback和Log4j和Logging的區(qū)別與聯(lián)系
這篇文章主要介紹了Java中?SLF4J和Logback和Log4j和Logging的區(qū)別與聯(lián)系,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考幾種,感興趣的小伙伴可以參考一下2022-09-09
Idea中mapper注入報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Idea中mapper注入報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
解決SpringBoot連接SqlServer出現(xiàn)的問(wèn)題
在嘗試通過(guò)SSL與SQL?Server建立安全連接時(shí),如果遇到“PKIX?path?building?failed”錯(cuò)誤,可能是因?yàn)槲茨苷_配置或信任服務(wù)器證書(shū),當(dāng)"Encrypt"屬性設(shè)置為"true"且"trustServerCertificate"屬性設(shè)置為"false"時(shí),要求驅(qū)動(dòng)程序使用安全套接字層(SSL)加密與SQL?Server建立連接2024-10-10

