透過您的圖書館登入
IP:3.138.33.87
  • 學位論文

達成Java語言外掛機制的輕量級方法

A Light-weighted Approach for Java Language Plug-in

指導教授 : 陳俊良

摘要


Java語言自1990年代初問世以來,至今依舊是程式開發領域的主流語言。相較於更早期的程式語言,Java是一個開放的技術。藉由使用者程式開發的經驗和技術回饋,不斷的改良和擴充,使Java日趨完善。但Java仍不是一個完美的語言,在程式設計風格、程式設計環境和需求方面,都還有可以突破的點。   語言的進步,其中一個方式就是加入新的功能在語言上,由於Java是一個公開的架構,有相當能力的開法者便可以視新功能的需求,對編譯器,文法,格式甚至是虛擬機器,作一定程度的修改。以一個增強版的語言來說,這樣的演進方式存在兩個可以詬病的問題。第一,高門檻,必須具備有足夠對整個語言架構的熟悉度;第二,修改過的架構若未能成為公開標準的語言架構,有鑑於其修改過的獨特架構,就會喪失了"一次編譯,到處執行"的跨平台特性。   針對這種可能存在的缺失,本論文提出一個相對於傳統重量級的做法較為低門檻的輕量級策略,並且具體實做出符合此輕量級策略的一個架構。所謂輕量級的策略,是指在不更動編譯器、類別檔案格式以及虛擬機器等核心重量級技術的情況下,採取外掛式的概念,進而實現增強版的Java語言。而可以視使用者新功能需求選擇的外掛機制,就是新功能存在的位置。   我們實作輕量級策略,是把外掛機制的切入點選擇附加於編譯器。有別於重量級做法是擴充且改寫編譯器,兩者在實作上的方式不同,但是在功能上和意義上是相同的。   在概念上,標準編譯器搭配外掛機制成為一個新的編譯器。我們採取的作法是分工;標準編譯器處理所有規範的工作,而外掛機制則負責處理新功能的工作。做法之一,修改二進位表示法。在標準編譯器產生二進位表示法後,且在二進位表示法在虛擬機器上執行之前,完整保留Java標準架構的情況下,針對二進位表示法做Java功能上的修正。就整體來說,這是一個完整的新Java架構;但就新增的部分來看,就像是第二個編譯器針對使用者寫的程式做編譯的動作,所以概念上,是一種編譯器外掛的意義。   這樣的一個外掛式編譯器,正常運作需要兩個條件:能夠讀寫二進位表示法以及需要有修改二進位表示法的判斷依據和規範來源。利用現有的Java技術-Javac中的類別ClassReader和ClassWriter,以及注釋是滿足上述條件的一組解決手段。   注釋(Annotation)也是一個新的Java功能,注釋被當作是提供可用資訊的程式註解。注釋的存在,並不影響程式本身的運作,但對程式開發者而言,則基本提供的程式碼的閱讀性、提醒以及改進的計畫,進而當作metadata,在Java中可以在編譯期間對編譯器提供資訊甚至是在執行時間使用反射機制獲得資訊,在某些時候還可以拿來當作XML的取代品。.   我們提出一種實現輕量級新語言功能的方式,首先利用ClassReader讀進二進位表示法。再而利用注釋能夠保存自訂的資訊在二進位表示法中的特性,將注釋作為修改二進位表示法的判斷依據和規範來源。最後將修改過的檔案利用ClassWriter寫回二進位表示法。從程式最初的寫法到最後的二進位表示法來看,Java語言的確具備了新的功能。   輕量級架構具備了低門檻達成新語言功能實現的特性。我們選擇C#中的屬性(Property)功能,以我們所提出的輕量級架構和實作方式,使得Java語言同樣具有相同的功能。

並列摘要


To make Java language powerful, we add new features into Java language. Because Java is open source, developers who are familiar with Java technique can modify the compiler, grammar, ClassFileFormat or virtual machine depending on require.  There are two problems existing if we modify the core technique of Java. First, it has high thresholds, because it is hard to translate an idea into a language feature for general programmers. Second, it loses the spirit of “write once, run anywhere”.  In order to avoid this disappearance, this discourse offers a light-weighted strategy which has lower threshold. And we implement an approach according with the light-weighted design. What is called a light-weighted strategy means that we design a plug-in structure but not change the core technique of Java, such as Javac and VM. Plug-in structure is that the position where new features exist is independent of standard Java structure. It is an optional mechanism depending on user’s requirement  The way we choose is to add new features on Javac Traditionally we modify directly Javac to get new Javac with new features. Now we design a plug-in outside Javac, but we can regard them as new Javac in the meaning.  Light-weighted structure needs the implementation to prove, and one of methods is to modify bytecode. Plug-in can be viewed as another compiler. In other words, plug-in is also a part of Javac in the meaning. We take advantage of ClassReader and ClassWriter classes in Javac to access bytecode, and modify bytecode by rules on annotations.  An annotation is a kind of new feature in Java; it is a way of adding metadata to Java source code that can also be available to the programmer at run-time. Many times it is used as an alternative to XML technology.  We offer an implementation which accords with light-weighted structure. First, we read bytecode into internal representation using ClassReader. Then, we modify the internal representation by rules on annotations, because annotations can keep user’s information in bytecode.    Finally, we transform internal representation into bytecode using ClassWriter.  We regard annotations as syntaxes of new features, and final bytecode has new features indeed. So, we can claim that Java language has new features by plug-in.  Finally, we implement properties feature of C# to Java language using the light-weighted structure we offered.

並列關鍵字

Java new feature plug-in annotation bytecode apt Javac

參考文獻


[13] Richard G. Baldwin. Understanding Properties in Java and C#. http://www.developer.com/java/other/article.php/2114451
[5] Joe Darcy and the Expert Group. Java Specification Request 269: Pluggable Annotation Processing API.
[9] Sun Microsystems. Java 2 Platform Standard Edition 5.
[10] Sun Microsystems. Java Platform, Enterprise Edition 5.0.
[11] Joseph A. Bank, Barbara Liskov, and Andrew C. Myers. Parameterized Types for Java.

延伸閱讀