In-depth understanding of how Java reflection is called requires specific code examples
Introduction:
Java reflection (Reflection) is a powerful mechanism that allows programs to Dynamically obtain class information and manipulate classes and objects at runtime. The core class of Java reflection is the Class class, which provides a series of methods to obtain the constructors, methods, fields, etc. of the class, and can call them through reflection. This article will delve into the calling method of Java reflection and give specific code examples.
1. Obtain Class object
In Java, there are three ways to obtain Class object:
2. Use reflection to call the constructor
Use reflection to dynamically call the constructor of a class. The following is a code example to obtain the constructor and call it:
Class clazz = Person.class; Constructor constructor = clazz.getConstructor(String.class, int.class); Person person = (Person) constructor.newInstance("Tom", 18);
3. Use reflection to call member methods
Use reflection to dynamically call member methods of a class. The following is a code example to obtain and call member methods:
Class clazz = Person.class; Method method = clazz.getMethod("sayHello", String.class); Person person = new Person(); method.invoke(person, "World");
4. Use reflection to obtain and modify fields
Use reflection to dynamically obtain and modify the fields of a class. The following is a code example for obtaining and modifying fields:
Class clazz = Person.class; Field field = clazz.getField("name"); Person person = new Person(); String name = (String) field.get(person); field.set(person, "Tom");
5. Use reflection to call private methods and fields
Use reflection to access and call private methods and fields of a class. The following is a code example for obtaining and calling private methods and fields:
Class clazz = Person.class; Method method = clazz.getDeclaredMethod("privateMethod", int.class); method.setAccessible(true); Person person = new Person(); int result = (int) method.invoke(person, 10); Field field = clazz.getDeclaredField("privateField"); field.setAccessible(true); int value = (int) field.get(person);
6. Use reflection to dynamically create objects
Use reflection to create objects dynamically. The following is a code example that uses reflection to dynamically create objects:
Class clazz = Person.class; Person person = (Person) clazz.newInstance();
7. Use reflection to extend applications
Using reflection can achieve some advanced functions in some special scenarios, such as dynamic proxy, annotation processing, etc. The following is a code example of using reflection to implement a simple dynamic proxy:
class ProxyHandler implements InvocationHandler { private Object target; public ProxyHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Before invoke"); Object result = method.invoke(target, args); System.out.println("After invoke"); return result; } } Class clazz = Person.class; Person person = new Person(); InvocationHandler handler = new ProxyHandler(person); Person proxy = (Person) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), handler); proxy.sayHello("World");
Conclusion:
Through the explanation and code examples of this article, we have a deep understanding of how Java reflection is called. The reflection mechanism can help us dynamically obtain and operate class information and implement some advanced functions in some special scenarios. It also brings a certain degree of complexity and performance loss, so it needs to be carefully weighed when using it. I hope this article can be helpful to readers and deepen their understanding and application of Java reflection.
The above is the detailed content of Explore a deeper understanding of Java reflection calling methods. For more information, please follow other related articles on the PHP Chinese website!