【问题标题】:Allow custom annotation only one one method per class每个类只允许自定义注解一种方法
【发布时间】:2018-08-01 14:43:05
【问题描述】:

我有一个注释@ToolExecution。目前我使用注解处理并抛出Error 来确保只有一个方法被这个注解注解。

是否有一种本地方式来创建约束并允许我的自定义注释仅应用于每个类的一个注释?

这应该是不可能的

@Tool(id = "scheduledtool")
public class ScheduledTool extends SimpleTool {
  private String parameter;

  @ToolExecution
  public void configuration(@ToolParameters(fields = {"config"}, credentials = true) ToolParameter parameters) {
    String parameter = parameters.getParameter("config");
    this.parameter = parameter;
  }

  @ToolExecution
  public void execute() {
    toolLog(Level.INFO, "Configured Param: " + parameter);
    toolLog(Level.INFO, "Finished scheduled tool");
  }

}

【问题讨论】:

  • 您不能在编译时对每个类强制使用单个方法注释,您只能在运行时进行该类型的验证。
  • 你应该学习Annotation Processor
  • @lance-java 请定义一个答案来关闭这个问题

标签: java annotations java-annotations


【解决方案1】:

您不能在编译时对每个类强制执行单个方法注释,您只能在运行时执行该类型的验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2017-12-04
    • 2022-10-14
    相关资源
    最近更新 更多