ebook img

Converting Java Programs to Use Generic Libraries Alan AA Donovan PDF

127 Pages·2012·2.46 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Converting Java Programs to Use Generic Libraries Alan AA Donovan

Converting Java Programs to Use Generic Libraries by Alan A. A. Donovan B.A., University of Cambridge, 1996 Submitted to the Department of Electrical Engineering and Computer Science in partial ful(cid:2)llment of the requirements for the degree of Master of Science in Electrical Engineering and Computer Science at the MASSACHUSETTS INSTITUTE OF TECHNOLOGY September 2004 (cid:13)c Alan A. A. Donovan, MMIV. All rights reserved. The author hereby grants to MIT permission to reproduce and distribute publicly paper and electronic copies of this thesis document in whole or in part. Author ............................................................. Department of Electrical Engineering and Computer Science September 10, 2004 Certi(cid:2)ed by ........................................................ Michael D. Ernst Douglas T. Ross Career Development Assistant Professor of Computer Software Technology Thesis Supervisor Accepted by........................................................ Arthur C. Smith Chairman, Department Committee on Graduate Students 2 To three Scientists and Mentors: Peter Grove, Stephen Downes, John Bridle 4 Converting Java Programs to Use Generic Libraries by Alan A. A. Donovan Submitted to the Department of Electrical Engineering and Computer Science on September 10, 2004, in partial ful(cid:2)llment of the requirements for the degree of Master of Science in Electrical Engineering and Computer Science Abstract Java 1.5 will include a type system (called JSR-14) that supports parametric poly- morphism, or generic classes. This will bring many bene(cid:2)ts to Java programmers, notleastbecausecurrentJavapractisemakesheavyuseoflogically-genericclasses, including container classes. TranslationofJavasourcecodeintosemanticallyequivalentJSR-14 sourcecode requires two steps: parameterisation (adding type parameters to class de(cid:2)nitions) andinstantiation(addingthe typeargumentsateach useof aparameterisedclass). Parameterisationneed be done only once for a class, whereasinstantiationmust be performed for each client, of which there are potentially many more. Therefore, this work focuses on the instantiation problem. We present a technique to deter- minesoundandprecise JSR-14 types ateach useof aclass forwhich ageneric type speci(cid:2)cationis available. Ourapproach usesaprecise andcontext-sensitive pointer analysis to determine possible types at allocation sites, and a set-constraint-based analysis (that incorporates guarded, or conditional, constraints) to choose consis- tent types for both allocation and declaration sites. The technique safely handles all features of the JSR-14 type system, notably the raw types (which provide back- ward compatibility) and ‘unchecked’ operations on them. We have implemented our analysis in a tool that automatically inserts type arguments into Java code, and we report its performance when applied to a numberof real-worldJava programs. Thesis Supervisor: Michael D. Ernst Title: Douglas T. Ross Career Development Assistant Professor of Computer Soft- ware Technology 5 6 Acknowledgements Much of this thesis draws on work published in the (forthcoming) proceedings of OOPSLA 2004 [DKTE00]. This work would not have been possible without the support, guidance and col- laboration of my adviser, Michael Ernst. He brought clarity during times of confu- sion, and encouragement at times of despair. He was always available and willing to discuss ideas and problems, and at both blackboard and keyboard, he put an enormous amount of effort into this work. I also owe thanks to my colleagues and neighbours Scott Ananian, Chandra Boyapati, Viktor Kuncak, Patrick Lam, Darko Marinov, Stephen McCamant, Derek Rayside, Martin Rinard, Alex Salcianu for their technical expertise and practical advice; and in particular to my co-author and collaborator Adam Kie(cid:1)zun, for his input at every stage, from brainstorming to writing; and to Matthew Tschantz, for his tireless work on the implementation. I am indebted to David Bacon, David Grove, Michael Hind, Stephen Fink, Rob O’Callahan and Darrell Reimer at IBM Research, for stimulating discussion and for broadening my understanding of the (cid:2)eld. ThisresearchwasfundedinpartbyNSFgrantsCCR-0133580andCCR-0234651, the Oxygen project, and gifts from IBM and NTT. Finally, special thanks are due to two people: to my brother Stephen, without whose encouragement, I would never have started; and to my lover Leila, without whose warmth and support, I would never have (cid:2)nished. 7 8 Contents 1 Introduction 15 1.1 Automatic translation . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.2 Contributionsof this thesis . . . . . . . . . . . . . . . . . . . . . . . . 18 1.3 Synopsis of the algorithm . . . . . . . . . . . . . . . . . . . . . . . . 19 1.4 The structure of this document . . . . . . . . . . . . . . . . . . . . . 21 2 Background: Java with generic types 23 2.1 Parametric polymorphism . . . . . . . . . . . . . . . . . . . . . . . . 23 2.2 JSR-14 syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.3 JSR-14 type system . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.3.1 Type erasure . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.3.2 Homogeneous translation . . . . . . . . . . . . . . . . . . . . 27 2.3.3 Invariant parametric subtyping . . . . . . . . . . . . . . . . . 27 2.3.4 Raw types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.3.5 Unchecked operations . . . . . . . . . . . . . . . . . . . . . . 29 2.3.6 Versions of JSR-14 . . . . . . . . . . . . . . . . . . . . . . . . 32 2.4 Design space for generics in Java . . . . . . . . . . . . . . . . . . . . 33 2.4.1 Homogeneous vs. heterogeneous translation . . . . . . . . . . 33 2.4.2 Constraints on type variables . . . . . . . . . . . . . . . . . . 36 3 Design principles 41 3.1 Soundness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.2 Behaviour preservation . . . . . . . . . . . . . . . . . . . . . . . . . . 42 9 3.3 Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.4 Completeness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.5 Practicality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.6 Success metric: cast elimination . . . . . . . . . . . . . . . . . . . . . 45 3.7 Assumptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4 Allocation type inference 49 4.1 De(cid:2)nitions and terminology . . . . . . . . . . . . . . . . . . . . . . . 50 4.2 Allocation type inference overview . . . . . . . . . . . . . . . . . . . 51 4.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.4 Pointer analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.5 S-uni(cid:2)cation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.5.1 S-uni(cid:2)cation algorithm details . . . . . . . . . . . . . . . . . . 63 4.6 Resolution of parametric types . . . . . . . . . . . . . . . . . . . . . . 65 5 Declaration Type Inference 67 5.1 Type Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.2 De(cid:2)nitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.3 Creating the type constraint system . . . . . . . . . . . . . . . . . . . 73 5.3.1 Ordinary type constraints . . . . . . . . . . . . . . . . . . . . 74 5.3.2 Framing constraints . . . . . . . . . . . . . . . . . . . . . . . 78 5.3.3 Guarded type constraints . . . . . . . . . . . . . . . . . . . . 79 5.3.4 Cast constraints . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.3.5 Allocation Types . . . . . . . . . . . . . . . . . . . . . . . . . 82 5.4 Solving the type constraints . . . . . . . . . . . . . . . . . . . . . . . 83 5.4.1 Closure rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 5.4.2 Dependency graph . . . . . . . . . . . . . . . . . . . . . . . . 85 5.4.3 Deciding guards, assigning types . . . . . . . . . . . . . . . . 86 5.4.4 Lazy vs. eager assignment . . . . . . . . . . . . . . . . . . . . 89 5.5 Join algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 5.5.1 Wildcard types . . . . . . . . . . . . . . . . . . . . . . . . . . 92 10

Description:
mine sound and precise JSR-14 types at each use of a class for which a our analysis in a tool that automatically inserts type arguments into Java code, and .. shows the same program, translated to make use of generic types.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.