[go: up one dir, main page]

0% found this document useful (0 votes)
435 views33 pages

Java Means Durgasoft: DURGA SOFTWARE SOLUTIONS, 202 HUDA Maitrivanam, Ameerpet, Hyd. PH: 040-64512786

The document discusses Java annotations. It defines annotations and explains that they are used to represent metadata in Java applications. It notes that annotations allow metadata to be retained through compilation and at runtime, unlike comments which are removed during compilation. It also compares annotations to using XML for metadata, noting advantages of annotations like avoiding issues with XML formatting and parsing. The document then covers the syntax of defining and using annotations in Java and describes standard and custom annotations provided by Java. It provides examples of common standard annotations like @Override and @Deprecated.

Uploaded by

Shubh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
435 views33 pages

Java Means Durgasoft: DURGA SOFTWARE SOLUTIONS, 202 HUDA Maitrivanam, Ameerpet, Hyd. PH: 040-64512786

The document discusses Java annotations. It defines annotations and explains that they are used to represent metadata in Java applications. It notes that annotations allow metadata to be retained through compilation and at runtime, unlike comments which are removed during compilation. It also compares annotations to using XML for metadata, noting advantages of annotations like avoiding issues with XML formatting and parsing. The document then covers the syntax of defining and using annotations in Java and describes standard and custom annotations provided by Java. It provides examples of common standard annotations like @Override and @Deprecated.

Uploaded by

Shubh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

JAVA Means DURGASOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
1
JAVA Means DURGASOFT

Annotations
1.Introduction

2.Comment Vs Annotations

3.XML Vs Annotations

4.Types of Annotations

5.Standard Annotations

6.Custom Annotations

Annotation:

Annotation is a Java Feature provided by JDK 5.0 version,it can


be used to represent metadata in Java applications.

Q)In java applications,to describe metadata we have already


Comments then what is the requirement to go for Annotations?

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
2
JAVA Means DURGASOFT

 Ans:In Java applications,if we provide metadata by using


comments then "Lexical Analyzer" will remove comments
metadata from Java program as part of Compilation.
 As per the application requirement,if we want to bring
metadata upto .java file,upto .class file and upto RUNTIME
of our application then we have to use "Annotations".

Q)In Java applications,to provide metadata at runtime of Java


applications we are able to use XML documents then what is the
requirement to go for "Annotations"?

Ans:In Java applications,if we provide metadata by using XML


documents then we are able to get the following problems.

1.Every time we have to check whether XML documents are


located properly or not.

2.Every time we have to check whether XML documents are


formatted properly or not.

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
3
JAVA Means DURGASOFT

3.Every time we have to check whether we are using right


parsing mechanism or not to access the data from XML
document.

4.First Developers must aware XML tech.

To Overcome all the above problems,we have to use Java


alternative that is "Annotation".

Ex:

Servlet Configuration with XML document:

web.xml

<web-app>

<servlet>

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
4
JAVA Means DURGASOFT

<servlet-name>ls</servlet-name>

<servlet-class>LoginServlet</servlet-name>

</servlet>

<servlet-mapping>

<servlet-name>ls</servlet-name>

<url-pattern>/login</url-pattern>

</servlet-mapping>

</web-app>

Servlet Configuration with Annotation

@WebServlet("/login")

public class LoginServlet extends HttpServlet{

XML-Based Tech Annotation Based Tech

JDK1.4 JDK5.0
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
5
JAVA Means DURGASOFT

JDBC3.0 JDBC4.0

Servlets2.5 Servlets3.0
Struts1.x Struts2.x

JSF1.x JSF2.x

Hiberante3.2.4 Hibernate3.5

EJBs2.x EJBs3.x

Spring2.x Spring3.x

NOTE:Annotations are not the complete alternative for XML


tech,with in a Java application we can use annotations as
an alternative for XML documents but when we go for
distributed applications where applications are designed
on different tech.standards there annotations are not
possible,only we have to use XML tech.

 To process the annotations,Java has provided a predefined


tool in the form of "Annotation Processing tool"[APT},it was
managed by Java upto JAVA7 version,it was removed from
Java in JAVA8 version.

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
6
JAVA Means DURGASOFT

Syntax to declare annotation:

@interface Annotation_Name{

data_Type member_Name() [default] value;

Syntax to use Annotation:

@Annotation_Name(member_name1=value1,member_Name2=v
alue2.........)

 In Java,all the annotations are interfaces by default.


 In Java,the common and default super type for all the
annotations is "java.lang.annotation.Annotation".
 As per the no.of members inside annotations,there are three
types of annotations.

1.Marker Annotations:

It is an annotation with out members.

Ex:

@interface Override{

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
7
JAVA Means DURGASOFT

2.Single-Valued Annotation:

It is an Annotation with exactly one single member

Ex:

@interface SuppressWarnings{

String value();

3.Multi-Valued Annotation:

It is an annotation with more than one member.

Ex:

@interface WebServlet{

int loadOnStartup();

String[] urlPatterns();

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
8
JAVA Means DURGASOFT

-----

In Java Annotations are divided into the following two types as


per their nature.

1.Standard Annotations

2.Custom Annotations

1.Standard Annotations:

These are predefined Annotations provided by Java along with


Java software.

There are two types of Standard Annotations

1.General Purpose Annotations

2.Meta Annotations

1)General Purpose Annotations:

These Annotations are commonly used Annotations in Java


applications

These Annotations are provided by Java as part of java.lang


package.

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
9
JAVA Means DURGASOFT

Ex:

@Override

@Deprecated

@SuppressWarnings

@FunctionalInterface [JAVA 8]

2)Meta Annotations:

These Annotations can be used to define another Annotations.

These Annotations are provided by Java as part of


java.lang.annotation package.

Ex:

@Inherited

@Documented

@Target

@Retention

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
10
JAVA Means DURGASOFT

1.@Override:

 In Java applications if we want to perform method overriding


then we have to provide a method in subclass with the same
prototype of super class method.
 While performing method Overriding,if we are not providing
the same super class method at sub class method then
compiler will not rise any error and JVM will not rise any
exception,JVM will provide super class method output.

 In the above context,if we want to get an error about to


describe failure case of method overriding from compiler
then we have to use @Override

Ex:

class JdbcApp

public void getDriver(){

System.out.println("Type-1 Driver");

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
11
JAVA Means DURGASOFT

class New_JdbcApp extends JdbcApp

@Override

public void getdriver(){

System.out.println("Type-4 Driver");

class Test

public static void main(String args[]){

JdbcApp app=new New_JdbcApp();

app.getDriver();

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
12
JAVA Means DURGASOFT

 If we compile the above programme then compiler will rise


an error like "method does not override or implement a
method from a SuperType".

NOTE:If we compile the above programme,compiler will


recognize @Override annotation,compiler will take sub
class method name which is marked with @Override
annotation,compiler will comparesub class method name
with all the super class method names.If any method name
is matched then compiler will not rise any error.If no super
class method name is matched with sub class method
name then compiler will rise an error.

@Deprecated:

 The main intention of this annotation is to make a method


as deprecated method and to provide deprecation message
when we access that deprecated method.
 In general,Java has provided Deprecation support for only
predefined methods,if we want to get deprecation support
for the user defined methods we have to use @Deprecated
annotation.

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
13
JAVA Means DURGASOFT

NOTE:Deprecated method is an outdated method


introduced in the initial versions of Java and having
alternative methods in the later versions of Java.

class Employee{

@Deprecated

public void gen_Salary(int basic,float hq){

System.out.println("Salary is calculated on the basis of basic


amount,hq");

public void gen_Salary(int basic,float hq,int ta,float pf){

System.out.println("Salary is calculated on the basis of basic


amount,hq,ta,pf");

class Test{

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
14
JAVA Means DURGASOFT

public static void main(String args[]){

Empoloyee emp=new Employee();

emp.gen_Salary(25000,20.0f);

Note:If we compile the above code then compiler will


provide the following deprecation message "Note:java
uses or overrides a deprecated API"

@SuppressWarnings(--):

In Java applications,when we perform unchecked or unsafe


operations then compiler will rise some warning messages.In this
context,to remove the compiler generated warning messages we
have to use @SuppressWarnings("unchecked") annotation.

Ex:

import java.util.*;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
15
JAVA Means DURGASOFT

class Bank{

@SuppressWarnings("unchecked")

public ArrayList listCustomers(){

ArrayList al=new ArrayList();

al.add("chaitu");

al.add("Mahesh");

al.add("Jr NTR");

al.add("Pavan");

return al;

class Test{

public static void main(String args[]){

Bank b=new Bank();

List l=b.listCustomers();

System.out.println(l);

}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
16
JAVA Means DURGASOFT

@FunctionalInterface:

 In Java,if we provide any interface with out abstract


methods then that interface is called as "Marker Interface".
 In Java,if we provide any interface with exactly one abstract
method then that interface is called as "Functional
Interface".
 To make any interface as Functional Interface and to allow
exactly one abstract method in any interface then we have
to use "@FunctionalInterface" annotation.

NOTE:This annotation is a new annotation provided by


JAVA8 version.

@FunctionalInterface

interface Loan{

void getLoan();

class GoldLoan implements Loan{

public void getLoan(){

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
17
JAVA Means DURGASOFT

System.out.println("GoldLoan");

class Test{

public static void main(String args[]){

Loan l=new GoldLoan();

l.getLoan();

@Inherited:

In general all the annotations are not inheritable by default.

If we want to make/prepare any annotation as Inheritable


annotation then we have to declare that annotation as Inheritable
annotation then we have to declare that annotation with

@Inherited annotation.

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
18
JAVA Means DURGASOFT

Ex:

@interface Persistable

@Persistable

class Employee

class Manager extends Employee{

In the above example,only Employee class objects are Persistable


i.e eligible to store in database.

@Inherited

@interface Persistable

}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
19
JAVA Means DURGASOFT

@Persistable

class Employee{

class Manager extends Employee{

NOTE:In the above example,both Employee class objects


and manager class objects are Persistable i.e eligible to
store in database.

@Documented:

In Java,by default all the annotations are not documentable.

In Java applications,if we want to make any annotation as


documentable annotation then we have to prepare the respective
annotation with @Documented annotation.

Ex:

@interface Persistable
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
20
JAVA Means DURGASOFT

@Persistable

class Employee

javadoc Employee.java

If we prepare html documentation for Employee class by using


"javadoc" tool then @Persistable annotation is not listed in the
html documentation.

@Documented

@interface Persistable

@Persistable

class Employee
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
21
JAVA Means DURGASOFT

If we prepare html documentation for Employee class by using


"javadoc" tool then @Persistable annotation will be listed in the
html documentation.

@Target:

The main intention of this annotation is to define a list of target


elements to which we are applying the respective annotation.

Synatx:

@Target(--list of constants from ElementType enum---)

Where ElementType enum contains


FIELD,CONSTRUCTOR,METHOD,TYPE[Classes,abstract
classes,interfaces]

Ex:

@Target(ElementType.TYPE,ElementType.FIELD,ElementType.ME
THOD)

@interface persistable

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
22
JAVA Means DURGASOFT

@Persistable

class Employee

@Persistable

Account acc;

@Persistable

public Address getAddress(){}

@Retention:

The main intention of the annotation is to define life time of the


respective annotation in Java application.

Syntax:

@Retention(---a constant from RestentionPolicy enum---)

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
23
JAVA Means DURGASOFT

Where RestentionPolicy enum contains the constants like


SOURCE,CLASS,RUNTIME

@Retention(RetentionPolicy.RUNTIME)

@interface Persistable

@Persistable

class Employee

Custom Annotations:

 These are the annotations defined by the developers as per


their application requirements.
 To use custom annotations in java applications,we have to
use the following steps.

1)Declare user defined Annotation:

Bank.java
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
24
JAVA Means DURGASOFT

import java.lang.annotation.*;

@Inherited

@Documented

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@interface Bank

String name() default "ICICI Bank";

String branch() default "S R Nagar";

String phone() default "040-123456";

2)Utilize User defined Annotations in Java applications:

Account.java

@Bank(name="Axis Bank",phone="040-987654")

public class Account{

String accNo;

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
25
JAVA Means DURGASOFT

String accName;

String accType;

public Account(String accNo,String accName,String accType){

this.accNo=accNo;

this.accName=accName;

this.accType=accType;

public void getAccountDetails(){

System.out.println("Account Details");

System.out.println("----------------");

System.out.println("Account Number:"+accNo);

System.out.println("Account Name :"+accName);

System.out.println("Account Type :"+accType);

3)Access the data from User-Defined Annotation in main Application:

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
26
JAVA Means DURGASOFT

1)If the annotation is class level annotation then use the following
steps:

a)Get java.lang.Class object of the respective class

b)Get Annotation object by using getAnnoataion(Class c)


method

2)If the annotation is Field level annotation then use the following
steps:

a)Get java.lang.Class object of the respective class

b)Get java.lang.reflect.Field class object of the respective


variable from java.lang.Classby using getField(String field_name)
method

c)Get Annotation object by using getAnnotation(Class c)


method from java.lang.reflect.Field class.

3)If the annotation is Method level annotation then use the


following steps:

a)Get java.lang.Class object of the respective class

b)Get java.lang.reflect.Method class object of the respective


Method from java.lang.Class by using getMethod(String
method_name) method

c)Get Annotation object by using getAnnotation(Class c)


method from java.lang.reflect.Method class.

MainApp.java

import java.lang.annotation.*;

import java.lang.reflect.*;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
27
JAVA Means DURGASOFT

public class MainApp{

public static void main(String args[])throws Exception{

Account acc=new Account("abc123","Durga","Hyd");

acc.getAccountDetails();

System.out.println();

Class c=acc.getClass();

Annotation ann=c.getAnnotation(Bank.class);

Bank b=(Bank)ann;

System.out.println("Bank Details");

System.out.println("------------");

System.out.println("Bank Name :"+b.name());

System.out.println("Branch Name :"+b.branch());

System.out.println("Phone "+b.phone());

NOTE:class Level Annotation

Method Level Annotation


DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
28
JAVA Means DURGASOFT

Course.java:

import java.lang.annotation.*;

@Inherited

@Documented

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@interface Course{

String cid() default "C-111";

String cname() default "Java";

int ccost() default 5000;

Student.java:

public class Student{

String sid;

String sname;

String saddr;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
29
JAVA Means DURGASOFT

public Student(String sid,String sname,String saddr){

this.sid=sid;

this.sname=sname;

this.saddr=saddr;

@Course(cid="C-222",cname=".NET")

public void getStudentDetails(){

System.out.println("Student Details");

System.out.println("---------------");

System.out.println("Student Id :"+sid);

System.out.println("Sudent Name :"+sname);

System.out.println("Student Address:"+saddr);

ClientApp.java

import java.lang.annotation.*;

import java.lang.reflect.*;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
30
JAVA Means DURGASOFT

public class ClientApp{

public static void main(String args[])throws Exception{

Student std=new Student("S-111","Durga","Hyd");

std.getStudentDetails();

System.out.println();

Class cl=std.getClass();

Method m=cl.getMethod("getStudentDetails");

Annotation ann=m.getAnnotation(Course.clas);

Course c=(Course)ann;

System.out.println("Course Details");

System.out.println("---------------");

System.out.println("Course Id :"+c.cid());

System.out.println("Course Name :"+c.cname());

System.out.println("Course Cost :"+c.ccost());

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
31
JAVA Means DURGASOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
32
JAVA Means DURGASOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
33

You might also like