Day 10 Annotation

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Java Annotations:

Java Annotations

Objectives:

Objectives At the end of this session, you will be able to Understand annotations Use predefined annotations Create custom annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Agenda:

Agenda Understanding Annotation s Pre Annotation Techniques Standard Annotation Custom Annotation Annotation Types Meta-Annotation Copyright © 2016 Tech Mahindra. All Rights Reserved.

Annotations :

So, what is annotation? Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Annotations :

Annotations in JAVA starts with @ sign Annotations are like meta-tags Organized data about the code, embedded within the code itself. You can add annotation to your code and apply it to Package declarations Type declarations Constructors Methods Fields Parameters & Variables NOTE: Annotations are f irst introduced in Java 5.0 Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Annotations :

public class AClass extends BClass { @Deprecated void oldMethod () { // do something in an old } } Annotations are not part of the program logic They may be evaluated: @ compile time by the compiler @ build / deployment time by annotation processors @ runtime by the program using reflection Annotations may be Built-in annotations User defined annotations Syntax @ <Name-of-annotation > [ <parameters> ] Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Before Annotations :

Pre Java-1.5 annotation techniques: – marker interfaces – modifiers – structured comments Ad-hoc mix of language elements and programming conventions Before Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Standard Annotations:

Predefined in java.lang – @Deprecated – @Override – @SuppressWarnings There are only three types of simple annotations provided by JAVA 5.0 Standard Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Standard Annotations :

a) @Deprecated May annotate classes, interfaces, methods Discourages programmers to use the element No advantage compared to the structured @deprecated -comment Copyright © 2016 Tech Mahindra. All Rights Reserved. Standard Annotations

Standard Annotations:

b) @Override Annotates methods Indicates that the annotated method overrides a declaration in a super type Is checked by the compiler May be used and checked by IDEs Copyright © 2016 Tech Mahindra. All Rights Reserved. Standard Annotations

Standard Annotations:

b) @Override - Example Copyright © 2016 Tech Mahindra. All Rights Reserved. Standard Annotations

Standard Annotations:

c) @ SuppressWarnings ( warning-category ) @ SuppressWarnings ({ list of warning-categories }) Annotates classes, methods, statements Indicates that the compiler should suppress within the annotated element all warnings in the warning category or any of the warning categories. Warning categories ➢ Examples: "all", "deprecation", "unchecked", "serial", ··· ➢ General: all warning categories that may be set using javac - Xlint may be suppressed using the annotation (The IDE will handle this) Copyright © 2016 Tech Mahindra. All Rights Reserved. Standard Annotations

Standard Annotations:

c) @ SuppressWarnings - Example class C { @SuppressWarnings ({" unchecked", "unused" }) private int g() { List l = new ArrayList(); return 0; } } Copyright © 2016 Tech Mahindra. All Rights Reserved. Standard Annotations

Standard Annotations:

Defined in package javax.annotation – Java 6 comprises annotations defined in package javax.annotation @ Generated @ PostConstruct @ PreDestroy @Resource @Resources – Intended usage Enterprise frameworks Copyright © 2016 Tech Mahindra. All Rights Reserved. Standard Annotations

Slide15:

Custom Annotations To create our own custom annotation, we will use an Annotation type The type is the actual construct used, and the annotation is the specific usage of that type. An annotation type definition takes an "at" (@) sign, followed by the interface keyword plus the annotation name. On the other hand, an annotation takes the form of an "at" sign (@), followed by the annotation type. Copyright © 2016 Tech Mahindra. All Rights Reserved.

Annotation Types:

Marker: Marker type annotations have no elements, except the annotation name itself. Single-Element: Single-element, or single-value type, annotations provide a single piece of data only. Multi-Value : Full-value type annotations have multiple data members . There are three annotation types: Annotation Types Copyright © 2016 Tech Mahindra. All Rights Reserved.

Annotation Types:

Annotation Types Copyright © 2016 Tech Mahindra. All Rights Reserved. a) Marker Annotation @interface MyMarkerAnno { } @ MyMarkerAnno class annotatedClass {···}

Annotation Types:

@interface MyValuedAnno { String value() default "default value" ; } @ MyValuedAnno class annotatedClass1 {···} @ MyValuedAnno (value= “Special value 1” ) class annotatedClass2 {···} b) Single-element annotation: Annotation with one element. Element may have default value Element definitions look like parameterless methods Annotation Types Copyright © 2016 Tech Mahindra. All Rights Reserved.

Annotation Types:

@ interface MyMultiValuedAnno { String[] value () default { “default string”,”42” }; int val2() default 42; } @MyMultiValuedAnno class annotatedClass1 {···} @MyMultiValuedAnno("Hello ", “11” ) class annotatedClass2 {···} b) Single-element annotation with array values Annotation with array elements Annotation Types Copyright © 2016 Tech Mahindra. All Rights Reserved.

Annotation Types:

@ interface MyMultiValuedAnno { String val1() default "default value"; int val2() default 42; } @MyMultiValuedAnno class a nnotatedClass1 {···} @ MyMultiValuedAnno (val1=" Hello ", val2=11) class a nnotatedClass2 {···} c) Multi-element annotation : Annotation with several elements Annotation Types Copyright © 2016 Tech Mahindra. All Rights Reserved.

Slide21:

Meta-Annotations Meta-annotations, which are actually known as the annotations of annotations, contain four types. These are: Target Retention Documented Inherited Note : They are built-in annotations imported from java.lang.annotation Copyright © 2016 Tech Mahindra. All Rights Reserved.

Slide22:

a. Target The target annotation indicates the targeted elements of a class in which the annotation type will be applicable It contains the following enumerated types as its value: @ 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 annotation type Meta-Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Slide23:

a. Target - Example // D efine an annotation named Test_Target with @Target metadata @ Target( ElementType.METHOD ) public @interface Test_Target { public String doTestTarget (); } // create a class that will use the Test_Target annotation: public class TestAnnotations { public static void main(String arg []) { new TestAnnotations (). doTestTarget (); } @ Test_Target ( doTestTarget ="Hello World !") public void doTestTarget () { System.out.printf ("Testing Target annotation"); } } Meta-Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Slide24:

b. Retention 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 retained only at the source level and will be ignored by the compiler RetentionPolicy.CLASS —Annotations with this type will be 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 Meta-Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Slide25:

b. Retention – Example @Retention( RetentionPolicy.RUNTIME ) public @interface Test_Retention { String doTestRetention (); } Meta-Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Slide26:

c . Documented 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 Meta-Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Slide27:

d. Inherited If an annotation is defined 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 Example @ Inherited // Define the annotaion public @interface myParentObject { boolean isInherited () default true; String doSomething () default "Do what?"; } @ myParentObject // Annotate a class with the annotation: public Class myChildObject { } Meta-Annotations Copyright © 2016 Tech Mahindra. All Rights Reserved.

Summary:

Summary In this session, we have covered Understanding Annotations Pre Annotation Techniques Standard Annotation Custom Annotation Annotation Types Meta-Annotation Copyright © 2016 Tech Mahindra. All Rights Reserved.

Thank you:

Thank you Disclaimer Tech Mahindra Limited, herein referred to as TechM provide a wide array of presentations and reports, with the contributions of various professionals. These presentations and reports are for informational purposes and private circulation only and do not constitute an offer to buy or sell any securities mentioned therein. They do not purport to be a complete description of the markets conditions or developments referred to in the material. While utmost care has been taken in preparing the above, we claim no responsibility for their accuracy. We shall not be liable for any direct or indirect losses arising from the use thereof and the viewers are requested to use the information contained herein at their own risk. These presentations and reports should not be reproduced, re-circulated, published in any media, website or otherwise, in any form or manner, in part or as a whole, without the express consent in writing of TechM or its subsidiaries. Any unauthorized use, disclosure or public dissemination of information contained herein is prohibited. Unless specifically noted, TechM is not responsible for the content of these presentations and/or the opinions of the presenters. Individual situations and local practices and standards may vary, so viewers and others utilizing information contained within a presentation are free to adopt differing standards and approaches as they see fit. You may not repackage or sell the presentation. Products and names mentioned in materials or presentations are the property of their respective owners and the mention of them does not constitute an endorsement by TechM. Information contained in a presentation hosted or promoted by TechM is provided “as is” without warranty of any kind, either expressed or implied, including any warranty of merchantability or fitness for a particular purpose. TechM assumes no liability or responsibility for the contents of a presentation or the opinions expressed by the presenters. All expressions of opinion are subject to change without notice. Copyright © 2016 Tech Mahindra. All Rights Reserved.