What is a Java Annotation?

T

Thomas Weidenfeller

Probably a very stupid newbee question, but what is meant with
annotation

A generic mechanism to add meta data to the source code. Tools, like the
compiler, but also additional tools, can then do some magic tricks if
they find an annotation in the code.

Depending on the type of annotation, the meta data is removed when
compiling, or is compiled into the class file. If you do the later, you
can check the meta data during runtime, an do even more magic tricks
while the program runs.


/Thomas
 
C

Chris Uppal

Thomas said:
Depending on the type of annotation, the meta data is removed when
compiling, or is compiled into the class file.

Nitpick: the metadata is always added to the classfile (or it wouldn't be a lot
of use ;-) The distinction is between whether the /JVM/ discards the metadata
as it loads the class, or retains it for runtime use by the application.

-- chris
 
D

Dale King

Chris said:
Nitpick: the metadata is always added to the classfile (or it wouldn't be a lot
of use ;-) The distinction is between whether the /JVM/ discards the metadata
as it loads the class, or retains it for runtime use by the application.

Nit-nitpick:

Annotations are not necessarily added to the class file. There are 3
RetentionPolicy values defined that specify how long annotations are to
be retained: SOURCE, CLASS, RUNTIME.

The SOURCE value is used for things that are only significant to the
compiler. For example, the Override and SuppressWarnings annotations are
source only annotations.

With the annotation processing API coming in JSE 6 (JSR 269) there will
probably be many more uses for source only annotations.
 
C

Chris Uppal

Dale said:
Annotations are not necessarily added to the class file. There are 3
RetentionPolicy values defined that specify how long annotations are to
be retained: SOURCE, CLASS, RUNTIME.

I'd completely forgotten about SOURCE. Thanks for the correction.

-- chris
 
M

Mike Schilling

Chris Uppal said:
I'd completely forgotten about SOURCE. Thanks for the correction.

Presumably if Java were being designed from scratch today Javadoc would be
SOURCE.
 
C

Chris Uppal

Mike said:
Presumably if Java were being designed from scratch today Javadoc would be
SOURCE.

Quite possibly. An even more interesting possibility (IMO) would be to give it
CLASS status.

Classfile format is a high-level programming language anyway, so why not ship
the API documentation along with the "code" ?

-- chris
 
M

Mike Schilling

Chris Uppal said:
Quite possibly. An even more interesting possibility (IMO) would be to
give it
CLASS status.

Classfile format is a high-level programming language anyway, so why not
ship
the API documentation along with the "code" ?

In some cases, security via obfuscation.
 
C

Chris Uppal

Mike Schilling wrote:

[me:]
In some cases, security via obfuscation.

Well, there's nothing to say that the info couldn't be stripped out, or never
added in the first place (cf. debugging info).

But it was just an idle thought ;-)

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top