Besides adding operations that are exclusive to a particular config, you can also add operations at extension level, that is, operations that are global to the module.
Operations at extension level cannot not have a config unless the extension class is the only config. This means that they are not bound to a config. To define operations at extension level, you need to annotate the extension class with your @Operations while having configurations defined, rather than using the extension class as your only config.
Take into account that this only makes sense if you have more than just one config. That’s because if we only have the extension class (our only config),
then all the operations we define are bound to it.
The next example adds two configs and defines operations for each of them. Then it adds some operations that are global to the module.
@Extension(name = "Foo")
@Operations(GlobalOperations.class)
@Configurations({FooConfig.class, BarConfig.class})
public class FooModule {
}
public class GlobalOperations {
public String fooModuleOperation() {
return "this operation is global to the module!";
}
}
The example above defines a module with two configurations, FooConfig and BarConfig, and a set of operations defined in GlobalOperations.
Now, see the configs and their operations:
@Operations({FooConfigOperations.class})
public class FooConfig {
@Parameter
private String fooParameter;
public String getFooParameter() {
return fooParameter;
}
}
public class FooConfigOperations {
public String fooConfigOperation(@Config FooConfig config) {
return "this operation receives the FooConfig which has a fooParameter with value: " + config.getFooParameter();
}
}
The example above defines a new configuration named FooConfig with some exclusive operations defined in FooConfigOperations. Then it defines an operation named fooConfigOperation. Because it belongs to the configuration FooConfig, it can receive the configuration class as an argument annotated with the special annotation @Config.
@Operations({BarConfigOperations.class})
public class BarConfig {
@Parameter
private String barParameter;
public String getBarParameter() {
return barParameter;
}
}
public class BarConfigOperations {
public String barConfigOperation(@Config BarConfig config){
return "this operation receives the BarConfig which has a barParameter with value: " + config.getBarParameter();
}
}
Defining an operation at the extension level (as with the GlobalOperations methods above) represents that the defined operations do not receive (and therefore do not need) any configuration to operate.
For more information about configurations, see Configs.