Thursday, May 3, 2007

More Venting about Annotations

It's been a while since I've posted here and I've been distracted by some other projects. For example check out lift (formerly scala with sails), a web development framework built with scala. But now its back to working on Openlims and finding myself frustrated with Java Annotations.
Java Annotations lack of support for inheritance can make for lots of code duplication. This is something to be aware of when designing something around the use of Annotations. Consider this example:

public @interface Observable {
String name();
}
public @interface Setable {
String name();
}

Ideally you would like these to be of the same type, so you don't have to type 'name' twice and so you can pass the instance annotation to a method that takes a common type.

public @interface Observable {
String name();

public @interface Setable {
//name is NOT a member of Setable
//Setable is not a subtype of Observable
}
}
public Class MyClass {
public MyClass(Observable o) {}
}
public Class MyPersonalClass extends MyClass {
public MyPersonalClass(Setable s)
{
super(s);//won't work
}
@Setable(name="rodney")//name is not a member of Setable
public void annotatedMethod() {
}
}

Unfortunately this is not currently possible and forces the developer to take caution to avoid code duplication. It becomes unclear the best way to avoid this. Wrapper objects can be used, but it is not as 'elegant'. Annotations are very useful, but perhaps should not be used in place of regular objects.

No comments: