© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.

Презентация:



Advertisements
Похожие презентации
© Luxoft Training 2013 Using Reflection API in Java.
Advertisements

1 © Luxoft Training 2012 Inner and anonymous classes.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
1/27 Chapter 9: Template Functions And Template Classes.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Multiples Michael Marchenko. Definition In mathematics, a multiple is the product of any quantity and an integer. in other words, for the quantities a.
2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Unit II Constructor Cont… Destructor Default constructor.
Mobility Control and one-X Mobile. Mobility Control User Configuration Mobile Call Control requires PRI-U, BRI or SIP (RFC2833) trunks in the IP Office.
© Luxoft Training 2013 Java Collections API. © Luxoft Training 2013 Collections hierarchy.
S5-1 PAT328, Section 5, September 2004 Copyright 2004 MSC.Software Corporation SECTION 5 RESULTS TITLE EDITOR.
RLC circuit. An RLC circuit (or LCR circuit) is an electrical circuit consisting of a resistor, an inductor, and a capacitor, connected in series or in.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
S11-1 PAT318, Section 11, March 2005 SECTION 11 ANALYSIS SETUP.
© The McGraw-Hill Companies, Inc., Chapter 4 Counting Techniques.
Copyright CCNA 2 Chapter 12 Configuring a Router By Your Name.
XjCharts A C++ / Java Statecharts Tool for Developers Experimental Object Technologies
Транксрипт:

© Luxoft Training 2013 Annotations

© Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info about it Class c = Class.forName(classname); // get the methods of this class Method[] methods = c.getMethods(); // select a particular method Method m1 = method[3]; // get the name of this method m1.getName() // get the argument types of one of the methods, also its return type Class[] arginfo = m1.getParameterTypes(); int nargs = arginfo.length; Class rtnclass = m1.getReturnType(); // get the name of one of the arguments arginfo[i].getName(); // call the method Object[] args = new Object[nargs] Object val = m1.invoke(c, args); // result of calling is in val // get a typed variable for the result Float f; Integer i; if (val instanceof Float) f = (Float)val; if (val instanceof Integer) i = (Integer)val;

© Luxoft Training 2013 Annotations Annotations are metadata that can be added to the program source code without affecting it in a semantic way. However, they can be used during code analysis, compilation and even during run- time 8-3 Introduction

© Luxoft Training 2013 Annotations In contrast to JavaDoc comments that are destroyed in compile-time, annotations are reflective: they are stored in a class file and can be retrieved in run-time using reflection mechanism Annotations were introduced in Java version Introduction

© Luxoft Training 2013 Annotations What can be annotated: Class Method Class fields Arguments Packages Etc. (the full list is examined below) 8-5 Introduction

© Luxoft Training 2013 Annotations Main uses of annotations include: Provide information for the compiler; Provide metadata to various software tools to generate code, configurations and so forth; To be used in runtime processing to control program execution 8-6 Using annotations

© Luxoft Training 2013 Standard Annotations There are annotations that are predefined by the language @SuppressWarnings 8-7 Annotations used by the compiler

© Luxoft Training 2013 Standard : indicates that the marked element is deprecated and should no longer be used. Specified in class, method or field Many IDE analyze this annotation and mark (Eclipse underlines) the corresponding element

© Luxoft Training 2013 Standard Annotations

© Luxoft Training 2013 Standard informs the compiler that the element is meant to override an element declared in a superclass. If a method marked fails to correctly override a method in one of its superclasses (or if the method has been removed/renamed), the compiler generates an error

© Luxoft Training 2013 Standard Annotations

© Luxoft Training 2013 Standard tells the compiler to suppress specific warnings that it would otherwise generate

© Luxoft Training 2013 Create Custom Annotations To create an annotation use the : annotations are annotations for annotations 8-13 Introduction

© Luxoft Training 2013 annotation indicates the elements to which an annotation type is applicable and takes the following values: TYPE FIELD METHOD PARAMETER CONSTRUCTOR LOCAL_VARIABLE ANNOTATION_TYPE PACKAGE Create Custom Annotations

© Luxoft Training 2013 annotation indicates where the specified annotation is available and takes the following values: SOURCE annotations are only available in the source code and are to be discarded by the compiler Create Custom Annotations

© Luxoft Training 2013 CLASS annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time RUNTIME annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively Create Custom Annotations

© Luxoft Training Example Create Custom Annotations

© Luxoft Training Getting annotations via reflection Create Custom Annotations

© Luxoft Training 2013 Example AnnotationTutor