Home > Java > javaTutorial > body text

How to use the invoke method in Java?

WBOY
Release: 2023-04-25 14:55:07
forward
1580 people have browsed it

First of all, you need to understand what the invoke method does and its specific use. In fact, you need to find out which class file it is in the source code and which package it is in, and trace the source.

The invoke method comes from the Method class and may not be used as much as the basic type wrapper classes we often use, as well as collection classes and their extensions and tool classes.

But the package where the Method class is located is the famous reflection Reflex. If there is no saying that Java does not have reflection, then many frameworks will not exist.

We often create new objects, but the premise of new is that you know what object you need before you can create new. However, there are unknowns in both code and real life, that is, until the program is running, it is subject to conditions. Only then do you know what class and what method.

Reflection solves this problem by obtaining the object structure and calling methods at runtime.
Method class is about reflective calling methods
The following figure is the official annotation of the Method class

How to use the invoke method in Java?

It probably means to provide method information of a class or interface. You can access and call the corresponding method.

Invoke means calling, which means we can call the invoke method through the Method class under the reflection package, call the method we provide and the parameters of the calling method to complete the dynamic call.

That is, it is called based on the object/instance, method name, and parameters you give. Find a "substitute" to help you call the method.

2 The use of invoke method
In fact, the use of invoke method is different from what we commonly see.

We often create an object A, the method getA() in the A object, and then A.getA()

We use a new way to call
(1) Get a method "Stand-in" (actually, it is to build a Method object and let this Method object replace the method you want to use now)
(2) Then give the stand-in the objects and parameters it needs, and let the stand-in call it for you (like JOJO's A substitute will fight for you)

The specific code demonstration is as follows:

public class InvokeTest {
    public void test(String[] arg){
        for (String string : arg) {
            System.out.println("zp is " + string);
        }
    }
    @Test
    public void invokeDemo() throws Exception {
        //获取字节码对象,这里要填好你对应对象的包的路径
        Class<InvokeTest> clazz = (Class<InvokeTest>) Class.forName("com.example.zp.demo.testDemo.InvokeTest");
        //形式一:获取一个对象
//        Constructor con =  clazz.getConstructor();
//        InvokeTest m = (InvokeTest) con.newInstance();
        //形式二:直接new对象,实际上不是框架的话,自己写代码直接指定某个对象创建并调用也可以
        InvokeTest m = new InvokeTest();
        String[] s = new String[]{"handsome","smart"};
        //获取Method对象
        Method method = clazz.getMethod("test", String[].class);
        //调用invoke方法来调用
        method.invoke(m, (Object) s);
    }
Copy after login

So using the invoke method requires one more step than other methods, which is to build a Method object. This object replaces the current program. A replacement for the method to be called.

In addition to parameters, invoke will also require one more object, because method calls require objects, so if the target method that invoke wants to call, it needs the required objects of the target method.

It seems that the invoke method is not only more troublesome than calling the ordinary method directly, but have you ever thought about it, I only need to enter parameters, I can call various alternative methods, and in unknown situations, it is determined based on conditions Which object and method to call suddenly makes the code flexible. This is not only the beauty of invoke, but also the beauty of reflection as a whole. It can be used flexibly according to conditions when the program is running.

The above is the detailed content of How to use the invoke method in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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