Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NotNull annotation for Java SE

 
Eric Lopez-Fernandez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there,

I am trying to implement an annotation that will function similar to the javax.validation.constraints.NotNull but something that I can use with Java SE.


I have the following annotation but it does not seem to work. Can you please take a look?

Here is the annotation:

-------------------------------------------------------------------------------------------------------------------

import java.lang.annotation.*;

/**
* Designates that a field, return value, argument, or variable is
* guaranteed to be non-null.
*/
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
@Documented
@Retention(RetentionPolicy.CLASS)
public @interface NotNull {}

-------------------------------------------------------------------------------------------------------------------

Here is the class that will use it:
-------------------------------------------------------------------------------------------------------------------
public class UsingNotNull{

public void notNullMethod(@NotNull String cadena){
System.out.println(cadena.trim());
}
public static void main(String... args){
String cadena = "it works!";
String cadenaNula = null;

UsingNotNull unn = new UsingNotNull();
unn.notNullMethod(cadenaNula);
}
}
-------------------------------------------------------------------------------------------------------------------

Thanks in advance for your help!



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that ItDoesntWorkIsUseless.(⇐click)
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because you create an annotation doesn't mean that the compiler knows what to do with it. For annotations @Deprecated, @Override, @SuppressWarnings and (since Java 7) @SafeVarargs, the compiler has built-in support because the compiler knows these will always exist. This isn't true for custom created annotations. The only way to get that one to work is to create your own compiler that does know that it should treat that annotation specially.

There are IDEs (IntelliJ comes to mind, with its own @NotNull) that have their own compiler with added support for extra annotations - but again, annotations pre-programmed into the compiler.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you know that there is a JSR: JSR 305 for these kinds of annotations? You can download the reference implementation for this JSR. A tool such as FindBugs will recognise the annotations defined in this JSR, so it can help you find potential bugs if you use these annotations. If you define your own NotNull annotation, FindBugs (and other tools) ofcourse won't recognise it, so it will be of limited value.

JSR 305 puts a number of annotations in the javax.annotation package. One of the annotations is @Nonnull.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic