Friday 16 August 2013

Annotation- 50

Learning Objectives
After completing this session, you will be able to:
‰  Define Annotation
‰  Explain the importance of Annotation
‰  Identify Built-in Annotations
How Annotations are Used?
Annotations are used to affect the way programs are treated by tools and libraries.
Annotations are used by tools to produce derived files:
Tools:Compiler, IDE, and Run-time tools
Derived files:New Java code, deployment descriptor, and class files
Ad-hoc Annotation-like Examples in pre-J2SE 5.0 Platform
Ad-hoc Annotation-like examples in pre-J2SE 5.0 platform:
‰  Transient
‰  Serializable interface
‰  @deprecated
‰  javadoc comments
‰  Xdoclet
J2SE 5.0 Annotation provides a standard, general purpose, and more powerful annotation
scheme.
Need of Annotation
Annotation enables “declarative programming” style:
‰  Less coding because tool will generate the boiler plate code from annotations in the
source code
‰  Easier to change
Annotation eliminates the need for maintaining "side files" that must be kept up to date with
changes in source files:
‰  Information is kept in the source file
‰  (example) Eliminate the need of deployment descriptor
Built-in Annotations
Java defines seven built-in annotations.
The following four are imported from java.lang.annotation:
‰  @Retention
‰  @Documented
‰  @Target
‰  @Inherited
The following three are included in java.lang:
‰  @Override
‰  @Deprecated
‰  @SuppressWarnings
Annotation Retention Policy
‰  A retention policy determines at whatpoint an annotation is discarded.
‰  Java defines three such policies of SOURCE, CLASS, and RUNTIME.
‰  The earlier policies are encapsulated withinthe java.lang.annotation.RetentionPolicy
enumeration.
‰  An annotation with a retention policy of SOURCE is retained only in the source file and
is discarded during compilation.
‰  An annotation with a retention policy of CLASS is stored in the .class file during
compilation. However, it is not available through the JVM during run time.
‰  An annotation with a retention policy of RUNTIME is stored in the .class file during
compilation and is available through the JVM during run time.
‰  RUNTIME retention offers the greatest annotation persistence.
@Retention
‰  @Retention is designed to be used only as an annotation to another annotation.
‰  Its general form is as follows:
@Retention(retention-policy)
‰  Here, retention-policy must be one of the earlier discussed enumeration constants. If
no retention policy is specified for an annotation, then the default policy of CLASS is
used.
‰  The following example uses @Retention tospecify the RUNTIME retention policy.
Thus, the following annotation (MyAnno) will be available to the JVM during program
execution.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
String str();
int val();
}
@Documented
‰  The @Documented annotation is a marker interface, which tells a tool that an
annotation is to be documented.
‰  It is designed to be used only as an annotation to an annotation declaration.
@Target
‰  The @Target annotation specifies the types of declarations to which an annotation
can be applied.
‰  It is designed to be used only as an annotation to another annotation.
‰  @Target takes one argument, which must be a constant from the ElementType
enumeration. This argument specifies the types of declarations to which the
annotation can be applied.
The following table shows the constants and the type of declaration to which they correspond:
Target Constant  Annotation Can Be Applied To
ANNOTATION_TYPE  Another annotation
CONSTRUCTOR  Constructor
FIELD  Field
LOCAL_VARIABLE  Local variable
METHOD  Method
PACKAGE  Package
PARAMETER  Parameter
TYPE  Class, interface, or enumeration
‰  You can specify one or more of the values (as shown in the earlier slide) in a @Target
annotation.
‰  To specify multiple values, you must specify them within a braces-delimited list.
‰  For example, to specify that an annotation applies only to fields and local variables,
you can use this @Target annotation:
@Target( { ElementType.FIELD,ElementType.LOCAL_VARIABLE } )
@Inherited
‰  @Inherited is a marker annotation that can be applied only on another annotation
declaration.
‰  It affects only annotations that will be applied on class declarations.
‰  @Inherited causes the annotation for a super class to be inherited by a subclass.
@Override
‰  @Override is a marker annotation that can be applied only on methods.
‰  A method annotated with @Override must override a method from a super class. If it
does not, then a compile-time error will result.
‰  It is used to ensure that a super class method is actually overridden, and not simply
overloaded.
@Deprecated
‰  @Deprecated is a marker annotation.
‰  It indicates that a declaration is obsolete and has been replaced by a new form.
@SuppressWarnings
‰  @SuppressWarnings specifies that one or more warnings that might be issued by the
compiler are to be suppressed.
‰  The warnings to suppress are specified by name, in string form.
‰  This annotation can be applied to any type of declaration.
Tips and Tricks:
Does the new feature in Java 2, v5.0 called ‘metadata’ is same as the ‘annotation’ feature?
Solution:
‰  Java 2, v5.0 adds a powerful new facility called metadata, which enables you to
embed supplemental information into a source file.
‰  This information, called an annotation, does not change the actions of a program.
‰  An annotation leaves the semantics of a program unchanged.
‰  However, this information can be applied by various tools, during both development
and deployment.
‰  For example, an annotation might be processed by a source-code generator.
‰  Although Sun refers to this feature as metadata, the term program annotation facility is
also applied, and is more descriptive.
Summary
‰  All annotation types automatically extend the Annotation interface.
‰  Thus Annotation is a super-interface of all annotations.
‰  The Annotation interface is declared within the java.lang.annotationpackage.
‰  It overrides hashCode(), equals(), and toString(), which are defined by Object.
‰  It also specifies annotationType(), which returns a Class object that represents the
invoking annotation.

No comments:

Post a Comment