kostja
kostja

Reputation: 61558

Why cannot all statements be annotated in Java?

I ran into a simple issue naively trying to do this:

public void someMethod(){    
  int x = 0;
  ...
  @SuppressWarnings({"rawtypes", "unchecked"})
  x = ((Comparable)lhs).compareTo(rhs);
  ...
}

This is illegal and has to be rephrased to compile:

public void someMethod(){  
  ...  
  @SuppressWarnings({"rawtypes", "unchecked"})
  int x = ((Comparable)lhs).compareTo(rhs);
  ...
}

I have traced the issue down to ElementType : a statement doesn't seem to be a valid program element. This is rather confusing - I thought that a statements is something like a supertype of all programming elements.

  1. Is there a theoretical or a technical reason for the restriction of valid elements?

  2. Could it be done differently - i.e. supposed I could supplant ElementType with my own class and master the rippling changes, could I annotate any statement?

Upvotes: 7

Views: 6599

Answers (4)

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

It has been proposed as part of JSR-308 to allow annotations on statements, but currently it is not supported and won't be part of the next Java language release (i.e. Java 8).

I guess that at the time annotations been added to the language, the main target class and method level information mainly crafted around run-time use cases (e.g. Java EE, JPA, JAX-WS, etc), while statement-level annotations are primarily useful at compile-time (see above link to the wiki for the list of use cases).

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

If you look at the Javadoc for @SuppressWarnings you'll see the answer: its declared targets are

@Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})

In other words, it cannot be legally applied to a statement. It can, however, be applied to a variable declaration. It has nothing to do with whether a statement is a program element; it is basically because this annotation applies only to declarations of things.

Furthermore, if you look at the Javadoc for the enumeration that describes things that can have annotations, statements and expressions are not among the choices. In general, annotations can be applied to declarations of things, not to bits of code.

The theoretical reason for this is just that annotations are stored as properties of individual items declared in the class file. Statements don't qualify; by the time your code is compiled, statements have ceased to exist. There is only a stream of bytecode, and the only reminder of the statements it came from are the (optional) line number tables. To deal with this, they'd need to add a new attribute to the class file to reference the individual bytecodes, as in this proposal, and deal with a number of complexities that arise as a result.

Upvotes: 12

Alex
Alex

Reputation: 13951

I know an answer has already been accepted, just throwing this out there. Here's an excerpt from the FAQ for JSR-175 which originally added annotations to Java:

Why can't you annotate arbitrary program elements such as blocks and individual statements?

This would greatly complicate the annotation syntax: We would have to sacrifice the simplicity of saying that annotations are simply modifiers, which can be used on declarations.

In other words, because it's too much work :-)

Upvotes: 5

Alex
Alex

Reputation: 13951

A declaration in Java is an entity that has an identifier and can be referenced from other parts of the program. A statement doesn't meet that criteria - it's an action that possibly results in a value being assigned to some declared entity.

Section 6.1 of the Java spec (http://docs.oracle.com/javase/specs/jls/se5.0/html/names.html#6.1) lists the types of declarations, which aligns with the ElementType enumeration values.

Upvotes: 2

Related Questions