• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NotNull annotation for Java SE

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that ItDoesntWorkIsUseless.(⇐click)
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic