Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an unsupported operation annotation in Java?

Are there any annotations in java which mark a method as unsupported? E.g. Let's say I'm writing a new class which implements the java.util.List interface. The add() methods in this interface are optional and I don't need them in my implementation and so I to do the following:

public void add(Object obj) {
   throw new UnsupportedOperationException("This impl doesn't support add");
}

Unfortunately, with this, it's not until runtime that one might discover that, in fact, this operation is unsupported.

Ideally, this would have been caught at compile time and such an annotation (e.g. maybe @UnsupportedOperation) would nudge the IDE to say to any users of this method, "Hey, you're using an unsupported operation" in the way that using @Deprecated flags Eclipse to highlight any uses of the deprecated item.

like image 833
Chris Knight Avatar asked Oct 27 '10 12:10

Chris Knight


1 Answers

Although on the surface this sounds useful, in reality it would not help much. How do you usually use a list? I generally do something like this:

List<String> list = new XXXList<String>();

There's already one indirection there, so if I call list.add("Hi"), how should the compiler know that this specific implementation of list doesn't support that?

How about this:

void populate(List<String> list) {
    list.add("1");
    list.add("2");
}

Now it's even harder: The compiler would need to verify that all calls to that function used lists that support the add() operation.

So no, there is no way to do what you are asking, sorry.

like image 168
Jonathan Avatar answered Sep 28 '22 03:09

Jonathan