Introduction
ð Annotations
are java language feature introduced in java5
ð Annotations
can be used for
o Automatic
generation of configuration files(deployment descriptor, bean classes,etc)
o Automatic
generation of code for testing, logging, database integration etc.
ð Annotations
let you avoid writing boiler plate code under many circumstances by enabling
tools to generate it from annotations in the source code.
ð Java5
platform introduces a simplified declarative programming model with annotations.
With java 5 technology, Xml deployment descriptors are now optional. Instead of
describing the components with the xml file we can describe with the
annotations in the java source file itself.
ð It
also eliminates the need of maintaining side files that must be kept upto the
date with changes in source file.
ð Annotations
supply additional instructions to the compiler to behave according to the
annotations applied for a class.
ð Annotations
do not directly effect program semantics, but they do effect the way programs
are treated by tools and libraries.
Annotations Topics
MetaData
a) Introduction
to MetaData
b) Types
of MetaData
c) Value
of MataData
Annotations
a) Why
Annotations and Why it matters?
b) World
before Annotations
c) What
is an Annotation
Annotations as far
as Java6
a)Meta (java.lang.annotation
package)
1) @Documented
2) @Inherited
3) @Target
4) @Retention
b)Standard(java.lang package)
1) @Override
2)
@Depricated
3) @SuppressWarnings
Types of
Annotations
a)
Marker
b)
SingleMarker
c)
MultiMarker or Full Annotation
d)
Complex or Nested Annotation
User defined
Annotations
Annotation Processing
and Life cycle of Annotations
Custom Annotation
processor
Reflections
Advantages and
Limitations of Annotations
Metadata
Data about the data is
called metadata
Examples
: documentation comments, ResultSetMetaData, user_tables etc
Types of
Metadata
There are 2 types of metadata
- Internal
metadata
- External
metadata
ð The data which is used to describe the
component inside the source file itself is called internal metadata.
Ex : /**-----------*/
ð The
data which is used to describe components externally to the source file is
called external metadata.
Ex: xml
file like web.xml
NOTE:
With annotations we can give the metadata within the same source file itself.
ð If
there are any problems in xml file we will come to know about them at runtime.
But with annotations we can solve them at compile time itself or we can make it
checked at runtime.
Value
of Metadata
We can get the value of metadata using some tools like
1) to get the documentation comments of .java file --“javadoc”
2) to create xml files and java src files to EJB -- “XDoclet”
3) to get servlets metadata from web.xml -- “parsers”
Why
Annotations ?
·
External
tools like javadoc(.html), XDoclet(.Java,.xml) are used to create particular
files only.
·
Marker
interfaces(Serializable, Cloneable, RandomAccess, Remote, SingleThreadModel…etc.) can be applied to only classes but we can’t
apply to other java elements
·
transient keyword can be applied to only variables
NOTE: Annotations can be applied to any type of
java elements
Why
annotations matters?
·
Annotations
are meant for adding metadata to all java elements like packages, classes,
methods, fields etc.
·
Annotations
can be used as marker interface
·
Annotations
can be processed at 3 levels
1) Source
2) Class
3) Runtime
World
before Annotations
Before annotations Xml was used to annotate java code and some tools
used to get annotated data.
What
are annotations?
·
In
short annotations are called metadata of any java elements. Annotations are
said to be annotate a java element.
·
An
annotation associates orbitrary information or metadata with a java element.
Annotations
as far as java6:
Meta
Annotations
·
Meta
Annotations can be imported from java.lang.annotation
package
·
Meta
Annotations are used to define other Annotations means custom annotations.
·
@Retention, @Target, @Documented, @Inherited are
called Meta annotations
@Documented
If we
use this annotation to a java element that annotation will be available in the
java documentation when we apply our class to “javadoc” tool.
@Inherited
If we
apply this annotation to any custom annotation that will be available in the
child classes also
@Target
This annotation is
used to tell to which java element you want to apply annotations.
If we don’t specify @Target annotation it will be
applicable for all java elements.
@Retention
This
annotation is used to specify up to which level the annotation behavior will be
available(source/class/runtime).
Example on
@Documented
DocumentedAnn.java
1. package com.nit.annotation;
2. import java.lang.annotation.Documented;
3. @Documented
4. public @interface DocumentedAnn {
5.
6. }
DocumentedDemo.java
1. package com.nit.annotation;
2.
3. /**
4. * @author sekhar
5. * This is a sample class
demontrating meta annotations
6. *
7. */
8. @DocumentedAnn
9. public class DocumentedDemo {
10.
11. /**
12. * this method doesn't contain any
great logic
13. */
14. public static void main(String[]
args) {
15.
16. }
17. }
C:\> javadoc
DocumentedDemo.java => *.html will be generated
NOTE : Now open index.html file, you can find
“@DocumentedAnn” annotation. Now remove
“@Documented” annotation on “@DcumentedAnn” and generate javadocs once again,
in the generated javadocs we don’t find “@DcumentedAnn” annotation.
Source Code of @Documented
1. @Documented
2. @Retention(RetentionPolicy.RUNTIME)
3. @Target(ElementType.ANNOTATION_TYPE)
4. public @interface Documented {
5. }
Example on
@Inherited
MyAnnotation.java
1. package com.nit.annotation;
2. import java.lang.annotation.Documented;
3. import java.lang.annotation.Inherited;
4. import java.lang.annotation.Retention;
5. import java.lang.annotation.RetentionPolicy;
6.
7. @Documented
8. @Inherited
9. @Retention(RetentionPolicy.RUNTIME)
10. public @interface MyAnnotation {
11.
12. }
Parent.java
1. package com.nit.annotation;
1. @MyAnnotation
2. public class Parent {
3.
4. }
Child.java
1. package com.nit.annotation;
2. public class Child extends Parent{
3.
4.
}
InheritedDemo
1. package com.nit.annotation;
2. import java.lang.annotation.Annotation;
3. public class InheritedDemo {
4. public static void main(String[]
args) {
5. Class
parentClass=Parent.class;
6. Class
childClass=Child.class;
7. Annotation[]
annotations=parentClass.getAnnotations();
8. System.out.println("Annoations
of parent class:"+annotations.length);
9. Annotation[]
annotations1=childClass.getAnnotations();
10. System.out.println("Annotations
inherited to child class from Parent
11. class
are:"+annotations1.length);
12. Annotation[]
annotations2=childClass.getDeclaredAnnotations();
13. System.out.println("Annotations
of child class
14. are"+annotations2.length);
15. }
16. }
NOTE: remove “@Inherited” annotation on
“@MyAnnotaion”, then see the difference in the output.
Source Code of @Documented
1. @Documented
2. @Retention(RetentionPolicy.RUNTIME)
3. @Target(ElementType.ANNOTATION_TYPE)
4. public @interface Inherited {
5. }
Java.lang.annotation.Annotation
ð
In
java every annotation implicitly implements Annotation interface. If we implement Annotation interface explicitly to any class that won’t behave like
an Annotation.
public class MyAnnotation implements
Annotation {
…………………
}
ð
In
the above example MyAnnotation is not an Annotation.
@Target
ð While defining
Annotation we should specify the Target type i.e should the annotation can be
used to Annotation class or method or some other java elements.
ð Target Annotation
is a meta Annotation. It is defined to annotate other annotations.
ð Target takes one
argument which must be a constant from ElementType enumeration. this argument
specifies the types of declarations to which annotation can be applied.
ð An annotation
targeted to ElementType.TYPE cannot be applied to method, field,
local-variables, constructors and parameters. Violation of this will result to
compilation error. The constants are shown in the below table.
Type
|
Annotation can be
applied to
|
ElementType.TYPE
|
Class,interface,enumeration
|
ElementType.METHOD
|
Methods
|
ElementType.FIELD
|
Fields
|
ElementType.PARAMETER
|
Parameters
|
ElementType.CONSTRUCTOR
|
Constructors
|
ElementType.LOCAL_VARIABLE
|
Local variables
|
ElementType.ANNOTATION_TYPE
|
Another annotation
|
ElementType.PACKAGE
|
Package
|
Source code of @Target
1. @Documented
2. @Retention(RetentionPolicy.RUNTIME)
3. @Target(ElementType.ANNOTATION_TYPE)
4. Public @interface Target{
5. ElementType[] value;
6. }
Source code of ElementType
1. Public enum ElementType{
2.
TYPE, FIELD, CONSTRUCTOR,
METHOD, PARAMETER, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE
3.
}
Examples on
@Target
Q.) Define
an annotation which will be applicable to interfaces, classes, enumerations and
that should be available in the documentation and that should not be inherited
to child classes.
1.
@Documented
2.
@Target(ElementType.TYPE)
3.
Public @ interface MyAnnotation{
4.
}
Q.) Define
an annotation which will be applicable to classes, methods, local variables.
1.
@Target({ElementType.TYPE, ElementType.METHOD,
ElementType.LOCAL_VARIABLE})
2.
Public @interface MyAnnotation{
3.
}
Note : If we
don’t specify any @Target annotation, then that will be applicable for all
elements.
@Retention
ð Retention
determines at what point an annotation discarded. Java defines 3 such policies.
Those are encapsulated with in java.lang.RetentionPolicy
enumeration. they are
§
RUNTIME
§
CLASS
§
SOURCE
ð An
annotation with RetentionPolicy.SOURCE
is retained in the source file and is discarded during compilation.
ð RetentionPolicy.CLASS
for storing in ‘.class’ file during the compilation. It is not available to the
JVM at runtime.
ð An
annotation with RetentionPolicy.RUNTIME
is stored in ‘.class’ file during the compilation and is available to jvm
during runtime. Thus RUNTIME RetentionPolicy
offers greatest annotation persistent.
Source code of @Retention
1.
@Documented
2.
@Retention(RetentionPolicy.RUNTIME)
3.
@Target(ElementType.ANNOTATION_TYPE)
4.
Public @interface Retention{
5.
RetentionPolicy value ();
6.
}
Source code of RetentionPolicy
1.
public enum RetentionPolicy {
2.
SOURCE,
CLASS, RUNTIME
3.
}
Example of
@Retention
Q.) Define
an annotation that is available at runtime and is applicable to fields only.
1.
@Retention(value=RetentionPolicy.RUNTIME)
2.
@Target(value=ElementType.FIELD)
3.
Public @interface MyAnnotation{
4.
}
Q.) Define
an annotation which is available at Source level and at runtime?
1.
@Retention({RetentionPolicy.SOURCE,RetentionPolicy.RUNTIME})
2.
// (OR)
3.
@Retention(value=RetentionPolicy.SOURCE)
4.
@Retention(value=RetentionPolicy.RUNTIME)
5.
Public @interface MyAnnotation{
6.
}
The above way of applying the RetentionPolicy is
illegal. Multiple RetentionPolicies cannot be applied.
1.
@Retention(RetentionPolicy.RUNTIME)
2.
Public @interface MyAnnotation{
3.
}
ð
All
the meta annotations Target type is ANNOTATION_TYPE
ð
For
all meta annotations the RetentionPolicy is RUNTIME
ð
All meta annotations are documentable
ð
All
meta Annotations are not inheritable
ð
If
you don’t specify the Target type will be applicable for all the elements
ð
Default
Retention value is CLASS
Q.) Write
an annotation which will be retained in the class file only but not available
to JVM.
1.
@Retention ( RetentionPolicy.CLASS)
2.
Public @interface MyAnnotation{
3.
………..
4.
}
Standard
Annotations
ð Standard
annotations are available in java.lang package.
ð Standard
annotations are used to annotate our java code directly.
ð Standard
annotations available are
1. @Override
2. @Deprecated
3. @SuppressWarnings
ð @Override is
retained only in the source code.
ð @Deprecated is
available in ‘.class’ file and available to JVM also
ð @SuppressWarnings
is available in the source file only.
@Override
ð It
indicates that a method is intended to override a method of a super class in
the sub class.
ð The
two most commonly made mistakes which are never detected by the java compiler
are
1. the overridden
method in sub class is misspelled.
2. the methods
signature of sub class does not match the super class method signature
ð this
will result in the sub class method can not be treated as overriding method
ð To overcome this
problem java 1.5 introduced @Overridde annotation.
ð It is a marker
annotation that can be used only on methods
ð A method annotated
with @Override must override a method from a super class if not we will get
compile time error.
Example on
@Override
Square.java
1.
package com.nit.annotation;
2.
public class Square{
3.
public void square(int a, int b) {
4.
System.out.println("square class method
called");
5.
System.out.println(a*a+2*a*b+b*b);
6.
}
7.
}
BetterSquare.java
1.
package com.nit.annotation;
2.
public class BetterSquare extends Square{
3.
@Override
4.
public void sqare(int a,int b) {
5.
System.out.println("Better Square class method called");
6.
System.out.println((a+b)*(a+b));
7.
}
8.
}
OverrideDemo.java
1.
package com.nit.annotation;
2.
public class OverrideDemo {
3.
public static void main(String[] args) {
4.
Square
s= new BetterSquare();
5.
s.square(10,
20);
6.
}
7.
}
NOTE:
In the above exmaple in BetterSquere class if we don’t write @Override
annotation, our intended method(overriding method) will not be called. If we apply @Override annotation, if it is
not overriding it will give compilation error.
Source code of
@Override
1.
@Target(ElementType.METHOD)
2.
@Retention(RetentionPolicy.SOURCE)
3.
public
@interface Override{
4.
5.
}
@Deprecated
ð Java provides a mechanism
to express deprecation. As new classes invented its API changes naturally,
method names are renamed for consistency, new and better methods are added and
fields may change.
ð Some changes
introduce problems, we need to keep the old API available until developers
habituated to new one. But we don’t want to continue them in the future.
No comments:
Post a Comment