© 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