Refactoring for Parameterizing Java Classes


Download Refactoring for Parameterizing Java Classes


Preview text

Refactoring for Parameterizing Java Classes
Adam Kiezun (MIT), Michael D. Ernst (MIT), Frank Tip (IBM), Robert M. Fuhrer (IBM)
1

Parameterization
Goal: migration of Java code to generics Generics (e.g., List) enable creation of type-safe, more reusable classes Parameterization improves formality of specification in lightweight way Libraries and applications must be migrated
Hard to do by hand
2

Parameterization Example

class Wrapper private Cell

{

class Cell {

c;

private Object data;

Object get(){

Object get(){

return c.get();

return data;

}

}

void set(Object t){

void set(Object t){

c.set(t);

data = t;

}

}

boolean in(Object o){ void copyFrom(Cell

return o.equals(get()); data = c.get();

}

}

}

void addTo(Collection

c.add(data);

}

}

c){ c){
3

Parameterization Example

class Wrapper{ private Cell c;

class Cell{

private E2

data;

E1

get(){

E2

get(){

return c.get();

return data;

}

}

void set(E1

t){

void set(E2

t){

c.set(t);

data = t;

}

}

boolean in(Object o){ void copyFrom(Cell c){

return o.equals(get()); data = c.get();

}

}

}

void addTo(Collection c){

c.add(data);

}

}

4

Migration Problem: 2 parts

1. Instantiation – updating clients to use generic libraries, e.g.,

Graph g;

Graph g;

Efficient and accurate tools exist (e.g., Eclipse’s INFER TYPE ARGUMENTS, based on our work): OOPSLA’04, ECOOP’05

2. Parameterization – annotating classes with type parameters, e.g.,

class Graph class Graph

No usable tools exist – generic libraries parameterized by hand. Parameterization subsumes instantiation.

5

Related Work

Constraint-based type inference for OO:

Smalltalk: (Graver-Johnson’89), (Palsberg-Schwartzbach’93)

Java cast verification: (O’Callahan’99), (Wang-Smith’01)

Refactoring using type constraints:

Decoupling classes (TipEtAl’03, SteimannEtAl’06)

Class library migration (BalabanEtAl’05)

Class customization (deSutterEtAl’04)

Generic instantiation:

Context-sensitive analysis (DonovanEtAl’04)

Context-insensitive analysis (FuhrerEtAl’05)

Generic parameterization:

Generalize C methods from operator overloading (RepsSiff’96)

Java methods, unification based (Pluemicke’06)

Start with over-generalizations, reduce imprecision heuristically (Duggan’97), (Donovan’03), (vonDincklageDiwan’04)

Only one implementation (vonDincklageDiwan’04) but incorrect results

(changes program behavior)

6

Type Inference Approach to Parameterization
Type inference using type constraints Type constraints
capture type relationships between program elements additional constraints for behavior preservation (method overriding)
Solution to constraint system is a correct typing of the program (and unchanged behavior)
7

Parameterization Algorithm
1. Generate type constraints for the program Syntax-driven, from source Close the constraint system using additional rules
2. Find types for constraint variables to satisfy all constraints Iterative work-list algorithm Many solutions possible: prefer eliminating more casts
3. Rewrite source code
8

Type Constraints

Notation: α : constraint variable (type of a program element), e.g.: [e] : type of expression e [Ret(A.m)] : return type of method A.m [Param(2, A.m)) : type of the 2nd parameter of A.m String : type constant ? extends [a] : wildcard type upper-bounded by type of a

α ≤ α’ : type constraint (“α is equal to or a subtype of α’ ”)

Examples of type constraints: Assignment: a = b; constraint: [b] ≤ [a]

Method overriding: SubClass.m overrides SuperClass.m:

[Ret(SubClass.m)]

≤ [Ret(SuperClass.m)]

(return types)

[Param(i, SubClass.m)] = [Param(i, SuperClass.m)] (parameters)

9

Context Variables
Given this declaration: class NumCell{
void set(Number p){…} } consider this call: c.set(arg)
What constraint for [arg]?: [arg] ≤ Number no: type of p may change as result of parameterization [arg] ≤ [p] no: type of p may differ for receivers, if NumCell gets parameterized to NumCell
If [c] is NumCell, then [p] is Float
[arg] ≤ I[c]([p]) “type of p in the context of the type of the receiver, c”
10

Preparing to load PDF file. please wait...

0 of 0
100%
Refactoring for Parameterizing Java Classes