java 反射 Reflection in Java
2017-07-21 10:58
274 查看
Reflection in Java
Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.The required classes for reflection are provided under java.lang.reflect package.
Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.
Reflection can be used to get information about –
Class The getClass() method is used to get the name of the class to which an object belongs.
Constructors The getConstructors() method is used to get the public constructors of the class to which an object belongs.
Methods The getMethods() method is used to get the public methods of the class to which an objects belongs.
Output :
The name of class is Test The name of constructor is Test The public methods of class are : method2 method1 wait wait wait equals toString hashCode getClass notify notifyAll The number is 19 The string is JAVA Private method invoked
Important observations :
We can invoke an method through reflection if we know its name and parameter types. We use below two methods for this purpose
getDeclaredMethod() : To create an object of method to be invoked. The syntax for this method is
Class.getDeclaredMethod(name, parametertype) name- the name of method whose object is to be created parametertype - parameter is an array of Class objects
invoke() : To invoke a method of the class at runtime we use following method–
Method.invoke(Object, parameter) If the method of the class doesn’t accepts any parameter then null is passed as argument.
Through reflection we can access the private variables and methods of a class with the help of its class object and invoke the method by using the object as discussed above. We
use below two methods for this purpose.
Class.getDeclaredField(FieldName) : Used to get the private field. Returns an object of type Field for specified field name.
Field.setAccessible(true) : Allows to access the field irrespective of the access modifier used with the field.
Advantages of Using Reflection:
Extensibility Features: An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
Debugging and testing tools: Debuggers use the property of reflection to examine private members on classes.
Drawbacks:
Performance Overhead: Reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently
in performance-sensitive applications.
Exposure of Internals: Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
Reference:
https://docs.oracle.com/javase/tutorial/reflect/index.html
This article is contributed by Akash Ojha.If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org.
See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
相关文章推荐
- 公共技术点之 Java 反射 Reflection
- JAVA 反射机制(Java Reflection)总结(一)
- Java反射经典实例 Java Reflection Cookbook
- Java-反射reflection
- Java动态性之反射机制(reflection)
- 测试 java反射Reflection中 获取 类属性、方法、构造方法及 调用类方法改变 对象属性
- 黑马程序员_Java基础:反射机制(Reflection)总结
- torque游戏引擎DTS文件的LOD实现(含reflection反射材质演示)-The LOD and reflection in DTS files
- JAVA 反射机制--reflection
- Java Reflection(反射) 入门学习笔记 之三 (Array)
- Java 反射Reflection使用
- Java反射经典实例 Java Reflection Cookbook
- JAVA的内省机制(introspector)与反射机制(reflection)[转]
- Java Reflection (JAVA反射)
- [2014-09-11]JAVA笔记_反射(Reflection)
- Java-Reflection反射-获取包括父类在内的所有字段
- Java反射(Reflection)
- Java反射经典实例 Java Reflection Cookbook
- JAVA反射 java reflection
- 【转】JAVA的内省(introspector)与反射(reflection)