陳俊良臺灣大學:資訊工程學研究所關尚儒Guan, Shang-RuShang-RuGuan2007-11-262018-07-052007-11-262018-07-052007http://ntur.lib.ntu.edu.tw//handle/246246/54157Java語言是個典型的物件導向程式語言。Java語言從出現時所帶來的可攜性、動態載入等等特性就受到強烈的注目。在其使用者與日俱增的同時,其語言特性也在以不影響其相容性、可攜性的原則之下快速的成長。Java語言在1.5(5.0)版開始正式在SDK中加入了泛型語法。其目的在於讓程式員在使用或是自定義容器類別時達到程式碼覆用以及編譯時期型別檢察等優點。 然而,Java語言在兼顧相容性和程式執行負擔的考量下使用了”一般化方法(Generalization)”來實現其泛型語法。此方法相較於C++語言使用的”特別化方法(Specialization)”,在執行時期無法達到真正的參數化多型。一個泛型類別的Java程式碼會產生一個泛型類別檔,而執行時期此類別之執行時期結構將會是唯一一份的存在,並不會因為其型別參數被給予引數的不同而產生多份結構。如此將會帶來許多限制以及危險性。 針對此現象,有相關研究提出了相當徹底的解決辦法,就是將Java的泛型語法實現方式想辦法改變為特別化方法,以避免掉所有一般化方法產生的問題。然而此解法必須修改Java虛擬機器才能實現,犧牲了相容性,也違背了泛型爪哇(Generic Java)的基本原則。這篇論文針對Java泛型語法所帶來的型別安全之缺陷,在不違背一般化方法實現以及泛型爪哇的原則之下,提出一個兼顧相容性的解法。藉由編譯器以及類別檔的修改。將執行時期泛型類別所產生的物件保有自己的型別引數(或稱之為泛型類別的Actual Type)資訊。藉此資訊可以提供Java反射機制的補完以及型別安全性的檢查Java language is a typical object-oriented computer language. Programmers have gotten convenience in container declaration and usage since generics was supported by JDK5.0. As a result of using generalization to implement the generics support of new Java compiler. There could be difference between the source code and the real semantic in its class file. Java core reflection is also incomplete. The primal reason is that Java Virtual Machine doesn’t support parameterized type. In this thesis we bring up a light weight solution for parameterized type checking at runtime. It can also support runtime parameterized type reflection. This solution can work without modifying Java Virtual Machine.中文摘要 i ABSTRACT iii 致謝 v Index vii Index of Tables ix Index of Figures xi Chapter 1 - INTRODUCTION 1 1-1 The origin of generics 1 1-2 Concept of generics 1 1-3 Development of JAVA language 2 1-4 Motivation 4 1-5 Related Work 4 1-6 Thesis Organization 4 Chapter 2 - Parametric Polymorphism in Java 7 2-1 Scenario 7 2-2 Terminologies 8 2-3 Comparison with non-generic programs 9 2-4 Specialization and Generalization 12 2-4-1 Generalization 12 2-4-2 Specialization 13 2-5 Type Erasure 14 2-6 Delay Checking Problem 16 2-7 Goals 18 Chapter 3 - Strategy 19 3-1 Where to store actual type information 22 3-2 When to know actual type information 24 3-3 How to set actual type information 26 3-3-1 PTCM.setAT() 27 3-3-2 PTCM.getAT() 27 3-3-3 PTCM.CheckCast() 28 3-4 How to use actual type information 29 Chapter 4 - Implementation 31 4-1 Attachment of instance variable to a generated class 31 4-2 Attachment of instance variable to a class file in existence 32 4-3 Replacement of generic new expressions on syntax tree 33 4-4 Replacement of parameterized type casting on syntax tree 36 4-5 Throw of exception with the correct line number 37 Chapter 5 - Conclusion and Future Works 39 5-1 Conclusion 39 5-2 Future Works 43 Reference 455328202 bytesapplication/pdfen-US爪哇泛型參數化多型反射爪哇編譯器一般化方法Javagenericsparametric polymorphismreflectionJavacgeneralization一個精確的Java語言型別檢查機制A Precise Parameterized Pype Phecking Mechanism for Javathesishttp://ntur.lib.ntu.edu.tw/bitstream/246246/54157/1/ntu-96-R94922088-1.pdf