SAM interface/class figures [Re: Preparing for the 0.2 draft]
Stephen Colebourne
scolebourne at joda.org
Fri Jan 29 08:12:13 PST 2010
1.4MLOC e-commerce web application
Total anon inner classes: 185
- implementing SAM interface: 62
- extending SAM class: 100
- extending non-SAM class overriding one method: 15
- extending non-SAM class overriding more than one method: 8
(133 anonymous inner classes in testing code not analysed)
Of the 100 SAM classes, there were two types
- 95 were subclasses of a lazy evaluator List, where the abstract
method was called to avoid up front list creation
- 5 were subclasses of a generator, where the abstract method generated data
Both were examples of classes that should have delegated to a SAM
interface/lambda rather than being a SAM class (ie. bad design). The
group of 95 all reference "OuterClass.this" (directly or indirectly).
Only 2 of these reference a field in the superclass, and they
shouldn't do (bad design).
Of the 15 one method non-SAM superclass ones, 8 were subclasses of
LinkedHashMap to override removeEldestEntry(). This suggests a JDK API
change would be useful here.
The breakdown of SAM interfaces was
- Comparator: 12
- FilenameFilter: 6
- Callable: 13
- Runnable: 8
- ThreadFactory: 9
- filters: 6
- generators: 6
- other: 2
I checked the following for non-anonymous class implementations:
- Comparator: 18
- FIlenameFIlter: 1
- Callable: 4
- Runnable: 8
- ThreadFactory: 0
- commons.collections.Transformer: 8
- Thread: 20
- TimerTask: 1
There were other one method interfaces implemented by classes (not
anon inner classes), but these interfaces wouldn't make sense as a
lambda.
Thus:
> - What %age of anonymous inner classes declare a single non-Object method?
95.7%
> - What %age of single-method anonymous inner classes declare fields?
0%
> - What %age of single methods in anonymous inner classes use 'this'?
1.1%
- What %age of single methods in anonymous inner classes use 'Outer.this'?
51.4% (entirely due to the 95 classes of one pattern mentioned above)
> - What %age of single methods in anonymous inner classes recurse?
0%
> - Are the %ages different for anonymous inner classes that implement an interface v. that extend a class ?
Yes. Generally if they are a named/top-level class then they will have
private methods or lots of other functionality. The anonymous inner
classes always are a few lines.
Overall, I'd say that lambda being solely SAM interfaces would cause
no problem to this codebase, and would in fact have encouraged good
development practice (ie. the 100 SAM class subclasses would almost
certainly have been written to delegate to a lambda rather than
subclass a bulky parent class and override a method.
Stephen
More information about the lambda-dev
mailing list