What is the Reflection API in Java

The Java Reflection API and its applications

The Java Reflection API and its Applications Page 1 of 29 Faculty of Electrical Engineering and Computer Science SEMINAR WORK The Java Reflection API and its Applications Author: Michael Koch Subject: Virtual Platforms, Programming Languages ​​and Compiler Construction Supervising Lecturer: Prof. Volkhard Pfeiffer Submission Date: May 19, 2009 Die Java Reflection API and its Applications Table of Contents Page 2 of 29 Page Table of Contents ..................................... .................................................. ......................... 2 List of figures ....................... .................................................. ............................... 4 List of Annexes ................. .................................................. .......................................... 5 0. Problem ... .................................................. .................................................. ... 6 1. General information about reflection .................................... .................................................. .. 6 1.1. Definition according to Lahres and Ra & yacute; man ........................................... ........................ 6 1.2. Definition according to Dr. Forman ................................................. ................................ 7 1.3. Meta and base level objects .......................................... ............................... 7 1.4. The Meta Object Protocol (MOP) ......................................... ............................ 8 2. The Java Reflection API ............... .................................................. .............................. 9 2.1. Classes ................................................. .................................................. .......... 9 2.1.1. Get class objects ................................................ ........................... 9 2.1.2. Determine class hierarchy ................................................ ...................... 10 2.1.3. Create instances at runtime .............................................. ................ 11 2.1.4. Type comparison and type casting ........................................... ............ 12 2.2. Methods ................................................. .................................................. ..... 13 2.2.1. Which methods does a class have? .................................................. 13 2.2.2. Determine the signature of a method ............................................. ....... 14 2.2.3. Determine a method object based on the signature .................................. 14 2.2.4. Calling methods dynamically at runtime ............................................. 15 2.3. Attributes (Fields) .............................................. ................................................ 15 2.3 .1. Which attributes does a class have? ............................................ .......... 15 2.3.2. Determine attribute values ​​and change them dynamically ......................................... 16 2.4 . Modifiers and Annotations ............................................... ................................. 17 2.4.1. Modifier ................................................. .................................................. 17 2.4.2. Annotations ................................................. ............................................. 17 The Java Reflection API and its applications Page 3 of 29 3. Practical applications of reflection ..................................... ............................. 18 3.1. PlugIns ................................................. .................................................. ......... 18 3.2. Serialization ................................................. ................................................. 19 3.2.1. Realization with Java ............................................... ................................ 20 3.2.2. Realization with reflection ............................................... ....................... 20 3.3. Documentation of the source code ............................................... ....................... 21 3.4. Further applications ................................................ .................................... 21 4. Main advantages and disadvantages of Reflection .... .................................................. ... 22 5. Summary ........................................... .................................................. ....... 23 Glossary ......................................... .................................................. ................................... 24 Bibliography ............. .................................................. ............................................ 25 attachments .... .................................................. .................................................. ..................... 26 Declaration on honor ...................... .................................................. .......................... 29 The Java Reflection API and its applications List of Figures Page 4 of 29 Page Figure 1: Meta- and Base -Level ................................................ .................................... 7 Figure 2: Class diagram for vehicles ..... .................................................. ............ 9 Figure 3: Class diagram for a car radio interface ........................... ............ 18 Figure 4: Circle closure when serializing objects with the help of Reflection .. 20 The Java Reflection API and its applications Appendix list Page 5 of 29 Page Appendix 1: List of sample programs .. .................................................. ................... 27 Appendix 2: CD .......................... .................................................. ................................... 28 The Java Reflection API and its applications 0. Page 6 of 29 Problem This scientific work relates to the Reflection API of Java and its practical applications. In order to make the topic understandable to the reader, we first explain what is meant by the term reflection in general and what architecture the API is based on. Based on these principles, the most important functions of the Java Reflection API are then explained. A greatly simplified class model of an imaginary vehicle manufacturer serves as the basis for the code examples. For reasons of clarity, the handling of exceptions is dispensed with in the documentation, although this is necessary in some places. After the description of the API, a number of practical applications of this API follow, whereby these examples also relate to the model of a vehicle manufacturer. At the end of this seminar work, the main advantages and disadvantages of Reflection are compared and the entire topic is briefly and concisely summarized. This seminar paper requires a fundamental understanding of object-oriented programming as well as a basic knowledge of the English language. All sample programs are based on the Java Runtime Environment (JRE) Version 6 from Sun Microsystems. 1. General information about reflection 1.1. Definition according to Lahres and Ra & yacute; man “Reflection is a process in which a program accesses information that does not belong to the data of the program, but to the structure of the program itself. This information can be read out via a defined interface. ”1 1 / Lahres06 / S.538 The Java Reflection API and its applications 1.2. Page 7 of 29 Definition according to Dr. Forman "Reflection is the ability of a running program to examine itself and its software environment, and to change what it does depending on what it finds." 2 1.3. Meta and base level objects Figure 1: Meta and base level 3 2 3 / Forman05 / p. 3 cf. / Forman05 / p. 248 Figure A.4 The Java Reflection API and its applications Page 8 of 29 With that a program can determine its own structure or the structure of other classes, it is necessary that each class has information about its own structure. 4 This information is commonly known as metadata. Java and other object-oriented programming languages ​​store this metadata in objects, which in turn are referred to as meta-objects. A metaclass creates classes as instances, with Class being the only metaclass in Java. 5 metaclasses and their instances are summarized as meta-levels. The instances of a class are summarized as a base level. Interesting are the facts that Class is also derived from Object and that the class object of Class is an instance of itself 6 (circle closure). This can be proven by the following call (explanation under 2.1.4): System.out.println (Class.class.isInstance (Class.class)); 1.4. // true The Meta-Object-Protocol (MOP) 7 The interface to the meta-objects is called the Meta-Object-Protocol. The operations related to the MOP can be categorized into two areas: • • Introspection o List instance variables o List methods o Determine values ​​of the instance variables Intercession o Add new instance variables to a class o Add new methods to a class o Redefine methods o Interventions in the class hierarchy o Control of method calls The Java MOP is almost exclusively restricted to introspective operations. 4 / Forman05 / S.9 cf. / Forman05 / S. 249f 6 cf. / Forman05 / S. 25 7 Information refer to chapter A.4 from / Forman05 / S.248f 5 The Java Reflection API and its applications 2. Page 9 of 29 The Java Reflection API In this chapter, the following example is used to explain which possibilities Java offers to determine metadata and how this can be used. Figure 2: Class diagram for vehicles In order to be able to use the types presented in this chapter, the package java.lang.reflect. * Must be completely imported. 2.1. Classes 2.1.1. Obtaining Class Objects In Java, a class is viewed as an instance of the Class metaclass (see Figure 1). This instance is also known as a class object. “The Class class provides methods for querying properties of the class and allows classes to be loaded dynamically and instances to be generated dynamically. It is also the key to the functionality of the Reflection API. ”8 8 / Kr & uuml; ger08 / p. 1024 The Java Reflection API and its Applications Page 10 of 29 Class objects can generally be related in three ways become 9: • Directly via the class: Class & lt;? & gt; currentClass = Crane.class; • Via an instance of the class: Class & lt;? & Gt; currentClass = myCrane.getClass (); • About the name of the class: Class & lt;? & Gt; currentClass = Class.forName (& quot; Crane & quot;); "If the class name is known at compile time, the abbreviated notation .class can be used instead of calling forName (). This has the advantage that the compiler can already check whether the named class is available. ”10 An interesting aspect of Java is that even primitive data types (even void), which do not actually derive from Object, are still given by a Class object are represented. However, it must be noted here that the class object of a wrapper class is not the same as the class object of the corresponding primitive data type. However, every wrapper class has the attribute TYPE, which represents the class object of the corresponding primitive data type. The following applies: double.class = Double.TYPE ≠ Double.class 2.1.2. Determining the class hierarchy In Java, a class can at most be derived from one superclass. The class object of the superclass can be obtained using the getSuperclass () method. Class & lt;? & Gt; currentClass = Crane.class.getSuperclass (); The currentClass variable would now refer to the class object of the Truck class. In some cases it is necessary to go through the complete class hierarchy up to the class Object. An example of this would be the serialization of objects using reflection (see 3.2.2). 9 see also / SUN002 / / Kr & uuml; ger08 / p. 1034 10 The Java Reflection API and its Applications Page 11 of 29 The following example shows how to recursively run through the class hierarchy up to the class Object and gives the respective class name on the console out. public static void printClassHierarchy (Class & lt;? & gt; c) {if ((c == null) || (c == Object.class)) {return; } else {printClassHierarchy (c.getSuperclass ()); System.out.println (c.getSimpleName ()); }} Which interfaces are implemented by a class can be determined with the getInterfaces () method. However, this method only returns the class objects of the interfaces that are implemented in the class itself and ignores the interfaces that have already been implemented within the superclass. The following method lists all implemented interfaces. public static void printInterfaces (Class & lt;? & gt; c) {while (c! = null) {for (Class & lt;? & gt; i: c.getInterfaces ()) {System.out.println (i.getName ()); } c = c.getSuperclass (); }} 2.1.3. Creating instances at runtime Especially with regard to plug-ins (see 3.1) it is important that there must be a possibility of being able to create a new instance of the plug-in class during the runtime of the main program. If you want to create an instance using the default constructor, you can do this using the newInstance () method: Radio r = (Radio) Class.forName (& quot; SuperHiFiRadio & quot;). NewInstance (); // Radio and SuperHiFiRadio refer to the example from 3.1 The Java Reflection API and its Applications Page 12 of 29 If you want or have to create an instance using a parameterized constructor, you can do this as follows: Truck tr = (Truck) Class.forName (& quot; Crane & quot;). GetConstructor (double.class, double.class, double.class) .newInstance (70, 0, 12.5); The getConstructor () method must be passed the data types according to the signature of the constructor. This method then returns an object of the type Constructor, from which you can then create an instance of the class using the newInstance () method. Finally, the newInstance () method has to be passed the appropriate values ​​in the correct order. The getConstructors () method is used to obtain an array that contains all constructors of the class. The following code fragment lists these: public static void printConstructors (Class & lt;? & Gt; c) {for (Constructor & lt;? & Gt; constructor: c.getConstructors ()) System.out.println (constructor); } 2.1.4. Type comparison and type casting Two objects are always of the same type if they have the same class object. System.out.println (myCrane.getClass () == Crane.class); System.out.println (myCrane.getClass () == Vehicle.class); // true // false To determine whether an instance can be assigned to a variable of a certain type, there are two possibilities: • Using the instanceof operator (only possible if the type with which is compared , known at compile time) System.out.println (myCrane instanceof Vehicle); • // true via the isInstance () method. That is always possible, but not performant! System.out.println (Vehicle.class.isInstance (myCrane)); // true The Java Reflection API and its Applications Page 13 of 29 If you do not have a specific instance, you can use isAssignable () to check whether, theoretically, you can, for example, assign an instance of the Crane class to a Vehicle type variable ; could. System.out.println (Vehicle.class.isAssignableFrom (Crane.class)); // true A downcast can be carried out in two ways: • If the types are known at compile time. Vehicle v = (Vehicle) myCrane; • Using the cast () method. That is always possible, but not performant! Vehicle v = Vehicle.class.cast (myCrane); 2.2. Methods 2.2.1. Which methods does a class have? In Java, methods are encapsulated as objects of type Method. Which public methods a class has can be determined with the help of the getMethods () method, which returns an array with the corresponding method objects. All public methods that have been added at some point along the inheritance hierarchy are taken into account. Similarly, there is also the method getDeclaredMethods (), which, regardless of its modifiers, returns all method objects that were immediately declared in the examined class. The following method outputs all methods with their full signature on the console. This procedure could be used, for example, to document the source code. public static void printMethods (Class & lt;? & gt; c) {while (c! = null) {for (Method m: c.getDeclaredMethods ()) System.out.println (m); c = c.getSuperclass (); }} The Java Reflection API and its Applications Page 14 of 29 2.2.2. Determine the signature of a method The signature of a method consists of its name, the data types of its parameters (depending on their order) and their return data type. 11 Assuming that the object m represents any method, the signature information can be determined as follows: // Full name of the method (including package) String methodenName = m.getName (); // Name of the method without package specification String short method name = m.getSimpleName (); // All parameter data types in the correct order Class & lt;? & Gt; [] parameter = m.getParameterTypes (); // The return data type Class & lt;? & Gt; result = m.getReturnType (); 2.2.3.Determining a method object based on the signature The method object of a specific public method can be determined directly from its name, as in the following example: Method m = Crane.class.getMethod (& quot; getArmAngle & quot;); If a method has parameters, the class objects of the respective parameters must be specified in the correct order. In the case of primitive data types, however, one must note that their class objects are not the same as the class objects of the corresponding wrapper class (see 2.1.1). Method m = Crane.class.getMethod (& quot; setArmPosition & quot ;, double.class, double.class); In order to be able to access non-public method objects, one would have to filter this out of the array of getDeclaredMethods (). An example of how this can be done for attributes (or fields) is shown under 2.3.2. With methods one would have to check the complete signature for identity. 11 cf. / Kr & uuml; ger08 / p.170 The Java Reflection API and its applications Page 15 of 29 2.2.4. Calling methods dynamically at runtime The methods that are represented by a Method object can also be called dynamically at runtime. In order to do this, each method object has a method called invoke (), to which the instance to which the operation should refer must be passed as the first parameter. If it is a class method, zero must be passed here. If the method has parameters, the parameter values ​​must then be transferred. Invoke () returns the return value of the method as a lump sum as an object. In the case of primitive return data types, autoboxing takes place. The result of the two following commands is absolutely equivalent: myCrane.setArmPosition (30.5, 12.5); Crane.class.getMethod (& quot; setArmPosition & quot ;, double.class, double.class) .invoke (myCrane, 30.5, 12.5); In terms of runtime, however, the invoke () call is much slower. “In an experimental comparison, there were (...) differences by a factor of 10 to 100.” 12 Non-public methods can also be executed via reflection, provided that the method setAccessible (true) of the method object has been called beforehand has been. However, since this overrides the principle of abstraction, fatal, unforeseen errors can occur (see 4). 2.3. Attributes (Fields) 2.3.1. What attributes does a class have? Similar to methods, attributes are also encapsulated as objects. To determine this, there are the methods getFields (), which lists all public attributes, and the method getDeclaredFields (), which also lists non-public attributes. Similar to method objects, getDeclaredFields () is limited to the attributes declared in the class itself. A concrete public attribute can be determined based on its name via getField (& quot; Attribute name & quot;). 12 / Kr & uuml; ger08 / S.1055 The Java Reflection API and its applications Page 16 of 29 2.3.2. Determine attribute values ​​and change them dynamically Most attributes are declared as non-public for reasons of data encapsulation. Since you can usually not access this via getField (& quot; Attribute name & quot;), you have to use a “trick”: public static Field getFieldByName (Class & lt;? & Gt; c, String name) {Field field = null; for (Field f: c.getDeclaredFields ()) {if (f.getName (). equals (name)) {field = f; field.setAccessible (true); // Access private fields too}} return field; } To read out the value of the field, there are the methods getInt (), getDouble (),…, getObject (), to which the corresponding instance must be transferred for instance variables. With class variables you have to pass zero. To change the value of the field, there are analogously the methods setInt (), setDouble (), ..., setObject (), to which the instance or zero must also be passed. The concrete value must then be transferred as the second parameter. Crane myCrane = new Crane (70.0); //Max. Arm length 70.0m myCrane.setArmPosition (30.5, 40.5); // current arm length 40.5m Field maxLength = getFieldByName (Crane.class, & quot; maxArmLength & quot;); System.out.println (maxLength.getDouble (myCrane)); //Max. 70.0m maxLength.setDouble (myCrane, 10.0); System.out.println (maxLength.getDouble (myCrane)); //Max. 10.0m In this example, for example, the unforeseen condition occurs that the arm of the crane is extended further (40.5m) than it should be after the intervention (10.0m). In safety-critical applications, such an intervention could of course have fatal consequences! Direct access to the attributes should therefore only be used in exceptional cases (e.g. serialization using Reflection 3.2.2). The Java Reflection API and its Applications 2.4. Page 17 of 29 Modifiers and Annotations Modifiers and annotations can be defined for Class, Method, Field and Constructor objects. 2.4.1. Modifiers Modifiers are represented as an integer (int) value, which can be determined using getModifiers (). This value can be passed to one of the following static methods of the Modifier class, which then return true or false. Due to the triviality of the method names, there is no further explanation of the individual methods here. The most important methods for checking modifiers are: • isAbstract • isFinal • isInterface • isNative • isPrivate • isProtected • isPublic • isStatic int mod = Vehicle.class.getModifiers (); System.out.println (Modifier.isAbstract (mod)); // true System.out.println (Modifier.isStatic (mod)); // false System.out.println (Modifier.isNative (mod)); // false 2.4.2. Annotations Annotations provide the program with additional meta information without affecting the way the program works. 13 They are similar to how an interface is defined and are represented by a class object. Annotations are particularly suitable for documenting the source code (see 3.3). A frequently used annotation is @Deprecated, which indicates that a method should no longer be used. The following method checks whether this annotation has been set for a method or not: public static boolean isMethodDeprecated (Method m) {return m.getAnnotation (Deprecated.class)! = Null; } 13 cf. / Kr & uuml; ger08 / S.1047 The Java Reflection API and its Applications 3. Practical Applications of Reflection 3.1. PlugIns Page 18 of 29 A major problem with "statically" programmed programs is that all classes have to be available at compile time. Once the program has been compiled, changes and extensions can only be implemented by changing the source code of the existing program and completely recompiling it. This can be very tedious under certain circumstances if the program is widely used. It can also be the case that you want to offer third-party software developers the opportunity to expand your own program at certain, clearly defined points without having to make the complete source code of your own application available to these developers . The following example describes how a car manufacturer can provide various radio manufacturers with a plug-in interface: Figure 3: Class diagram for a car radio interface The Java Reflection API and its applications Page 19 of 29 Every vehicle either does not have one or exactly one board computer. This in turn has no or exactly one radio. Car stereos are offered by various manufacturers, from whom the source code of the vehicle manufacturer remains hidden. You only know the radio interface. A vehicle manufacturer, on the other hand, does not know any specific radio classes at compile time, as these may not be developed until much later than the vehicle manufacturer's software. For this reason, the on-board computer must use Reflection to create an instance of the specific radio using its class name during runtime (see 2.1.3). In this example it gets the class name from the bc.ini file, which a technician would have to configure once after installing the radio. Once this instance has been created, a cast on the radio type must be carried out, since the vehicle manufacturer only knows the semantics of its methods. The vehicle can now communicate with the dynamically generated radio instance using all methods that are defined by the radio interface. The methods louder (), quieter (), next () and previous () could be linked to a button on the steering wheel via the on-board computer. The values ​​from getInfoText () and getVolume () could be visualized on the screen of the board computer. The on-board computer also provides a method called shutdown (). This attempts to determine a method object for every device connected to the on-board computer using a method name specified in the bc.ini file (see 2.2.3). If such a method object was found, for example with the SuperHiFiRadio class, the method is now called via invoke () (see 2.2.4). The example described here is on the CD in the "examplePlugIn" package. 3.2. Serialization "By serialization we mean the ability to convert an object that exists in the main memory of the application into a format that allows the object to be written to a file or transported over a network connection." 14 14 / Kr & uuml; ger08 / S. 963 The Java Reflection API and its Applications Page 20 of 29 3.2.1. Realization with Java The java.io.Serializable interface is provided as standard in Java. If a class implements this interface, instances of it can be serialized. Unfortunately, this interface is not implemented by all classes because this functionality was either deliberately not intended by the developers of the class, or because not all aspects of using the class were taken into account in the planning. 3.2.2. Realization with Reflection The Java Reflection API offers the possibility of serializing instances of classes that do not implement the Serializable interface. To do this, you have to be able to read the values ​​of all attributes (including private and protected) that have been declared within the class hierarchy (see 2.1.2 and 2.3.2). This is usually not a problem with primitive data types and also with strings. However, all objects that are directly or indirectly referenced via another object must also be serialized recursively. However, this can lead to a circuit closure that ends in a non-terminating recursion if two objects refer to each other directly or indirectly via another object. Figure 4: Circle closure when serializing objects with the help of Reflection The Java Reflection API and its applications Page 21 of 29 Assuming that the vehicle API from Chapter 3.1 (see Figure 3) offers radios the possibility of accessing the on-board computer, to which they are connected, a radio could, for example, implement an anti-theft device by only working if it is operated with a specific on-board computer. In addition to the standard reference from the on-board computer to the radio, there would also be a reference from the radio to the on-board computer. When serializing the on-board computer, the radio would also be serialized. Since there is a reference to the board computer on the radio, this would also be serialized again. In order to solve the problem, one would have to note incidentally which objects have already been serialized. 3.3. Documentation of the source code Annotations (see 2.4.2) make it possible to provide the program with additional meta information during development, which can later be read out at runtime with the help of Reflection. As an example, you could provide methods during development with further information about their last change by creating an annotation that provides the personnel number, the last change date and a description of the changes. supply. Every time a developer changes something in the source code, he can note his changes in the annotation. With a documentation tool, you could later determine all methods and the associated information about their changes and, for example, have an HTML report created. The example described here is located on the CD in the “exampleAnnotation” package. 3.4. Other applications Reflection is mainly used in the following areas, which are not explained further here: • • • Java Beans Debug Tools Class-To-Class Transformation The Java Reflection API and its applications 4. Page 22 of 29 Essentials Advantages and disadvantages of Reflection 15 Reflection enables dynamic program extensions. With regard to plug-ins (see 3.1), additional classes are only available after the main program has been published and must be loaded dynamically at runtime. That would not be possible without reflection. Problems due to violation of data encapsulation If you access non-public methods or attributes with the help of the Reflection API, the program can go into an undefined state and no longer work properly. Reflection removes abstraction It may happen with direct access to internal components that the program no longer works when the examined class is updated, e.g. because an internal attribute has been removed or an internal method has been replaced by several other methods. Problems can be solved in a very general way.As you can dynamically determine all attribute values ​​of an instance, you could, for example, write a universally applicable hashCode method once and use it in every class. Long running time & AElig; Lower performance Since with Reflection certain data types can only be accessed dynamically at runtime, various optimizations of the Java VM cannot be carried out. Therefore, the execution time of operations is significantly longer here than in an implementation that dispenses with reflection. Documentation and test possibilities With Reflection, class browsers and test tools can be developed. Restrictions through security guidelines The methods of the Reflection API require certain rights at runtime, which are not available for applets, for example. 15 see also / SUN001 / The Java Reflection API and its Applications 5. Page 23 of 29 Summary Reflection is a technique with which a program can call up information about its own structure at runtime. To describe the program structure, metadata is required, which is provided in the form of meta objects. In Java, all classes are instances of the single class metaclass. Instances of classes are referred to as base-level objects, while the aforementioned elements are grouped under the term meta-level objects. With Reflection, class objects can be referenced by their name and it is possible to create instances of them at runtime. Methods, constructors, fields and annotations can be determined from a class object at runtime, both specifically via their name or signature, and as a complete list. These elements are also represented as objects and provide information about their declaration (e.g. modifier). Method objects can be executed dynamically at runtime, but this process takes much longer than a static call and should therefore be avoided if possible. With Reflection, it is also possible to access internal attributes (write), but this violates the principle of data encapsulation and abstraction and can lead to undefined states and the loss of the portability of the program. However, the Reflection API is indispensable with regard to dynamic extensions of the program (plug-ins). Their skills are also essential for documentation and debug tools. Ultimately, however, you should use reflection as sparingly as possible, since reflexively programmed solutions are significantly slower and more insecure than the equivalent static implementations, since the compiler has no way of changing the code with regard to its speed and type safety to optimize. The Java Reflection API and its Applications Page 24 of 29 Glossary Term Definition API API is the abbreviation for Application Programming Interface. This is a collection of interfaces and class definitions that a software provider makes available to other developers so that their programs can communicate with the software provider's program. Applet “Applets are also executable Java programs. In contrast to applications, they are called from an HTML page. ”16 There are some security restrictions that apply to them. Beans “In Java, independent, reusable software components for building applets and applications are called beans.” 17 Primitive data type Primitive data types are not objects.Java knows a total of 8 primitive data types: boolean, byte, short, int, long, float, double, char. Wrapper class For every primitive data type there is a class which encapsulates the primitive data type as an object. 16 17 / Kr & uuml; ger08 / S. 302 / Kr & uuml; ger08 / S.1057 The Java Reflection API and its Applications Page 25 of 29 Bibliography / Forman05 / Forman, I .; Forman, N .: JAVA Reflection In Action, Manning Publications Co., Greenwich (USA), 2005 / Konrad03 / Konrad, R .; Kothe, A .: TU Darmstadt, http://www.aop.informatik.tudarmstadt.de/pages/lectures/aood/ws03-04/download/reflection.pdf, as of: May 12, 2009 / Kr & uuml; ger08 / Kr & uuml; ger, G .; Stark, T .: Handbook of Java Programming, 5th edition, Addison-Wesley Verlag, Munich, 2008 / Lahres06 / Lahres, B .; Ra & yacute; man, G .: Praxisbuch Objektorientierung, 1st edition, Galileo Press, Bonn, 2006 / Lochbihler08 / Lochbihler, A .: Universit & auml; t Karlsruhe, http://pp.info.unikarlsruhe.de/lehre/SS2008/foo /reflection.pdf, status: 26.03.2009 / Middendorf02 / Middendorf, S .; Singer, R .; Heid J .: Java programming manual and reference, 3rd edition, Dpunkt Verlag, Heidelberg, 2002 / SUN001 / Without author; Sun Microsystems Inc., http://java.sun.com/docs/books/tutorial/reflect/index.html, as of: May 12, 2009 / SUN002 / Without author; Sun Microsystems Inc., http://java.sun.com/docs/books/tutorial/reflect/class/classNew.html, as of: May 12, 2009 / SUN003 / Without author; Sun Microsystems Inc., http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html, as of: May 13, 2009 / SUN004 / McCluskey, G .; Sun Microsystems Inc., http://java.sun.com/developer/technicalArticles/ALT/Reflection/, as of May 16, 2009 / Ullenboom09 / Ullenboom, C .: Java is also an island, 8th edition, Galileo Press, Bonn, 2009 The Java Reflection API and its applications Appendix Page 26 of 29 The Java Reflection API and its applications Appendix 1: Page 27 of 29 List of sample programs To illustrate the applications of the Reflection API, the following sample programs are available on the CD. Each of these programs is in its own package, which bears the name of the example. The respective main method is located in the Program.java file. exampleMetaInfos This program has a class called ClassInfo, which contains all the static methods from Chapter 2. These are called in the main method with specific objects so that their function can be traced on the console. examplePlugIn This program illustrates the example described under 3.1. The on-board computer of a vehicle loads the class name of the radio from a configuration file (bc.ini) and creates an instance of it at runtime. To understand the concept of this plug-in interface, you have to change the value "Radio" in the bc.ini file alternately to one of the two values ​​and then run the program again: • • radio manufacturerA.EasyRadio radio manufacturerB.SuperHiFiRadio example annotation This program shows how you can define your own annotation (that is not a topic of this work) and how you can read out its information with Reflection at runtime. In this example, methods of a class are provided with information about their last change, which can then be read out. exampleHashCode This program implements the hashCode method mentioned in chapter 4. It is practically a negative example of the use of reflection. The Java Reflection API and its Applications Appendix 2: CD Page 28 of 29 The Java Reflection API and its Applications Page 29 of 29 Declaration of Honor I hereby assure that I have written my seminar paper on the subject of The Java Reflection API and its Applications I wrote it myself and did not use any sources or resources other than those given. Place, date, signature