将方法名称作为字符串给出时,如何调用Java方法?

bra*_*zoo 655 java reflection invoke

如果我有两个变量:

Object obj;
String methodName = "getName";
Run Code Online (Sandbox Code Playgroud)

在不知道类的情况下obj,如何调用其上标识的方法methodName

被调用的方法没有参数和String返回值.它是Java bean的getter.

Hen*_*aul 940

从臀部编码,它将是这样的:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) { ... }
  catch (NoSuchMethodException e) { ... }
Run Code Online (Sandbox Code Playgroud)

参数标识了您需要的非常具体的方法(如果有多个重载可用,如果方法没有参数,则只给出methodName).

然后通过调用调用该方法

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { ... }
  catch (IllegalAccessException e) { ... }
  catch (InvocationTargetException e) { ... }
Run Code Online (Sandbox Code Playgroud)

再次,.invoke如果你没有参数,请省略参数.但是,是的.阅读Java Reflection

  • 不公平-1.亨里克可能并不主张压缩异常,也没有为他们写任何东西,因为他只是想证明反思. (118认同)
  • 另外一个用于显示一些潜在的例外.如果我写了这个,那将是...... catch(例外e){... (66认同)
  • 对Java使用类型擦除这一事实感到有点不安,但知道至少它有反射让我再次振作起来:D现在,在Java 8中使用lambdas语言实际上正在加速现代开发.现在唯一缺少的是对getter和setter的本机支持,或者在C#中已知的属性. (2认同)
  • @DeaMon1 Java方法不使用"退出代码",但如果方法返回任何内容,`invoke`将返回它返回的内容.如果运行该方法时发生异常,则异常将包含在`InvocationTargetException`中. (2认同)

Owe*_*wen 184

使用反射中的方法调用:

Class<?> c = Class.forName("class name");
Method method = c.getDeclaredMethod("method name", parameterTypes);
method.invoke(objectToInvokeOn, params);
Run Code Online (Sandbox Code Playgroud)

哪里:

  • "class name" 是类的名称
  • objectToInvokeOn 是Object类型,是要调用方法的对象
  • "method name" 是您要调用的方法的名称
  • parameterTypes是类型Class[]并声明方法采用的参数
  • params是类型Object[]并声明要传递给方法的参数

  • 错误.是的,getDeclaredMethod可以使用私有和受保护的方法.但是:它不适用于超类中定义的方法(继承方法).所以,这很大程度上取决于你想做什么.在许多情况下,无论定义方法的确切类如何,您都希望它能够工作. (21认同)

sil*_*ver 93

对于那些想要在Java 7中使用直接代码示例的人:

Dog 类:

package com.mypackage.bean;

public class Dog {
    private String name;
    private int age;

    public Dog() {
        // empty constructor
    }

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void printDog(String name, int age) {
        System.out.println(name + " is " + age + " year(s) old.");
    }
}
Run Code Online (Sandbox Code Playgroud)

ReflectionDemo 类:

package com.mypackage.demo;

import java.lang.reflect.*;

public class ReflectionDemo {

    public static void main(String[] args) throws Exception {
        String dogClassName = "com.mypackage.bean.Dog";
        Class<?> dogClass = Class.forName(dogClassName); // convert string classname to class
        Object dog = dogClass.newInstance(); // invoke empty constructor

        String methodName = "";

        // with single parameter, return void
        methodName = "setName";
        Method setNameMethod = dog.getClass().getMethod(methodName, String.class);
        setNameMethod.invoke(dog, "Mishka"); // pass arg

        // without parameters, return string
        methodName = "getName";
        Method getNameMethod = dog.getClass().getMethod(methodName);
        String name = (String) getNameMethod.invoke(dog); // explicit cast

        // with multiple parameters
        methodName = "printDog";
        Class<?>[] paramTypes = {String.class, int.class};
        Method printDogMethod = dog.getClass().getMethod(methodName, paramTypes);
        printDogMethod.invoke(dog, name, 3); // pass args
    }
}
Run Code Online (Sandbox Code Playgroud)

输出: Mishka is 3 year(s) old.


您可以通过以下方式调用带有参数的构造函数:

Constructor<?> dogConstructor = dogClass.getConstructor(String.class, int.class);
Object dog = dogConstructor.newInstance("Hachiko", 10);
Run Code Online (Sandbox Code Playgroud)

或者,您可以删除

String dogClassName = "com.mypackage.bean.Dog";
Class<?> dogClass = Class.forName(dogClassName);
Object dog = dogClass.newInstance();
Run Code Online (Sandbox Code Playgroud)

并做

Dog dog = new Dog();

Method method = Dog.class.getMethod(methodName, ...);
method.invoke(dog, ...);
Run Code Online (Sandbox Code Playgroud)

建议阅读: 创建新类实例

  • 最好的答案在这里。完整简洁 (2认同)

Pet*_*cek 55

可以像这样调用该方法.还有更多的可能性(检查反射api),但这是最简单的:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Assert;
import org.junit.Test;

public class ReflectionTest {

    private String methodName = "length";
    private String valueObject = "Some object";

    @Test
    public void testGetMethod() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        Method m = valueObject.getClass().getMethod(methodName, new Class[] {});
        Object ret = m.invoke(valueObject, new Object[] {});
        Assert.assertEquals(11, ret);
    }



}
Run Code Online (Sandbox Code Playgroud)

  • +1唯一的答案,认识到OP在他的问题中指定了"无参数"(因为它也是我所寻找的). (7认同)

Tom*_*ine 16

首先,不要.避免使用这种代码.它往往是非常糟糕的代码和不安全的(参见Java编程语言安全编码指南第2节,版本2.0).

如果你必须这样做,那就选择java.beans来反思.豆包裹反射允许相对安全和传统的访问.

  • 我不同意.编写这样的代码以保证安全是非常容易的,我已经用多种语言编写了这些代码.例如,可以创建一组允许的方法,并且只有在方法名称在集合中时才允许调用方法.更安全(但仍然简单的骨头)将每个允许的方法限制为特定状态,并且不允许调用该方法,除非线程/接口/用户/任何符合这样的标准. (9认同)

Von*_*onC 13

为了完成我同事的答案,您可能需要密切关注:

  • 静态或实例调用(在一种情况下,您不需要该类的实例,在另一种情况下,您可能需要依赖现有的默认构造函数,可能存在也可能不存在)
  • 公共或非公共方法调用(对于后者,您需要在doPrivileged块中的方法上调用setAccessible,其他findbugs将不满意)
  • 如果你想要抛弃大量的java系统异常(因此下面的代码中的CCException),则封装成一个更易于管理的应用程序异常

这是一个旧的java1.4代码,它考虑了这些点:

/**
 * Allow for instance call, avoiding certain class circular dependencies. <br />
 * Calls even private method if java Security allows it.
 * @param aninstance instance on which method is invoked (if null, static call)
 * @param classname name of the class containing the method 
 * (can be null - ignored, actually - if instance if provided, must be provided if static call)
 * @param amethodname name of the method to invoke
 * @param parameterTypes array of Classes
 * @param parameters array of Object
 * @return resulting Object
 * @throws CCException if any problem
 */
public static Object reflectionCall(final Object aninstance, final String classname, final String amethodname, final Class[] parameterTypes, final Object[] parameters) throws CCException
{
    Object res;// = null;
    try {
        Class aclass;// = null;
        if(aninstance == null)
        {
            aclass = Class.forName(classname);
        }
        else
        {
            aclass = aninstance.getClass();
        }
        //Class[] parameterTypes = new Class[]{String[].class};
    final Method amethod = aclass.getDeclaredMethod(amethodname, parameterTypes);
        AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
                amethod.setAccessible(true);
                return null; // nothing to return
            }
        });
        res = amethod.invoke(aninstance, parameters);
    } catch (final ClassNotFoundException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+CLASS, e);
    } catch (final SecurityException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_SECURITY_ISSUE, e);
    } catch (final NoSuchMethodException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_NOT_FOUND, e);
    } catch (final IllegalArgumentException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_ILLEGAL_ARGUMENTS+String.valueOf(parameters)+GenericConstants.CLOSING_ROUND_BRACKET, e);
    } catch (final IllegalAccessException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_ACCESS_RESTRICTION, e);
    } catch (final InvocationTargetException e) {
    throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_INVOCATION_ISSUE, e);
    } 
    return res;
}
Run Code Online (Sandbox Code Playgroud)


chi*_*uit 12

Object obj;

Method method = obj.getClass().getMethod("methodName", null);

method.invoke(obj, null);
Run Code Online (Sandbox Code Playgroud)

  • 有一个错误.obj必须初始化. (5认同)

anu*_*jin 12

//Step1 - Using string funClass to convert to class
String funClass = "package.myclass";
Class c = Class.forName(funClass);

//Step2 - instantiate an object of the class abov
Object o = c.newInstance();
//Prepare array of the arguments that your function accepts, lets say only one string here
Class[] paramTypes = new Class[1];
paramTypes[0]=String.class;
String methodName = "mymethod";
//Instantiate an object of type method that returns you method name
 Method m = c.getDeclaredMethod(methodName, paramTypes);
//invoke method with actual params
m.invoke(o, "testparam");
Run Code Online (Sandbox Code Playgroud)


Ami*_*ati 12

索引(更快)

您可以使用FunctionalInterface将方法保存在容器中以对它们进行索引。您可以使用数组容器通过数字调用它们或使用 hashmap 通过字符串调用它们。通过这个技巧,您可以索引您的方法以更快地动态调用它们。

@FunctionalInterface
public interface Method {
    double execute(int number);
}

public class ShapeArea {
    private final static double PI = 3.14;

    private Method[] methods = {
        this::square,
        this::circle
    };

    private double square(int number) {
        return number * number;
    }

    private double circle(int number) {
        return PI * number * number;
    }

    public double run(int methodIndex, int number) {
        return methods[methodIndex].execute(number);
    }
}
Run Code Online (Sandbox Code Playgroud)

Lambda 语法

您还可以使用 lambda 语法:

public class ShapeArea {
    private final static double PI = 3.14;

    private Method[] methods = {
        number -> {
            return number * number;
        },
        number -> {
            return PI * number * number;
        },
    };

    public double run(int methodIndex, int number) {
        return methods[methodIndex].execute(number);
    }
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*oom 8

如果多次执行调用,则可以使用Java 7中引入的新方法句柄.这里我们将返回一个返回String的方法:

Object obj = new Point( 100, 200 );
String methodName = "toString";  
Class<String> resultType = String.class;

MethodType mt = MethodType.methodType( resultType );
MethodHandle methodHandle = MethodHandles.lookup().findVirtual( obj.getClass(), methodName, mt );
String result = resultType.cast( methodHandle.invoke( obj ) );

System.out.println( result );  // java.awt.Point[x=100,y=200]
Run Code Online (Sandbox Code Playgroud)

  • 致未来的读者;如果您关心性能,您会尽可能使用“invokeExact”。为此,调用站点签名必须与方法句柄类型完全匹配。通常需要进行一些修改才能开始工作。在这种情况下,您需要使用以下方法强制转换第一个参数:`methodHandle = methodHandle.asType(methodHandle.type().changeParameterType(0, Object.class));`,然后像`String result = (String) methodHandle那样调用.invokeExact(obj);` (2认同)

zxc*_*xcv 7

这听起来像Java Reflection包可以使用的东西.

http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html

特别是在名称调用方法下:

import java.lang.reflect.*;

public class method2 {
  public int add(int a, int b)
  {
     return a + b;
  }

  public static void main(String args[])
  {
     try {
       Class cls = Class.forName("method2");
       Class partypes[] = new Class[2];
        partypes[0] = Integer.TYPE;
        partypes[1] = Integer.TYPE;
        Method meth = cls.getMethod(
          "add", partypes);
        method2 methobj = new method2();
        Object arglist[] = new Object[2];
        arglist[0] = new Integer(37);
        arglist[1] = new Integer(47);
        Object retobj 
          = meth.invoke(methobj, arglist);
        Integer retval = (Integer)retobj;
        System.out.println(retval.intValue());
     }
     catch (Throwable e) {
        System.err.println(e);
     }
  }
}
Run Code Online (Sandbox Code Playgroud)


San*_*lla 7

以下是准备使用的方法:

要调用一个方法,不带参数:

public static void callMethodByName(Object object, String methodName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    object.getClass().getDeclaredMethod(methodName).invoke(object);
}
Run Code Online (Sandbox Code Playgroud)

要使用参数调用方法:

    public static void callMethodByName(Object object, String methodName, int i, String s) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        object.getClass().getDeclaredMethod(methodName, int.class, String.class).invoke(object, i, s);
    }
Run Code Online (Sandbox Code Playgroud)

使用上述方法如下:

package practice;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class MethodInvoke {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
        String methodName1 = "methodA";
        String methodName2 = "methodB";
        MethodInvoke object = new MethodInvoke();
        callMethodByName(object, methodName1);
        callMethodByName(object, methodName2, 1, "Test");
    }

    public static void callMethodByName(Object object, String methodName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        object.getClass().getDeclaredMethod(methodName).invoke(object);
    }

    public static void callMethodByName(Object object, String methodName, int i, String s) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        object.getClass().getDeclaredMethod(methodName, int.class, String.class).invoke(object, i, s);
    }

    void methodA() {
        System.out.println("Method A");
    }

    void methodB(int i, String s) {
        System.out.println("Method B: "+"\n\tParam1 - "+i+"\n\tParam 2 - "+s);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

方法一  
方法B:  
	参数 1 - 1  
	参数 2 - 测试


Rah*_*kal 5

请参考以下代码可能对您有所帮助.

public static Method method[];
public static MethodClass obj;
public static String testMethod="A";

public static void main(String args[]) 
{
    obj=new MethodClass();
    method=obj.getClass().getMethods();
    try
    {
        for(int i=0;i<method.length;i++)
        {
            String name=method[i].getName();
            if(name==testMethod)
            {   
                method[i].invoke(name,"Test Parameters of A");
            }
        }
    }
    catch(Exception ex)
    {
        System.out.println(ex.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢....


Mar*_*cel 5

我这样做是这样的:

try {
    YourClass yourClass = new YourClass();
    Method method = YourClass.class.getMethod("yourMethodName", ParameterOfThisMethod.class);
    method.invoke(yourClass, parameter);
} catch (Exception e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)


Sub*_*sad 5

Method method = someVariable.class.getMethod(SomeClass);
String status = (String) method.invoke(method);
Run Code Online (Sandbox Code Playgroud)

SomeClass是类,someVariable是变量。