【发布时间】:2019-06-07 00:42:51
【问题描述】:
我需要从 play.api.mvc.Call 实例中调用方法。 我已经注意到我的控制器的方法并使用反向路由我需要检查那些注释。
我正在使用 Play Framework 2.5.12
例子:
注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyAnnotation{
}
控制器:
public class MyController extends Controller {
@MyAnnotation
public Result home(int index){
return ok(index);
}
}
类:
public class MyClass{
private Call call;
public MyClass(Call call){
this.call = call;
}
public boolean hasAnnotation(){
//TODO
return call.getControllerMethod().isAnnotationPresent(MyAnnotation.class);
}
}
用途:
MyClass obj = new MyClass(routes.MyController.home(1));
if(obj.hasAnnotation()){
//do something
}
显然call.getControllerMethod() 不存在,但我需要一些解决方案来从 URL 或调用实例中获取控制器的方法。
感谢您的支持。
【问题讨论】:
-
我将您的解决方案移至社区 wiki 答案。
标签: java reflection playframework