Java Generics AdoptionHow New Features are Introduced, Championed, or Ignored
Chris Parnin Georgia Tech
Christian BirdMicrosoft Research
Emerson Murphy-HillNorth Carolina State
Quick refresher
List myList = …;String aStringFromMyList = (String)myList.get(0);
class List{ Object get(int i){…}} Cast (C)
Raw Type R
Quick refresher
List myList = …;String aStringFromMyList = (String)myList.get(0);
class List<T>{ T get(int i){…}}
Generic Type <T>
Quick refresher
List<String> myList = …;String aStringFromMyList = myList.get(0);
class List<T>{ T get(int i){…}}
static <T> T head(List<T> l){return l.get(0);
};
Parameterized Type <P>
Generic Method <M>
but life is not so simple…
public interface Identifiable<T extends Identifier<? extends What>, What> { public T getObjectID(); public Class<? super What> type();}public interface Identifier<T> { public long getID(); public Class<? super T> type();}interface X <SubjectType extends Identifiable, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable>{} static class A< SI extends Identifier<? extends SubjectType>, OI extends Identifier<? extends ObjectType>, SubjectType extends Identifiable<SI, SubjectType>, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable<? extends OI, ? extends ObjectType>> implements X<SubjectType, RelationshipType, ObjectType> {}
public interface Identifiable<T extends Identifier<? extends What>, What> { public T getObjectID(); public Class<? super What> type();}public interface Identifier<T> { public long getID(); public Class<? super T> type();}interface X <SubjectType extends Identifiable, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable>{} static class A< SI extends Identifier<? extends SubjectType>, OI extends Identifier<? extends ObjectType>, SubjectType extends Identifiable<SI, SubjectType>, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable<? extends OI, ? extends ObjectType>> implements X<SubjectType, RelationshipType, ObjectType> {}
“As I sit and stare in stunned horror at this monster”
"Writing generified classes is rocket science"
“Probably more time and brainpower was spent fussing with generics than any other single thing”
why are these people so angry?
As a community, we often don’t formally evaluate our claims after we
put them out into the world.
Outcome?
GoslingIdea
1994
Guy Steele Debate
1998
Java 5Implement
and Release
2004
Developer Usage
2011
To investigate what happened to generics, let’s look at the claims and complaints made
about generics
Did programmers adopt and use generics?
“I suspect we will see a lot of Java 5 code that continues to do things the bad old way as a result and
contend with the mish-mash that results” (2005) – lambda the ultimate
Did programmers adopt and use generics?
“There seems to be some debate over refactoring to utilize java generics within my current team … a holy war going on about what should and should not be done …” – stackoverflow
Will teammates all agree to use generics?
Did programmers adopt and use generics?
Will teammates all agree to use generics?
“Do you want to go back to casting-from-Object in every container access and dealing with the resulting runtime-revealed type errors?” – stackoverflow
Does generics reduce runtime errors?
ClassCastExceptions are rarely seen in bug reports!Caught with local testing
before check-ins, etc…
Does generics reduce casts?
Did programmers adopt and use generics?
Will teammates all agree to use generics?
Does generics reduce casts?
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
Study Setup
500 million LOC
<P> <M> (C) <T> R
Entities of Interest Per Commit Database for Analysis
Lifetime of 20 open-source projects: Before and After generics.
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
532 developer’s generic usage?
No generics!
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
We use generic collections!
We use generics methods or
classes!
Developers with higher commit frequency?
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
Higher ratio of generics collection
usage (42%)
But generic methods or classes stay
about the same.
2002 2004 2006 2008 2010
05
00
10
00
15
00
20
00
25
00
Squirrel−SQL
Date
Typ
e In
tro
du
ctio
ns/
Re
mov
als
GenericRaw
Never used generics
2002 2004 2006 2008 2010
05
00
10
00
15
00
20
00
25
00
Squirrel−SQL
Date
Typ
e In
tro
du
ctio
ns/
Re
mov
als
GenericRaw
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
2002 2004 2006 2008 2010
05
00
10
00
15
00
20
00
25
00
Squirrel−SQL
Date
Typ
e In
tro
du
ctio
ns/
Re
mov
als
GenericRaw
2002 2004 2006 2008 2010
05
00
10
00
15
00
20
00
25
00
Squirrel−SQL
Date
Typ
e In
tro
du
ctio
ns/
Re
mov
als
GenericRaw
Another programmer
starts contributing…
Then a champion emerges!
Then others follow.
The champion even migrates
old code.
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
-
(C)
<P>
Casts (normalized) should decrease with generics.
jEdit
We found no correlation for jEdit
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
-
(C)
<P>
jEdit
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
Squirrel-SQL
(C)
<P>
We found a strong correlation for Squirrel-SQL
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
Casts reduced before developers start to introduce generics.
But, overall, only Squirrel-SQL had a strong correlation, and 6 with moderate correlation.
RQ 1 Adoption
RQ 2Coordination
RQ 3Casts
Other findings
Very little refactoring to generics (typically under 10%)
Half of generics instantiated with just one type
Generics support modest reductions in duplication
90% of generic usage is java.util collections
IDE support has little relation to generic adoption date
Generic methods are rare!
A Metaphor
Regulates Drugs Regulates Programming Features
Monitors Adverse Reactions
Measures treatment effectiveness
Monitors Allergic Reactions
Measures impact and migration costs