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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
1 Inheritance Classes and Subclasses Or Extending a Class.
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Object Oriented Programming with Java
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – week 7, 2010 • Pakages • Looking back.
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
COSC 2006 Data Structures I Instructor: S. Xu URL:
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Abstract Class, Packages and interface from Chapter 9
Introduction to Programming G51PRG University of Nottingham Revision 1
Yoshi
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Java 1.5 Annotations. Motivation Computer scientists and engineers are always trying to add new features to programming languages Sometimes they are genuine.
Chapter 4 Code Editor Goals and Objectives Program more efficiently? How can you speed up your development process? Do you want to learn useful shortcuts.
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.
The Java Programming Language
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Chapter 9 Inheritance and Polymorphism
Enumerations & Annotations
Enumerations & Annotations
Java Programming Language
Java – Inheritance.
Enumerations & Annotations
Java Programming Course
CSE 331 Annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Java Annotations.
Presentation transcript:

Lecture 6 Annotations Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki References: Introduction-to-Java-Annotations.htm

Annotations 2 Annotations provide data about a program that is not part of the program itself. Annotations have a number of uses, among them: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth. Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements. Runtime processing — Some annotations are available to be examined at runtime. They have no direct effect on the operation of the code they annotate.

Declaring annotations 3 The annotation may be defined on its own line, and may include elements with named or unnamed name = "Benjamin Franklin", date = "3/27/2003" ) class MyClass() { name = "Benjamin Franklin", date = "3/27/2003" ) class MyClass() { = "unchecked") void myMethod() { = "unchecked") void myMethod() { } If there is just one element named "value," then the name may be void myMethod() { void myMethod() { } if an annotation has no elements, the parentheses may be void mySuperMethod() { void mySuperMethod() { }

Documentation and annotations 4 Many annotations replace what would otherwise have been comments in code. Suppose that a software group has traditionally begun the body of every class with comments providing important information: public class Generation3List extends Generation2List { // Author: John Doe // Date: 10/17/2011 // Current revision: 6 // Last modified: 10/21/2011 // By: Jane Doe // Reviewers: Alice, Bill, Cindy // class code goes here } public class Generation3List extends Generation2List { // Author: John Doe // Date: 10/17/2011 // Current revision: 6 // Last modified: 10/21/2011 // By: Jane Doe // Reviewers: Alice, Bill, Cindy // class code goes here } To add this same metadata with an annotation, you must first define the annotation ClassPreamble { String author(); String date(); int currentRevision() default 1; String lastModified() default "N/A"; String lastModifiedBy() default "N/A"; String[] reviewers(); // Note use of array ClassPreamble { String author(); String date(); int currentRevision() default 1; String lastModified() default "N/A"; String lastModifiedBy() default "N/A"; String[] reviewers(); // Note use of array }

Documentation and annotations 5 The annotation type definition looks somewhat like an interface definition where the keyword interface is preceded by character = "AT" as in Annotation Type). Annotation types are, in fact, a form of interface. The body of the annotation definition above contains annotation type element declarations, which look a lot like methods. Note that they may define optional default values. Once the annotation type has been defined, you can use annotations of that ( author = "John Doe", date = "10/17/2011", currentRevision = 6, lastModified = "10/21/2011", lastModifiedBy = "Jane Doe", reviewers = {"Alice", "Bob", "Cindy"} // Note array notation ) public class Generation3List extends Generation2List { // class code goes here ( author = "John Doe", date = "10/17/2011", currentRevision = 6, lastModified = "10/21/2011", lastModifiedBy = "Jane Doe", reviewers = {"Alice", "Bob", "Cindy"} // Note array notation ) public class Generation3List extends Generation2List { // class code goes here }

Annotations with javadoc 6 To make the information appear in Javadoc generated documentation, you must annotate definition itself with annotation import java.lang.annotation.*; // import this @interface ClassPreamble { // Annotation element definitions }

Annotations and the compiler 7 There are three annotation types that are predefined by the language @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with annotation. When an element is deprecated, it should also be documented using the tag, as shown in the following example. note that the Javadoc tag starts with a lowercase "d" and the annotation starts with an uppercase "D". // Javadoc comment follows /** * explanation of why it was deprecated static void deprecatedMethod() { } }

Annotations and the compiler annotation informs the compiler that the element is meant to override an element declared in a superclass // mark method as a superclass method // that has been int overriddenMethod() { } While it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked fails to correctly override a method in one of its superclasses, the compiler generates an error

Annotations and the compiler 9 Annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the example below, a deprecated method is used and the compiler would normally generate a warning. In this case, however, the annotation causes the warning to be suppressed. // use a deprecated method and tell // compiler not to generate a void useDeprecatedMethod() { objectOne.deprecatedMethod(); //deprecation warning - suppressed } Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "deprecation"}) The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax:

Annotation Processing 10 The more advanced uses of annotations include writing an annotation processor that can read a Java program and take actions based on its annotations. To make annotation information available at runtime, the annotation type itself must be annotated as follows: AnnotationForRuntime { // Elements that give information // for runtime processing }

@Retention annotation 11 The retention annotation indicates where and how long annotations with this type are to be retained. There are three values: RetentionPolicy.SOURCE – Annotations with this type will be by retained only at the source level and will be ignored by the compiler RetentionPolicy.CLASS – Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM RetentionPolicy.RUNTIME – Annotations with this type will be retained by the VM so they can be read only at run-time

@Retention annotation 12 In this example, the Retention(RetentionPolicy.RUNTIME) annotation indicates that your Test_Retention annotation is to be retained by the VM so that it can be read reflectively at Test_Retention { String doTestRetention(); }

@Retention Test_Documented { String doTestDocument(); } public class TestAnnotations { public static void main(String arg[]) { new TestAnnotations().doSomeTestRetention(); new TestAnnotations().doSomeTestDocumented(); (doTestRetention="Hello retention test") public void doSomeTestRetention() { System.out.printf("Testing annotation 'Retention'"); document") public void doSomeTestDocumented() { System.out.printf("Testing annotation 'Documented'"); }

Annotations and Reflection 14 If the annotation is specifies its retention policy as RUNTIME, it can be queried at runtime by any Java program using Reflection. import java.lang.annotation.*; MyAnnotation{ String key(); String value(); } import java.lang.annotation.*; MyAnnotation{ String key(); String value(); }

Annotations and Reflection 15 public class MyAnnotationTest value="java2novice.com") public void myAnnotationTestMethod(){ try { Class cls = this.getClass(); Method mth = cls.getMethod("myAnnotationTestMethod"); MyAnnotation myAnno = mth.getAnnotation(MyAnnotation.class); System.out.println("key: "+myAnno.key()); System.out.println("value: "+myAnno.value()); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } public static void main(String a[]){ MyAnnotationTest mat = new MyAnnotationTest(); mat.myAnnotationTestMethod(); } public class MyAnnotationTest value="java2novice.com") public void myAnnotationTestMethod(){ try { Class cls = this.getClass(); Method mth = cls.getMethod("myAnnotationTestMethod"); MyAnnotation myAnno = mth.getAnnotation(MyAnnotation.class); System.out.println("key: "+myAnno.key()); System.out.println("value: "+myAnno.value()); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } public static void main(String a[]){ MyAnnotationTest mat = new MyAnnotationTest(); mat.myAnnotationTestMethod(); }

default values of custom annotations 16 You may assign default values to annotation members, returned incase if you didn't provide any values to the annotation members. Make sure that the default values are same type as members. import java.lang.annotation.*; MyAnnotation { String key() default "site"; String value() default "java2novice.com"; } import java.lang.annotation.*; MyAnnotation { String key() default "site"; String value() default "java2novice.com"; }

Default values of custom annotations 17 public class public void myAnnotationTestMethod(){ try { Class cls = this.getClass(); Method mth = cls.getMethod("myAnnotationTestMethod"); MyAnnotation myAnno = mth.getAnnotation(MyAnnot.class); System.out.println("key: "+myAnno.key()); System.out.println("value: "+myAnno.value()); } catch (SecurityException e) {// TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) {// TODO Auto-generated catch block e.printStackTrace(); } public static void main(String a[]){ MyAnnotationTest mat = new MyAnnotationTest(); mat.myAnnotationTestMethod(); }

Making simple – single member annotation 18 We can assign the value without specifying the member name import java.lang.annotation.*; MyAnnotation{ String value(); } public class MyAnnotationTest public void myAnnotationTestMethod(){ try { Class cls = this.getClass(); Method mth = cls.getMethod("myAnnotationTestMethod"); MyAnnotn myAnno = mth.getAnnotation(MyAnnotn.class); System.out.println("value: "+myAnno.value()); } catch (Exception e) { e.printStackTrace(); } public static void main(String a[]){ new MyAnnotationTest().myAnnotationTestMethod(); }

@Inherited Annotation 19 It indicates that the annotated class with this type is automatically inherited. If you define an annotation with tag, then annotate a class with your annotation, and finally extend the class in a subclass, all properties of the parent class will be inherited into its myParentObject { boolean isInherited() default true; String doSomething() default "Do what?"; public Class myChildObject { } you do not have to define the interface methods inside the implementing class. These are automatically inherited because of using tag. Also you don’t have to remeber about equals(), hashCode(), toString() and annotationType() methods