spring-shell

Spring Shell 2: Refactoring-safe Dynamic Command Availability


I'm just trying out Spring Shell 2. The section Dynamic Command Availability of the reference documentation shows three ways to indicate availability. However, all of them rely on a naming scheme or string parameter in an annotation. This will break (at runtime) if one uses the refactor functionality of an IDE. So, is there a possibility to use the Dynamic Command Availability feature in a refactoring-safe way?

Update 1:

Considering the answer below, I think this snippet demonstrates the solution:

@ShellComponent
public class MyCommands {
    private final static String ADD_NAME = "add";

    @ShellMethod(key=ADD_NAME, value = "Add two integers together.")
    public int addTwoInts(int a, int b) {
        return a+b;
    }

    @ShellMethodAvailability(ADD_NAME)
    public Availability checkAddAvailability() {
        return Availability.available();
    }
}

Solution

  • Note that the string parameter in the annotation is the command name, so if you specify it both on the availability method and on the command method, this will survive refactoring. Bonus points if you extract the command name in a constant.