简体   繁体   English

注释和方法

[英]Annotation and methods

Is there a way to call a method method in annotation有没有办法在注解中调用方法方法

For example例如

@Annotation(param = Class.method();)

or is there anoter way?还是有其他方法?

It is possible by providing the name of the function into the annotation可以通过在注释中提供 function 的名称

Heres a class I wrote which contains the generic function 'callMethod()' which takes in a String and instance of a class and returns the result of calling that method.这是我写的一个 class,它包含通用的 function 'callMethod()',它接受一个字符串和一个 class 的实例并返回调用该方法的结果。

public static <T> Object callMethod(String methodName, T classInstance) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Method method = classInstance.getClass().getDeclaredMethod(methodName);
    return method.invoke(classInstance);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM