Rajat Saxena
Rajat Saxena

Reputation: 3915

Java annotations: make @NotNull

I am learning Java annotations and I want to design a annotation like @NotNull which enforces compiler to throw an error on following:

@NotNull
private String myVar = null;

Now, I am not getting any lead in what to write in the body of the annotation:

@interface NotNull{
    // what goes here
}

Can I design such a functionality using Annotations or I have read it all wrong? I am looking at the checker package and it contains one such annotation.

Upvotes: 0

Views: 261

Answers (1)

nihab
nihab

Reputation: 56

You can only define the "meta" properties themselves in annotations, no actual logic

For compile-time annotation logic you need to write an annotation-processor (regular java-code that gets executed during compilation and allows you to examine annotated elements using reflection), during runtime you can use the regular reflection API

Upvotes: 3

Related Questions