Wednesday, 9 March 2016

                             ANNOTATIONS  CLEAR EXPLANATION WITH EXAMPLES
                            ==================================================

     
                                                    ANNOTATIONS IN JAVA
                                                   ====================


WHAT IS ANNOTATION?
WHAT IS REQUIREMENT TO GO FOR ANNOTATION?

ANNOTATION IS NEW FEATURE PRIVIDED BY JAVA PROGRAMMING LANGUAGE

IT CAN BE USED TO PROVIDE SOME META DATA IN JAVA PROGRAMS.

META DATA IN THE SENSE DATA ABOUT DATA.


DATA ABOUT OUR CODING PART IS CALLED META DATA.


IN JAVA TO DESCRIBE META DATA WE HAVE ALREADY COMMENTS 

THEN WHY SHOULD GO FOR ANNOTAIONS.


FOR THIS ONE WE HAVE TO UNDERSTAND THE COMPILE STRUCTURE OF JAVA PROGRAM..


   .JAVA PROGRAM


   COMPILE ---> .CLASS FILE


GENERATING THE CLASS FILE IS DEPENDING ON HOW MANY CLASSES WE ARE DECLARING IN OUR PROGRAM..


AFTER EXECUTING WE ARE GETTING OUTPUT.


COMPILATION IS DONE BY SOME PHASES.


   1. PRE-PROCESSOR


   2. COMPILER


   3. ASSEMBLER

   4. LOADER/LINK EDITOR




THE ABOVE MACHINES WORK FOR COMPILATION.


IN JAVA COMPILATION, PRE-PROCESSOR IS NOT AVAILABLE.

ONLY COMPILER ,ASSEMBER,LOADER IS AVAILABLE.


IN JAVA PROGRAMMING, #INCLUDE STATMENTS NOT AVAILABLE.
ALL ARE AVAILABLE IN THE FORM OF PACKAGES.

TO RECOGNIZE PACKAGES COMPILER IS SUFFICIENT.





COMPILER IS EXISTED WITH 6 NO OF PHASES.


  1. LEXICAL ANALYSIS PHASE

  2. SYNTAX ANALYSIS

  3. SEMANTICS ANALYSIS PHASSE


  4. INTERMEDIATE CODE GENERATION

  5. CODE OPTIMISATION

  6. CODE GENERATION


WHAT IS THE RESPONSIBILITY FOR LEXICAL ANALYSIS PHASE?

1. THE BASIC FUNCTIONALITY FOR LEXICAL ANALYSIS PHASE IS


  1. SOURCE CODE INPUT TO COMPILER INPUT TO LEXICAL ANALYSIS PHASE


WILL TAKE THE COMPLETE SOURCE CODE AND STORING IN THE FORM OF A BUFFER IS CALLED BUFFERING.

WILL PERFORM TOKENIZATION OVER THE RESPECTIVE PROGRAMS.
FINALLY .CLASS WILL BE GENERATED


 1. BUFFERING

 2. TOKENIZATION


LEXICAL ANALYSIS WOULD BE REMOVED ALL THE COMMENTED DATA.

I WANT TO PROVIDE SOME DESCRIPTION TO MY PROGRAM.

I WANT TO BRING MY DESCRIPTION UP TO

  . JAVA
  .CLASS
  RUNTIME OF MY APPLICATION


TO SIMPLIFY MY DEBUGGING 

TO SIMPLIFY TESTING




IF WE TALK THE MAIN DIFFERENCE B/W COMMENTS AND ANNOTATION?


XML IS ALSO THERE TO DESCRIBE META DATA AND TO BRING UP TO RUNTIME
BEFORE JDK 1.5 TO BRING META DATA UPTO RUNTIME WE USED XML APPROACH.



  DRAWBACKS OF XML:
 ==================

BEING THE JAVA DEVELOPER, FIRST WE HAVE TO LEARN XML TECHNOLOGY SEPARATELY.


IF WE PROVDE META DATA USING XML FILE WE HAVE TO CHECK EACH AND EVERY TIME XML FILE IS LOCATED AND SAVED CORRECTLY OR NOT


XML FILE IS FORMATED OR NOT
?


USING RIGHT PARSING TECHNOLGY OR NOT
?

TO OVERCOME  ALL THESE PROBLEMS WE GO FOR ANNOTAIONS.


ANNOTATION IS A REPLACEMENT OF XML

XML BASED TECHNOLGY
===================

UPTO JDK1.4 VERSION
JDBC 3.x
SERVLETS 2.5
STRUTS 1.X
JSF 1.X
HIBERNATE 3.2.4
SPRING2.X
EJB2.X

BASIC SYNTAX AND TYPES OF ANNOTATIONS:
========================================
ANNOTATIONS ARE EXECUTED BY PREDEFINED TOOL CALLED 

APT(ANNOTATION PROCESSING TOOL)

TWO TYPES OF SYNTAXES:
======================
 1. DECLARTION SYNTAX

 2. UTILIZATION SYNTAX.

IF WE COME FOR DECLARTION SYNTAX, 

   @interface Annotation_name
   {
    //members
    }

member syntax:
==============
   datatype member_name() [default value]

Utilization syntax:
==================
in java we can use annotations for

  variables
  classes
  methods
  ....

@Annotation_Name(Member1=value1,member2=value2,..)
Programming Element
(variables,methods,classes...)

  @abc
  void display()
  {
   }



ON THE BASIS OF THE ANNOTATION MEMBERS, ANNOTATIONS ARE DEVIDED INTO LIKE FOLLOWING

 1. MARKER ANNOTATION
 2. SINGLE VALUED ANNOTATION
 3. MULTIVALUED ANNOTAION

MARKER ANNOTATION:
===================
 1. @Override

This annotaion is not having any member.

Single Valued Annotation:
=========================
Which contains only one member.




@SupressWarnings("unchecked")

Multivalued Annotaion:
=======================

@WebServlet(name:ms",urlpattern:("/abc"))

THis classification is done on the basis of members only.


Classifications of Annotations:
===============================
Annotations are devided into two types

 1. Standard annotations

 2. Custom Annotations

Standard annotations can be devided into two types.

  1. General purpose annotations
  2. meta annotations



General purpose annotations:
============================
 1. @Override
 2. @SupressWarnings
 3. @Depricated
 4. @FunctionalInterface----java1.8

Meta Annotations:
=================
 1. @Documented
 2. @Inherited
 3. @Target
 4. @Retention

we are having on overall standard annotations

Meta annotations:
=================
For preparing other annotations to give some nature we go for Meta annotations.

at custom annotations we are using these annotatations.

all these general purpose annotations are given some package is called

java.lang package.

but all the meta annotations are given in 

java.lang.annotation package.


for all these annotations we have some common and super type is available i.e

java.lang.annotation.Annotation

above one is super type of any annotation in java.

ALL THE ANNOTATIONS ARE INTERFACES.

@Override:
=========

Method overriding is a process of replacing super class method functionality with subclass method functionality.
example:
========
 class db_driver
 {
 public void getDriver()
 {
 System.out.println("type-1 driver");
  }
}
class new_db_driver extends db_driver
 {

 public void getDriver()
  {
 System.out.println("type-4 driver");
 }
}
class Test
 {
 public static void main(String[] args)
{
  db_driver driver = new new_db_driver();
  driver.getDriver();

 }
}


WHENEVER THE METHOD OVERRIDING IS FAILURE BECAUSE OF SMALL MISTAKES THERE I NEED TO GIVE SOME ERROR MESSAGE TO THE USER.


TO GIVE THIS MESSAGE or just notification WE ARE USING @Override annotaion at the above the subclass method called Method level annotation.

conclusion:
============

*********************

@SuppressWarnings:
==================
unchecked or unsafe operations

in general collections are able to store heterogenious objects.

whenever collection object is allowed to heterogenious obects type safety is not there.
immediately compiler is giving some warnings.


example:
========
import java.util.*;
class Bank
 {
  public ArrayList get_Customers_Details()
{
//@SuppressWarnings("unchecked")
  ArrayList al = new ArrayList();
   al.add("aaa");
   al.add("bbb");
   al.add("ccc");
   al.add("ddd");
   return al;
 }
}
class Test 
{
 public static void main(String[] args)
{
  Bank bank = new Bank();
   ArrayList list = bank.get_Customers_Details();
  System.out.println(list);
}
}

Conclusion:
===========
***************************

@Depricated annotation:
=======================
Java has provided some methods at the initial versions of the java. 

and laterversions some alternative methods will be provided.
whenever we are using any depricated methods at that time we are getting some depricated warning message.

example:
========
import java.awt.*;
class Test
 {
 public static void main(String[] args)
{
  Frame f = new Frame();
  f.show();//f.setVisible();

}
}

This show() is outdated methods provided in older versions of java.

alternative method for this method is 

f.setVisible(true);

example:
=========
class Employee
 {
//@Depricated
  public void gen_Salary()
  {
  System.out.println("salary is caluculated from basic,hr,ta");
 }
  public void gen_New_Salary()
 {
  System.out.println("salary is caluculated from basic, hr,ta,pf");
}
}
class TestDepricated
 {
 public static void main(String[] args)
 {
 Employee emp = new Employee();
 emp.gen_Salary();
 }
}


java 8 feature 
===============
@FunctionalInterface Annotation:
=================================

If we declare any interface without any methods we can call it as Marker interface.

if we declare any interface with only one abstract method i.e Functional Interface.

It contains exactly one abstract method i.e  known as Functional Interface.

Example:
========
@FunctionalInterface
interface Account
 {
   public void getAccount();
   public void getBankDetails();
 }
class Test
 {
 public static void main(String[] args)
 {

 }
}

Meta Annotations:
=================
Annotations about annotations is called as Meta annotations.

Declaring annotations by using another annotations is called Meta Annotations.


all the meta annotations are available in java.lang.annotation package.

for all the annotations a common and default type is available i.e

java.lang.annotation.Annotation

1.@Inherited
2.@Documented
3.@Target
4.@Retention

example:
========
@Persistable
class EMployee
 {
 String eid;
  String ename;
 }
class Manager extends Employee
 {
 void getMgrDetails()
 {
 System.out.println(eid);
 System.out.println(ename);
 }
}

by default annotations are not inheritable. to make inheritable we have to use @Inherited annotation


for example
  @Inherited
  @Interface Persistable
  {


  }

i am going to apply this annotation to Employee class.

The meaning of persistable is permanently storing.

To make an annotations as inheritable annotation we must go for @Inherited annotation.

2.@Documented:
==============

@Persistable
class Employee
 {
  public String eid;
  public String ename;
  public Emploee(String eid)
  {
}
public Employee(String eid,String ename)
 {
 }
public void add()
 {
}
}

By default, in java annotations are not doccumented.

@Documented
@interface Persistable
  {

 }

@Target annotation:
======================

to define the target elements for the annotation to which we want to apply annotations.

we can apply annotations for all the programming elements.


syntax:
=======
@Target(ElementType val)

ElementType.TYPE ->class,Ac,INTERFACE
ElementType.FIELD -> VAR
ElementType.METHOD -> METHODS
ElementType.CONSTRUCTOR ->CONSTRUCTOR
EXAMPLE:
========
@Target(ElementType.TYPE,ElementType.FIELD)
@interface Persistable
{


}

@Retention annotation:
======================
annotations are available upto source,upto .class and upto runtime

syntax:
=======
@Retention(RetentionPolicy val)

RetentionPolicy.SOURCE
RetentionPolicy.CLASS
RetentionPolicy.RUNTIME


@Retention(RetentionPolicy.RUNTIME)
@interface Persistable
 {
  ====
 }

Custom annotations:
====================
custom annotations are the annotations defined by developers as per their application requirements.
if we want to define custom annotations we have to follow some steps.



1.define user defined annotation

2. utilize userdefined annotation in java application

3. access data from user defined annotations

example:
=========
//Course..java
import java.lang.annotation.*;
@Inherited
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Course
 {
  String cid() default "c-111";
  String cname() default "C Prgramming Lang";
  int cost() default 10000;
  }

//Student.java

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

    this.sid = sid;
    this.sname = sname;
    this.saddr = saddr;
   }
public void getStudentDetails()
 {
 System.out.println("Student Details");
 System.out.println("-----------------");
 System.out.println("Student Id     :"+sid);
 System.out.println("Student Name   :"+sname);
 System.out.println("Student Addr   :"+saddr);
}
}

=====================
//Test.java
import java.lang.annotation.*;
public class Test
 {
 public static void main(String[] args) throws Exception
 {
 Student s = new Student("s-111","Ram","Hyd");
 s.getStudentDetails();
 Class c = s.getClass();
 Annotation ann = c.getAnnotation(Course.class);
 Course crs=(Course)ann;
system.out.println("Student Course Details");
 System.out.println("======================");
 System.out.println("Course ID  :"+crs.cid());
 System.out.println("Course Name: "+crs.cname());
 System.out.println("Course cost: "+crs.ccost());
 }
}