 |
@nnotation type or type @nnotation
Posted by forax on March 18, 2007 at 07:42 AM | Comments (8)
The purpose of JSR 308 is to allow to
define annotation on types.
Currently, the JLS 3 only allows to annotate
language elements than accept modifiers so
it's not possible to annotate types.
Why allowing this is a good idea ?
There is real interest to allow annotation on Java types,
it enables to write safer code by performing
static source code analysis. Imagine annotations like:
- JetBrain's IDEA @NonNull, @Nullable
to avoid NullPointerException and helps database mapper
- @Scoped to denote objects allocated in scoped memory of
RTSJ
- Java Concurrent In Practice @Immutable or @ThreadSafe that allows to write
safer code in a multi-threaded environment.
If you want to see more examples, the
current draft of the spec is available
here.
There are two possible syntax, the first one is to allow
annotation of type at the left of the type like
any other Java annotations. But it introduce an ambiguity
because a reader doesn't see clearly if an annotation belong to
a variable, a method etc. or its type.
In the following sample, @NonNull refers to String
and @Column refers to name.
@Local
class MyBean {
@NonNull @Column(name="_NAME") String name;
@Deprecated @NonNull Dimension getSize() { ... }
}
The second one is to allow annotation on type at the
right of a type, in that case there is no ambiguity.
@Local
class MyBean {
@Column(name="_NAME") String @NonNull name;
@Deprecated Dimension @NonNull getSize() { ... }
}
The JSR308 expert group seems to think that the first syntax is
better, more Java-like even if it introduce an ambiguity.
I am not totally convinced.
What do you think ?
Cheers, Rémi
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Given the first syntax, one would expect the number of declarations allowed per statement to be 1, simply to get rid of the ambiguity, but that's not how Java works at the moment. The following line annotates both fields:
public @Deprecated String t,t2;
Posted by: ricky_clarkson on March 18, 2007 at 08:23 AM
-
The mistake was in putting the field annotation to the left of the type declaration, it should have been just next to the field name.
Posted by: tobega on March 18, 2007 at 11:00 AM
-
I'm sure you know my position: I'd prefer the unambiguous syntax, which is putting the annotation on a type to the right of the type.
Posted by: gafter on March 18, 2007 at 01:42 PM
-
Remi, is your second example correct? @NonNull applies to String and is to the right. @Column applies to name and is way to the left? This is confusing. If an annotation is allowed between the type declaration and the variable name, it should apply to the variable
Something like:
@TypeAnnotation Type @VarAnnotation variable
Posted by: tcbinjon on March 18, 2007 at 03:03 PM
-
My problem is that I can no longer see the code - the real code that is. There are just too many annotations. I remain of the view that NotNull/Nullable is language-level definition, not annotation level source checking - http://www.jroller.com/page/scolebourne?entry=java_7_null_safe_types (except that I'm now using # in FCM...).
Posted by: scolebourne on March 18, 2007 at 05:42 PM
-
I read it as:
a Variable String named name which is non-null and is a Column named _NAME.
so for me I don't care the Datetype is String or "NonNull-String". What I want to get is the String is Nullable or non-Nullable.
Following the English Grammar:
[Adjective Noun]
@NonNull @Column(name="_NAME") <=== adjective
String name; <=== noun
Posted by: fcmmok on March 19, 2007 at 12:12 PM
-
I like the first syntax. I prefer the descriptors first. Infixing some annotations and prefixing other just creates more opportunity to make a mistake.
Posted by: aberrant on March 20, 2007 at 08:06 AM
-
I would prefer:
@Column(name="_NAME") (@NonNull String) name
Posted by: mister__m on March 27, 2007 at 10:12 AM
|