6

I've been having trouble understanding where exactly an annotation has to or can be placed.

The class with this method compiles, but gives a warning "unchecked":

<B extends BitSet> void doStuff(LinkedList<B> list) {
    B board = list.getFirst();
    B cloneBoard;
    cloneBoard = (B) board.clone(); //unchecked
}

This compiles without warning:

<B extends BitSet> void doStuff(LinkedList<B> list) {
    B board = list.getFirst();

    @SuppressWarnings("unchecked")
    B cloneBoard = (B) board.clone();
}

This doesn't compile but marks cloneBoard with an error:

<B extends BitSet> void doStuff(LinkedList<B> list) {
    B board = list.getFirst();
    B cloneBoard;

    @SuppressWarnings("unchecked")
    cloneBoard = (B) board.clone(); // cloneBoard cannot be resolved to a type;
                                    // VariableDeclaratorID expected
}

In the Sun tutorial on annotations, I couldn't find an answer as to why this is: http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html.

The grammar definition didn't help me either, since I'm not quite sure I understand it correctly: http://java.sun.com/docs/books/jls/third_edition/html/syntax.html#18.1

It seems to me that what's the problem here is that annotations can be specifically used for variables, but only when they are declared; any later assignment will not be covered by the annotation. Is this correct? Is there a more elegant solution than suppressing unchecked warnings for the whole method?

1
  • Woah, your answers came way faster than I expected, thanks! All of them were really helpful; I accepted ruakh's answer for the useful part one elegance (especially the code part). Commented Dec 8, 2011 at 12:30

3 Answers 3

3

From Java Language Specification - Interfaces > Annotation

Annotations may be used as modifiers in any declaration, whether package, class, interface, field, method, parameter, constructor, or local variable..

It can only use in a declaration.

Sign up to request clarification or add additional context in comments.

Comments

2

An annotation is part of a declaration; just as you can't write Object obj except at the point where obj is declared, nor final obj except as final Object obj, so too is @Deprecated obj forbidden.

As for elegance — ideally your methods should not be very long and complicated, anyway, but if you do find that you'd like to mark a specific assignment with this annotation, you can always make use of a simple wrapper method:

@SuppressWarnings("unchecked")
private static <T extends ClassThatDeclaresCloneAsPublic> T cloneObj(T obj)
    { return (T) obj.clone(); }

(Though in this specific case, I suppose you could write cloneBoard = board.getClass().cast(board.clone()); and dispense with the annotation altogether, if you wanted.)

1 Comment

If you're wondering why you got +15 again today, I was curious whether it really is possible to "un-accept" an answer after such a long time :P
1

Annotations are part of the declaration (others have already answered this part).

However, to answer the second part of your question: I have never had a problem marking the whole method with this annotation when the method is short like this. I personally prefer annotations on the method/class level rather than inside of methods because I think they just distract the reader.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.