Tuesday, December 16, 2003

Generic throws clauses in Java interfaces declarations

Anyone who has written an EJB to expose a POJO (Plain Old Java Object) has encountered the problem - your POJO implements an interface and you would like to reuses that interface declaration when you write your EJB's remote interface declaration; but you can't because the remote EJB methods must throw RemoteException and the base interface methods don't. So what do you do? Pragmatic Programmers out there might write some code generation code that builds the remote interface but more likely than not you copy-paste-modify the original interface. All this leading inevitably to a future maintenance problem

This isn't just a J2EE issue either, other code can have layering designs that change the sort of exceptions that are thrown but not the basic signature of the methods. (Its just such code I'm dealing with today at work that's sparking this rant). What I'm dreaming of is support of some sort of generic exception declarations in an interface. Rather than always declaring the throws I would like to be able declare an exception agnostic interface and then derive exception explicit variations from that base version. Here's an example in my dream pseudo Java:

A simple generic interface class with no explicit throws

public abstract interface AbstractFoo {
void methodOne() throws *;
void methodTwo() throws *;

A derivation of the generic interface that throws SomeException

public interface Foo extends AbstractFoo throws SomeException {

Another derivation of the generic interface that throw RemoteException and SomeException.

public interface FooRemote extends AbstractFoo
throws SomeException, RemoteException {

I wont hold my breath.

Post a Comment
The Out Campaign: Scarlet Letter of Atheism