Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is annotation processing in Java?

Quoting, Sun's Official Java Tutorial

Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested

What does it mean? And how to apply it?

like image 612
Xolve Avatar asked Jan 27 '10 10:01

Xolve


People also ask

How does annotation processing work?

Annotation processing takes place at compile time (compile time = the time when the java compiler compiles your java source code). Annotation processing is a tool build in javac for scanning and processing annotations at compile time. You can register your own annotation processor for certain annotations.

What is annotation in Java with example?

Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. Annotations start with @ . Its syntax is: @AnnotationName.

Why do we need annotation processor?

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.

What is annotation processing tool?

Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.


2 Answers

"Annotation Processing" is a hook into the compile process of the java compiler, to analyse the source code for user defined annotations and handle then (by producing compiler errors, compiler warning, emitting source code, byte code ...).

API reference: http://java.sun.com/javase/6/docs/api/javax/annotation/processing/package-summary.html

like image 56
Arne Deutsch Avatar answered Sep 25 '22 12:09

Arne Deutsch


From the very next line of the page that you refer to:

Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested

If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac HelloWorldApp.

That is, the string that you are referring to is a possible error that you might get when trying to compile the examples. The very next line in the document, tells you how to resolve the issue.

If you want to know more about annotations, what they are, and how to use them, then I would suggest to go through the Annotations tutorial.

like image 20
Paul Wagland Avatar answered Sep 25 '22 12:09

Paul Wagland