0

I have a annotation @ToolExecution. At the moment I use annotation processing and throwing Error to ensure that only one method is annotated with this annotation.

Is there an native way to create a constraint and allow my custom annotation to be only applied to one annotation per class?

This should not be possible

@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");
  }

}
xetra11
  • 7,671
  • 14
  • 84
  • 159
  • 1
    You can't enforce a single method annotation per class at compile time, you can only do that type of validation at runtime. – lance-java Feb 21 '18 at 22:42
  • You should learn [Annotation Processor](https://medium.com/@iammert/annotation-processing-dont-repeat-yourself-generate-your-code-8425e60c6657) – Dean Xu Feb 22 '18 at 00:46
  • @lance-java pls define an answer to close this question – xetra11 Feb 22 '18 at 16:47

1 Answers1

0

You can't enforce a single method annotation per class at compile time, you can only do that type of validation at runtime.

lance-java
  • 25,497
  • 4
  • 59
  • 101