Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.

Slides:



Advertisements
Similar presentations
Lecture 6 Annotations Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Java 1.5 Annotations. Motivation Computer scientists and engineers are always trying to add new features to programming languages Sometimes they are genuine.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Fun with Java Annotations Brian McGinnis. Java Annotations Introduced in “Tiger” release (Java 1.5) Introduced in “Tiger” release (Java 1.5) One of most.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
CSC 142 Computer Science II Zhen Jiang West Chester University
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
1 Java Programming II Annotations and Reflection.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
1 CSE 331 Hash codes; annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Annotation trước tiên được hiểu là một dạng meta data. Meta data là đặc tả dữ liệu cho một đối tượng, giá trị gì đó. VD: các tập tin mp3, ảnh, hoặc một.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Final and Abstract Classes
Inheritance and Polymorphism
More on Java Generics Multiple Generic Types Bounded Generic Types
Fundamental of Java Programming
Interface.
Enumerations & Annotations
Enumerations & Annotations
Java Programming Language
Interfaces.
Chapter 12 Abstract Classes and Interfaces
Sampath Kumar.S Assistant Professor/IT, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Enumerations & Annotations
Java Programming Course
Applying OO Concepts Using Java
CSE 331 Annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Chapter 14 Abstract Classes and Interfaces
Java Annotations.
Java Annotations for Invariant Specification
CS 240 – Advanced Programming Concepts
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Java Annotations

Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in some special way by a compiler, development tool, deployment tool, or during runtime.  Annotation-based development is certainly one of the latest Java development trends  Annotations can be applied to several Java Elements like, package declarations, class, constructors, methods, fields, Variables and etc.

The Basics  Example to Define an Annotation (Annotation type) MyAnnotation { String doSomething(); }  Example to Annotate Your Code (Annotation) MyAnnotation (doSomething="What to do") public void mymethod() { }

class C Object field; Object p) { field = p; Object get() { return field; } Structure of Java Compiler Parser Source File Class File Writer Class File Error Type Checker Comments

class C Object field; Object p) { field = p; Object get() { return field; } Structure of Java5 Compiler Parser Annotation Checker Plugins Annotation Checker Class File Writer Class File Type Checker Error Source File Program with annotations

Annotation Types  Marker  Single-Element  Full-value or multi-value

Marker Marker annotations take no parameters. They are used to mark a Java element to be processed in a particular way.  Example: MyAnnotation { }  public void mymethod() {.... }

Single-Element  Single-element, or single-value type, annotations provide a single piece of data only. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis.  Example: MyAnnotation { String doSomething(); }  ("What to do") public void mymethod() {.... }

Full-value or multi-value  Full-value type annotations have multiple data members.  Example: MyAnnotation { String doSomething(); int count; String date(); }  (doSomething= "What to do", count=1, date=" ") public void mymethod() {.... }

The Built-In Annotations  Java defines seven built-in annotations.  Four are imported from java.lang.annotation   Three are included

Simple Annotations (or) Standard Annotations  There are three types of simple annotations provided by JDK5. They are: Override Deprecated Suppresswarnings

Override  Override is a Marker Annotation type that can be applied to a method to indicate to the Compiler that the method overrides a method in a Superclass. This Annotation type guards the programmer against making a mistake when overriding a method. For eg The syntax  Example Program: class Parent { public float calculate (float a, float b) { return a * b; } } Whenever you want to override a method, declare the Override annotatio n type before the method: public class Child extends Parent public int calculate (int a, int b) { return (a + 1) * b; } }

The Deprecated annotation  This annotation indicates that when a deprecated program element is used, the compiler should warn you about it. Example 2 shows you the deprecated annotation.  The syntax  Example : public class DeprecatedTest public void serve() { } } If you use or override a deprecated method, you will get a warning at compile time. public class DeprecatedTest2 { public static void main(String[] args) { DeprecatedTest test = new DeprecatedTest(); test.serve(); } }

The Suppresswarnings annotation  SuppressWarnings is used to suppress compiler warnings. You can to types, constructors, methods, fields, parameters, and local variables.  The syntax  Eg: import java.util.Date; public class Main public static void main(String[] args) { Date date = new Date(2009, 9, 30); System.out.println("date = " + date); } }

Documentation  public class Generation3List extends { // Author: Parag Shukla // Date: 01/12/2010 // Current revision: 6 // Last modified: 2/12/2010 // By: Parag Shukla // Reviewers: Kapil, Deven // class code goes here }

Documentation  Using ClassPreamble { String author(); String date(); int currentRevision() default 1; String lastModified() default "N/A"; String lastModifiedBy() default "N/A"; String[] reviewers(); ( author = “Parag Shukla", date = “01/12/2010", currentRevision = 6, lastModified = “2/12/2010", lastModifiedBy = “Parag Shukla", reviewers = {“Kapil", “Deven", “Ram"} )

The Target annotation be applied to any element of a class be applied to a field or property be applied to a method level annotation be applied to the parameters of a method be applied to constructors be applied to local variables that the declared type itself is an