It allows you to get information about the other files in a java project. The reflection library is essentially a classpath scanning tool. I just want to document my progress and hopefully help someone else along the way. This is my fair warning that I am not an expert on the reflection library. However, as we all know, with great power comes great responsibility. This library contains features that can give you immense power when used correctly. It throws another exception, called InstantiationException, typically, if the Class represents an abstract class, interface, array class, primitive type, Void, or if it does not have a no-argument constructor either default or provided by the programmer.In this series I want to take a deep dive into the realm of the Java reflection library. It throws IllegalAccessException if the class or no-argument constructor is inaccessible. The newInstance() method is used to instantiate an object that the current class Class represents. Similarly, we can retrieve field information of the Integer class. The getDeclaredFields() method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object, and so on.įor example, we easily can list all the public methods of the Object class as follows: Method methods = Object. Similarly, the getModifiers() method returns Java language modifiers the getConstructors() method returns an array which contains Constructor objects reflecting all the public constructors of the class represented by this Class object. For example, the getName() method of the Class can be used to obtain the full qualified name of the class it represents. The Class class provides a set of methods to get the class name, its methods, modifiers, constructors, and so on. Alternatively, the static forName() method defined in the Class also can be used to load an unknown class and get a reference of its Class object. We can obtain a reference of the instantiated Class object by using this method. As we know, the Object class is the parent of all the classes in Java it offers a method called getClass(). Serializable, GenericDeclaration, Type, AnnotatedElementĪny class loaded into Java Runtime creates an instance of Class which represents the loaded class or an interface. public final class Class extends Object implements It is a final class hence, it cannot be extended. The class named Class is the most important class in the Reflection API and the basis of introspection and reflection mechanism. Structurally, the APIs are not different from the Java language and comprise all the language elements such as classes, methods, fields, constructors, interfaces, annotations, and so forth. This is done dynamically the compiler has no knowledge of the classes that are loaded at runtime. Using this API an executing code can ask an object for its class, explore its methods, the parameters it takes, return types, and constructors. It enables us to explore not only the intrinsic nature of the class but also instantiate one without using an explicit new operator at runtime. The Reflection API is a part of the Standard Java API Library. This article attempts to explore some of the intricacies and glimpses upon its effective use. This powerful feature can be used to expand the capabilities of a program, to examine the class or object internals at runtime. It can be used to inspect, modify, and transform a Java program without affecting existing code. Reflection is a built-in mechanism to introspect a Java program at the time of execution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |