Java Programming Course

Slides:



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

George Blank University Lecturer.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
ASP.NET Programming with C# and SQL Server First Edition
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object-Oriented Analysis and Design
Java Course Outline Kumar Harshit, USW. Course Description Teaches students to program using the Java programming language with the help of the Netbeans.
Java 1.5 Annotations. Motivation Computer scientists and engineers are always trying to add new features to programming languages Sometimes they are genuine.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
JavaDoc1 JavaDoc DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY July 24, 2006 by Emil Vassev & Joey Paquet revision 1.2 –
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.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
Software Documentation Section 5.5 ALBING’s Section JIA’s Appendix B JIA’s.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Chapter 5 Introduction to Defining Classes
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
Creating a Java Application and Applet
Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
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.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
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.
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.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Modern Programming Tools And Techniques-I
Java Generics.
Software Construction Lab 10 Unit Testing with JUnit
Inheritance and Polymorphism
More on Java Generics Multiple Generic Types Bounded Generic Types
Chapter 3: Using Methods, Classes, and Objects
Fundamental of Java Programming
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Interface.
Enumerations & Annotations
Enumerations & Annotations
Java Programming Language
Interfaces.
Chapter 12 Abstract Classes and Interfaces
Inheritance Inheritance is a fundamental Object Oriented concept
Recitation 7 October 7, 2011.
Java Programming, Second Edition
Enumerations & Annotations
Applying OO Concepts Using Java
Tonga Institute of Higher Education
CSE 331 Annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Fundaments of Game Design
CS520 Web Programming Java Annotations
Inheritance and Polymorphism
Final and Abstract Classes
Java Annotations.
Java Annotations for Invariant Specification
Presentation transcript:

Java Programming Course Annotation

Session objectives Introduction Anotation - basic Annotation Types The Built-In Annotations from java.lang package @Override, @Deprecated, @SuppressWarnings. from java.lang.annotation @Target, @Retention, @Documented @Inherited Custom Annotations

Introduction Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate. 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. Runtime processing — Some annotations are available to be examined at runtime. Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements.

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

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

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

What can be annotated? Annotatable program elements: method field package class, including interface enum method field only at compile time local variable formal parameter

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

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

Annotation Types: 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: public @interface MyAnnotation { String value(); } Usage: @MyAnnotation ("What to do") public void mymethod() { ....

Annotation Types: Full-value (multi-value) Full-value type annotations have multiple data members. Example: Usage:

The Built-In Annotations Java defines seven built-in annotations. Four are imported from java.lang.annotation @Retention, @Documented, @Target, @Inherited. Three are included in java.lang. @Override, @Deprecated, and @SuppressWarnings.

Standard Annotations: The @Override annotation 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. Example Program: Whenever you want to override a method, declare the Override annotation type before the method:

Standard Annotations: The @Deprecated annotation This annotation indicates that when a deprecated program element is used, the compiler should warn you about it. If you use or override a deprecated method, you will get a warning at compile time.

The @Suppresswarnings annotation @SuppressWarnings is used to suppress compiler warnings. You can apply @SuppressWarnings to types, constructors, methods, fields, parameters, and local variables. Before After

Meta-Annotations Meta-annotations - types designed for annotating annotation-type declarations (annotations-of-annotations) Meta-annotations: @Target - indicates the targeted elements of a class in which the annotation type will be applicable TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, etc @Retention - how long the element holds onto its annotation SOURCE, CLASS, RUNTIME @Documented - indicates that an annotation with this type should be documented by the javadoc tool @Inherited - indicates that the annotated class with this type is automatically inherited Retention : sự duy trì

The Target annotation @Target(ElementType.TYPE)—can be applied to any element of a class @Target(ElementType.FIELD)—can be applied to a field or property @Target(ElementType.METHOD)—can be applied to a method level annotation @Target(ElementType.PARAMETER)—can be applied to the parameters of a method @Target(ElementType.CONSTRUCTOR)—can be applied to constructors @Target(ElementType.LOCAL_VARIABLE)—can be applied to local variables @Target(ElementType.ANNOTATION_TYPE)—indicates that the declared type itself is an

The @Retention Annotation 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

Annotation Processing It's possible to 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 with @Retention(RetentionPolicy.RUNTIME): Annotation data can be examined using reflection mechanism java.lang.reflect.AccessibleObject: <T extends Annotation> T getAnnotation(Class<T>) Annotation[] getAnnotations() boolean isAnnotationsPresent(<Class<? extends Annotation>)

Annotation Processing example (1)

Annotation Processing example (2)

The @Documented Annotation The documented annotation indicates that an annotation with this type should be documented by the javadoc tool. By default, annotations are not included in javadoc. But if @Documented is used, it then will be processed by javadoc-like tools and the annotation type information will also be included in the generated document.

The @Documented Annotation

The @Documented Annotation As you can see from the screenshot, there is no annotation-type information for the doSomeTestRetention() method. But, this description is provided for the doSomeTestDocumented() method. This is because of the @Documented tag attached with your Test_Documented annotation. Your earlier annotation Test_Retention did not include this tag.

The @Inherited Annotation This is a bit of a complex annotation type. It indicates that the annotated class with this type is automatically inherited. More specifically, if you define an annotation with the @Inherited 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 subclass

Custom Annotations To create an annotation type for classes Syntax: "@" symbol + interface keyword + annotation name Saved with a . java extension. Annotations will be compiled to .class files. To use a class level annotation, the annotation name will precede the class declaration. Parameters can be added to Annotation declaration. Will not have a null value. Can have a default value. Are written as simple methods. Can be arrays of comma-separated values of primitive data type.

Example of Annotation for Classes Output

Annotation for Methods Output

Reallife example – JPA Annotations When using JPA, you can configure the JPA behavior of your entities using annotations: @Entity - designate a plain old Java object (POJO) class as an entity so that you can use it with JPA services @Table, @Column, @JoinColumn, @PrimaryKeyJoinColumn – database schema attributes @OneToOne, @ManyToMany – relationship mappings @Inheritance, @DiscriminatorColumn – inheritance controlling

Reallife example – JUnit Annotations Annotations and support for Java 5 are key new features of JUnit 4: @Test – annotates test method @Before, @After– annotates setUp() and tearDown() methods for each test @BeforeClass, @AfterClass – class-scoped setUp() and tearDown() @Ignore – do not run test

Summary

Thank you all for your attention and patient ! That’s all for this session! Thank you all for your attention and patient ! 33/27