While the reflection provides a lot of advantages, it has some disadvantages too. Java reflection should always be used with caution. ![]() Let's see the simple example of forName() method. It should be used if you know the fully qualified name of class.This cannot be used for primitive types.There are 3 ways to get the instance of Class class. Returns the total number of constructors of this class.ġ1) public Method getDeclaredMethod(String name,Class parameterTypes)throws NoSuchMethodException,SecurityException Returns the total number of methods of this class.ġ0) public Constructor getDeclaredConstructors()throws SecurityException Returns the total number of fields of this class.ĩ) public Method getDeclaredMethods()throws SecurityException ![]() Loads the class and returns the reference of Class class.ģ) public Object newInstance()throws InstantiationException,IllegalAccessExceptionĨ) public Field getDeclaredFields()throws SecurityException This was only a brief post to get start to use.Commonly used methods of Class class: MethodĢ) public static Class forName(String className)throws ClassNotFoundException You can find more details about the reflection in each constructor, annotations. To access methods: Method method = aClass.getMethods() Fields Field method = aClass.getFields() Annotations Annotation annotations = aClass.getAnnotations() To access constructors: Constructor constructors = aClass.getConstructors() Methods If you need the complete list of interfaces, you will need to consult both class and superclasses recursively. obtain Class object, as above Class interfaces = aClass.getInterfaces() To get a list of interfaces you can use: Class aClass =. You can use: Class superclass = aClass.getSuperclass() Interfaces obtain Class object, as above Package package = aClass.getPackage() Superclass With the package you can have a look in the Manifest of the JAR and others. Modifier.isVolatile(int modifiers) Package Info The modifiers are packed into an int, you can use the methods with : Modifier.isAbstract(int modifiers) obtain Class object, as above int modifiers = aClass.getModifiers() To obtain the class modifiers: Class aClass =. String simpleClassName = aClass.getSimpleName() ![]() If you want only the name without the package: Class aClass =. To obtain the class name, you can try both: Class aClass =. obtain class name as string at runtime Class class = Class.forName(className) Class Name If you don’t know the name at compile time: String className =. If you know the name: Class myObjectClass = MyObject.class Class Objectįirst you need obtain the object. Class Modifies (public, private, synchronized etc.)įor a full list you should consult the JavaDoc for.What is happening here? It is obtaining the Class object from the class called MyObject, it is using the class object to get a list of methods in this class and print out their names.įrom the classes you can obtain information about Java’s static typing system isn’t really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called ‘doSomething’ and then call it if you want to. It is used to examine and modify the structure and code in the same system at runtime.įor example, say you have an object of an unknown type in Java, and you would like to call a ‘doSomething’ method on it if one exists. Java reflection it is useful to instantiate new objects, invoke methods and get/set field values without knowing the names of the classes/methods… and when you want to inspect classes, interfaces, fields and methods at runtime. Hi guys, I will write simplified about Java reflection and how you can access with constructors, interfaces, super class, etc…
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |