to check code for possible generalizations of declared types; to detect the smell of not using suitable abstractions
It is considered good programming practice that variables are typed with interfaces (or abstract classes) rather than concrete classes. For instance, rather than having
HashSet set =
new
HashSet();
one should write
Set set =
new
HashSet();
where Set
is
an interface of HashSet
.
Now Eclipse comes with two standard refactorings, Generalize Declared Type (formerly called Generalize Type) and Use Supertype Where Possible, that provide for automatic replacement of a declared type with an existing supertype. To complement these, we provide a third refactoring, Infer Type, that can compute and insert a new supertype (interface or abstract class) based on a declaration element by automatically extracting precisely the members that are needed from the declaration element in the program. However, all refactorings must be initiated by hand, i.e., the programmer must reckon that the type used in a declaration could be generalized.
The Declared Type Generalization Checker is an auditor that scans the code for declarations whose types could be generalized. For this, it uses one of currently two algorithms, Generalize Declared Type and Infer Type, to compute for every declaration element of a program whether its declared type can be generalized. If so, it issues a warning, as in
which can be resolved by a corresponding Quick Fix, as in
If the declared type is to remain as is,
with no warning shown, a corresponding annotation, called @Sic
(Latin for so!),
can be inserted. This is also offered by the Quick Fix (see
above), but requires the project to build on Java 5.
The algorithm to be used, as well as whether generalization checking is to be performed, can be set in a project's preferences, as in
The Declared Type Generalization Checker plugin is updateable from http://www.fernuni-hagen.de/ps/prjs/DTGC/update/.