Monday, September 3, 2012

Proposing a Improvement to the Override Annotation

The Java "@Override" annotation should have a "Class[] value() default {};" allowing you to be more specific about where the overridden method is inherited. The "default {}" is to be backwards compatible; and also to not force you to do it; and for non-branching inheritance paths, where it is not needed. (Or could it be useful even then? Discuss.) Note that multiple 'sub/super-type paths' could be involved, even where there may be only one 'original' method type declaration. 

Small thing, but nice-to-have. In Eclipse, you can already navigate to it using Cmd-T (or corresponding). But it just might be nice for readability, and refactorings could probably use it in some cases. 

Anyway, it would be more expressive. And aesthetics matter.

This will of course happen really soon, just because I suggested it, but never soon enough; so how about using this simple annotation while we hold our respective breaths:

@interface Overriding {
   Class[] value() default {};

Maybe @Overrides is better. It's one letter shorter. 

We could also implement some library code to walk the classpath and perform actual checking using reflection. It's not trivial, so I'll leave it as an exercise for the reader. 

No comments:

Post a Comment