views:

101

answers:

2

Hi,

Can anyone please explain what the following two paragraphs mean, in simple english? (Taken from http://www.ibm.com/developerworks/java/library/j-cwt08025.html)

"Annotations are more flexible in terms of how you use them, with options for whether the annotation information is to be included in class files output by the compiler and made available to the application at run time"

Not sure what these means. Can Annotations be configured to optionally change the bytecode?

While annotations are ideal for metadata that relates to a particular component, they are not well suited to metadata with cross-component application.

IMHO most web applications would be cross-component ones. What is the author trying to say here?

+3  A: 

"Annotations are more flexible in terms of how you use them, with options for whether the annotation information is to be included in class files output by the compiler and made available to the application at run time"

It is more flexible than XDoclet because:

  • it can be used from the source code (like XDoclet)
  • it can be used at runtime, when you only have the byte-code and not the source code (unlike XDoclet)


While annotations are ideal for metadata that relates to a particular component, they are not well suited to metadata with cross-component application.

Annotations (like XDoclet) have one interesting feature, as opposed to an external Xml for example :
Annotations live in the code, so it is natural for them to be applied to the code they are defined on. You don't have to specify (using some complex syntax) to what piece of code they apply. Examples:

  • if an annotation is defined on a method, it applies naturally to that method
  • if an annotation is defined on a field, it applies naturally to that field
  • if an annotation is defined on a class, it applies naturally to that class
  • if an annotation is defined on a package, it applies naturally to that package

If you want to have the same in an external Xml file, you have to use a complex syntax to identify the piece of code you refer to. So that makes them very easy to apply.

Also, in case of a code refactoring (like renaming), annotations continue to work just fine, while an external xml would have to be changed to point to the new class or method name.


I don't believe that in a web application, most things are cross-component.

  • If you defined Persistance (to a database) of an Entity, like what is the table where this class should be persisted, it is not something global to all Entities, it only affects the current Entity.
  • same for many other examples...
KLE
+3  A: 

Annotations are more flexible in terms of how you use them, with options for whether the annotation information is to be included in class files output by the compiler and made available to the application at run time

This, I think, refers to the fact that Java5 annotations can be dropped by the compiler, whereas some can be retained in the bytecode. This is controlled by the @Retention annotation that is placed on your annotation type, e.g

@Documented
@Retention(value=RUNTIME)
public @interface Deprecated

This indicates that the @Deprecated annotation will be present in the bytecode, and will also be visible to reflection. java.lang.annotation.RetentionPolicy defines the different options.

skaffman
Thanks Skaffman, that clarifies it well.
Dee