Home > Java > javaTutorial > body text

Explore a deeper understanding of Java reflection calling methods

WBOY
Release: 2023-12-23 12:45:55
Original
811 people have browsed it

Explore a deeper understanding of Java reflection calling methods

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:

  1. Use the static variable class of the class to obtain, for example: Class clazz = Person. class;
  2. Use the getClass() method of the object to obtain, for example: Class clazz = person.getClass();
  3. Use the Class.forName() method to obtain, for example: Class clazz = Class. forName("com.example.Person");

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);
Copy after login

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");
Copy after login

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");
Copy after login

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);
Copy after login

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();
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template