Introduction
- · Annotations provide the special way
of specifying metadata inside the java class instead of writing in XML file.
- · When you are developing application
using Servlets, Struts, Hibernate, Sprint, EJB etc, you need to write many XML documents
which are specific to corresponding framework or technology.
- · If you are specifying metadata
inside the XML file, then internally SAX parser will be used to read the
information from XML file and to store in main memory.
- · With JAVA 5, you can avoid this XML
files by specifying the metadata related to Servlets, Struts, Spring, Hibernate
in the class itself with the help of annotation i.e annotation is alternative
to XML and provides another way of representing metadata.
- · There are two types of annotation:
o
Build-in
Annotations
o
User-defined
Annotations
Build-in Annotations
- · Following build-in annotations are
provided from Java 5:
o
@Override
o
@Deprecated
o
@SuppressWarnings
o
Etc
- · Following built-in annotations are
provided from Servlet 3:
o
@WebServlet
o
@WebFilter
o
@WebListener
o
Etc
- · Following build-in annotations are
provided from EJB 3:
o
@Stateless
o
@Stateful
o
@Remote
o
@Entity
o
Etc
- · Following build-in annotations are
provided from Spring 2.5:
o
@Autowired
o
@Transactional
o
@Aspect
o
Etc
- · @Override:
o
@Override
annotation can be applied for the methods only.
o
It
is used by Java compiler to check if the annotated method really overrides a
method of an interface or the extended class.
- · @Deprecated
o
@Deprecated
annotation can be used on fields, methods, constructors or classes.
o
It
is used by Java compiler to check whether the element is outdated and should
not be used anymore.
o
Adding
@Deprecated to the class does not deprecate automatically all its fields and
methods.
- · @SuppressWarnings
o
The
@SuppressWarnings annotation can be used on fields, methods, constructors or
classes.
o
It
just tells compiler to ignore specific warnings compiler produces.
import java.util.Date;
public class Lab729 {
public static void main(String[] args) {
Date dt = new Date();
System.out.println(dt.
System.out.println(dt.
System.out.println(dt.
}
}
|
public class Lab730 {
public static void main(String[] args) {
Student st = new
System.out.println(st);
Student st1 = new Student(111, "Shailesh");
System.out.println(st1);
}
}
|
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Deprecated
}
public String toString() {
return id + "\t" + name;
}
}
|
Lab731.java
|
Lab732.java
|
public class Lab731 {
public static void main(String[] args) {
Student st = new Student(111, "Shailesh");
System.out.println(st);
}
}
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
public String tostring() {
return id + "\t" + name;
}
}
|
public class Lab732 {
public static void main(String[] args) {
Student st = new Student(111, "Shailesh");
System.out.println(st);
}
}
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String tostring()
{
return id + "\t" + name;
}
}
|
Lab733.java
|
|
public class Lab733 {
public static void main(String[] args) {
Student st = new Student(111, "Shailesh");
System.out.println(st);
}
}
|
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return id + "\t" + name;
}
}
|
Lab734.java
|
Lab735.java
|
import java.util.Date;
public class Lab734 {
public static void main(String[] args) {
Date dt = new Date();
System.out.println(dt.
}
}
|
import java.util.Date;
public class Lab735 {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Date dt = new Date();
System.out.println(dt.
}
}
|
Lab736.java
|
Lab737.java
|
public class Lab736 {
public static void main(String[] args) {
@SuppressWarnings("unused")
int a = 10;
System.out.println("main completed");
}
}
|
public class Lab731 {
@SuppressWarnings("unused")
public static void main(String[] args) {
int a = 10;
System.out.println("main completed");
}
}
|
Lab738.java
|
|
import java.util.ArrayList;
public class Lab738 {
@SuppressWarnings({ "unused", "rawtypes", "static-access" })
public static void main(String[] args) {
int ab = 10;
Hello h = new Hello();
System.out.println(h.a);
ArrayList al = new ArrayList<>();
}
}
|
class Hello {
static int a = 90;
}
|
User-defined Annotations:
- · Following are three main tasks in
Annotations implementation and uses.
o
Define
the annotation
o
Provide
the APT (Annotation Processing Tool)
o
Use
the annotation
- · Usually technology or framework vendors are responsible for defining the annotation and providing the annotation processing tool.
- · You cannot define constructors,
initialization blocks and methods within the annotation.
- · You can define classes, interfaces,
enums or annotations as the member of annotation.
- · All the annotations are sub type of
java.lang.annotation.Annotation interface.
Define the Annotation:
Syntax
|
Example
|
Usage
|
@interface
Annotation_Name{
datatype member_name();
datatype member_name();
}
|
@interface Author{
String name();
int age();
}
|
@Author(name=”Shailesh”,
age=30)
class Book{
……
}
|
Types of annotation:
- 1. Marker annotation
- 2. Single valued annotation
- 3. Multi valued annotation
Marker Annotation: Annotation which is defined without any members are called as Marker
Annotation.
Syntax
|
Example
|
@interface
ShaileshCloneable{}
|
@ShaileshCloneable
class Hello{
….
}
|
Single valued annotation: Annotations which are defined with only one member are called as single
valued annotation.
Example
|
Usage1
|
Usage2
|
@interface Author {
String value();
}
|
@Author(value=”sri”)
class Hello{
….
}
|
@Author(”sri”)
class Hello{}
|
@interface Author{
String name();
}
|
@Author(name=”sri”)
class Hello{
…
}
|
@Author(“sri”)
//invalid
class Hello{}
|
Multi-valued annotation: Annotations which are defined with two or more members are called as
Multi valued annotations.
Example
|
Usage
|
@interface Employee
{
int eid();
String ename();
long phone();
}
|
@Employee(eid=101,
ename=”Shailesh”, phone=99999999)
class Hello{
…
}
|
Default-valued annotation: Annotations which are defined with default value for the members of
annotation are called as default valued annotation.
Example
|
Usage
|
Usage
|
@interface Author{
String name() default “Shailesh”;
}
|
@Author(name=”Vikram”)
class Hello{
…
}
|
@Author
class Hello{}
|
Till now we have seen how to define
annotation and how to use the annotation. Now we have to develop Annotation
Processing Tool. We need the following to develop APT:
- · Reflection API related to annotation
- · Meta annotations
Reflection API related to annotations: Following are the methods provided in
java.lang.Class:
- · public boolean isAnnotation();
- · public Annotation
getAnnotation(class);
- · public boolean
isAnnotationPresent(class);
- · public Annotation[] getAnnotations();
Meta Annotations: Following are the meta
annotations provided in java.lang.annotation package:
- · @Retention
- · @Target
- · @Inherited
@Retention:
- · It will be used to specify the scope
of user defined annotation i.e visibility of user defined annotation.
- · Scope of annotation is also called
as RetentionPolicy.
- · We need to use the following
constants of java.lang.annotation.RetentionPolicy enum to specify the scope of
annotations or RetentionPolicy of annotations.
o
SOURCE
o
CLASS
o
RUNTIME
- · CLASS is default Retention Policy.
SOURCE
|
When the Retention
Policy is SOURCE then annotation will be available only in source code and
will not be available in byte code and native code.
Usage:
@Retention(RetentionPolicy.SOURCE)
@interface Author{}
|
CLASS
|
When the Retention
Policy is CLASS then annotations will be available only in source code and
byte code and will not be available in Native code.
Usage:
@Retention(RetentionPolicy.CLASS)
@interface Author{}
|
RUNTIME
|
When the Retention
Policy is RUNTIME then annotations will be available in source code, byte
code and Native code.
Usage:
@Retention(RetentionPolicy.RUNTIME)
@interface Author{}
|
2) @Target:
- · This annotation is used to specify
the Target, i.e to specify whether the annotation should be used for class or
method or field or constructor or all.
- · We need to use following constants
of java.lang.annotation.ElementType enum to specify the target of annotations.
o
PACKAGE
o
TYPE
o
FIELD
o
CONSTRUCTOR
o
METHOD
o
PARAMETER
o
LOCAL_VARIABLE
etc
- · When we are not specifying target
then that annotation cannot be used anywhere.
3) @Inherited
- · When we specify any annotation for
the class then that annotation will not be inherited in the sub class by
default.
- · If we want to inherit the super
class annotations to the sub class then annotation definition must be marked with
@Inherited.
Lab739.java
|
|
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
public class Lab739
{
public static void main(String[]
args) throws CloneNotSupportedException {
Employee emp1 =
new Employee(101, "Shailesh");
System.out.println(emp1);
Employee emp2 =
emp1.getClonedObject();
System.out.println(emp2);
System.out.println(emp1
== emp2);
}
}
|
@Retention(RetentionPolicy.RUNTIME)
@interface ShaileshClonable {
}
@ShaileshClonable
class Employee {
int id;
String name;
Employee(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ",
name=" + name + "]";
}
public Employee
getClonedObject() throws CloneNotSupportedException {
boolean b1 = this.getClass().isAnnotationPresent(ShaileshClonable.class);
if (b1) {
return new Employee(this.id, this.name);
}
throw new CloneNotSupportedException("Employee class not using ShaileshClonable");
}
}
|
Lab740.java
|
|
import
java.lang.annotation.ElementType;
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target;
public class Lab740
{
public static void main(String[]
args) {
Student st = new
Student();
Class<?
extends Student> c = st.getClass();
boolean isPresent
= c.isAnnotationPresent(Table.class);
if (isPresent) {
Table
t = (Table) c.getAnnotation(Table.class);
String
tableName = t.value();
System.out.println("Table
name: " + tableName);
} else {
System.out.println("Table
annotation is not present");
}
}
}
|
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Table {
String value();
}
@Table("student_table")
class Student {
}
|
Lab741.java
|
|
import
java.lang.annotation.ElementType;
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target;
import
java.lang.reflect.Field;
public class Lab741
{
public static void main(String[]
args) {
Faculty f1 = new
Faculty();
Class<?
extends Faculty> cl = f1.getClass();
Field[] fields =
cl.getDeclaredFields();
for (Field field
: fields) {
boolean
pre = field.isAnnotationPresent(Column.class);
if
(pre) {
Column
col = field.getAnnotation(Column.class);
String
name = col.name();
boolean
charType = col.charType();
System.out.println(field.getName()
+ "\t" + name + "\t" + charType);
}
else {
System.out.println("For
" + field.getName() + " column annotation is not present");
}
}
}
}
|
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Column {
String name();
boolean charType() default false;
}
class Faculty {
@Column(name = "facultyid", charType = true)
int id;
@Column(name = "facultyname")
String name;
long phone;
}
|
No comments:
Post a Comment