Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can you do and not do with java annotations? [closed]

The typical use-case is for simple things like @Override, but clearly you can do a lot more with them. If you push the limits of them, you get things like Project Lombok, though my understanding is that that's a huge abuse of annotations. What exactly can you do? What sort of things can you do at compile-time and run-time with annotations? What can you not do?

like image 720
swampsjohn Avatar asked Jun 06 '10 18:06

swampsjohn


People also ask

What can you do with Java annotations?

Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.

What Cannot be annotated in Java?

Java scoping construct cannot be annotated with type-use.

What is not true about annotations in Java?

Which of the following is not pre defined annotation in Java? Explanation: @Overriden is not a pre defined annotation in Java. @Depricated, @Override, @SuppressWarnings, @SafeVarags and @FunctionInterface are the pre defined annotations.

Why do we need Java annotations?

Annotations provide information to a program at compile time or at runtime based on which the program can take further action. An annotation processor processes these annotations at compile time or runtime to provide functionality such as code generation, error checking, etc. The java.


1 Answers

We use runtime annotation together with reflection to customize our Domain Model Mapping to the database. Also our form validation is based on Annotations in the code used at runtime.

In addition you can use the Annotation Processor which comes with Java to preprocess your Source files

EDIT: And with lombok, as asked in the Question there was added a new more powerful way of using this Annotation processing than mostly everyone here thought to be possible. Let me describe this for you in a few words: They hook the Java Annotation Processing step in

  1. The Java-Compiler and
  2. the Eclipse IDE where the parse tree for your code is generated.

This way eclipse shows you more code that you really wrote, and the javac believes the same. The generator technic uses old style Java Annoation Processing, but you can think of Lombok as the missing glue everyone needed to make it really useful.

like image 188
Daniel Avatar answered Sep 28 '22 01:09

Daniel