For C++ and C# programmers, the weakness of Java’s Generics is pretty clear.
For the others, though, maybe a little recap of what we’re missing is needed :
- speed : heard about GNU Trove’s primitive collections ? They are generated from home-made class templates and are much faster than Sun’s collection, because they do not wrap primitives into objects.
- code reusability and factorization : it is a pain in the arse to use primitive arrays in current generics (T can be cast to Object, not to int !).
- code generation : we have no macros in Java. No way to automate the creation of dummy methods, even when the code to do so would seem obvious.
After quite a while lamenting about the weakness of Java’s generics when compared to C#’s generics or even to the much, much more powerful C++ templates, I decided to act.
I started a project on Google Code with a few constraints and objectives :
- never give up on standard Eclipse & Netbeans autocompletion and general interactivity.
- target Java 1.5
- from the same source, be able to do reified generics and specialized instantiated templates, in the GNU Trove-style.
- no autogenerated code cluttering in the same window you’re typing into. Any generated code should be hidden by default, yet remain visible if the user wishes to see it
- offer a unified syntax to handle primitive and non-primitive arrays creation in reified generics. This syntax has to be compiled out when specializing a template to given parameter types, so that generated code looks like regular, not templatized Java
As added bonuses along the development path, came the following features :
- contract-based definition of template parameters capabilities (declaration of available constructors with complete signatures)
- java code inclusion from other classes, to get sort of multiple inheritance (of fields and public instance methods)
- integration of script-based code generation via BSF and Java Scripting API (only on Java 6 for the latter) (not functional yet)
- getters and setters autogeneration via the @Property annotation on fields (with optional addition of the variables to class constructors)
Jeneral relies heavily on Spoon, a very nice framework that lets you parse Java sources, transform them in a snap and serialize them back to files.
More on this to come soon as the project matures (or gets completely abandoned :-D)