Explanation about annotation

Can someone explain what the next two paragraphs mean in plain English? (Taken from http://www.ibm.com/developerworks/java/library/j-cwt08025.html )

"Annotations are more flexible in terms of your use, with options for annotation information to be included in the class files, output by the compiler and made available for the application at startup time"

Not sure what that means. Can annotations be configured to optionally change the bytecode?

While annotations are ideal for component-specific metadata, they are not well suited for cross-component metadata.

IMHO Most web applications will be cross-component. What is the author trying to say here?

+2


source to share


2 answers


Annotations are more flexible in terms of how you use them, with options for whether the annotation information is to include in the output of the class files by the compiler and the application at runtime

This, I think, refers to the fact that Java5 annotations can be removed by the compiler, whereas some can be stored in bytecode. This is controlled by an annotation @Retention

that is put into your annotation type, for example



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

      

This means that the annotation @Deprecated

will be present in the bytecode and will also be visible for reflection. java.lang.annotation.RetentionPolicy

defines various parameters.

+3


source


"Annotations are more flexible in terms of how you use them, with options for including annotation information in class files that are output by the compiler and provided to the application at runtime."

It is more flexible than XDoclet because:

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

While annotations are ideal for component-specific metadata, they are poorly suited for cross-component metadata.

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



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

If you want to have the same in an external Xml file, you need to use complex syntax to identify the piece of code you are linking to. Thus, they are very easy to apply.

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


I do not believe that most things in a web application are cross-component.

  • If you define Persistance (to the database) of an Entity as what is the table in which this class should be stored, it is not something global for all Entities, it only affects the current Entity.
  • for many other examples ...
+3


source







All Articles