Skip to content

Commit

Permalink
Provides a way to provide custom handling for unknown parameters ni R…
Browse files Browse the repository at this point in the history
…eflectiveConfigGroup

Fixes #2960
  • Loading branch information
mrieser committed Dec 30, 2023
1 parent b3af497 commit 719d2a2
Showing 1 changed file with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,29 @@ public final void addParam(final String param_name, final String value) {
return;
}

Preconditions.checkArgument(storeUnknownParameters, "Module %s of type %s doesn't accept unknown parameters."
+ " Parameter %s is not part of the valid parameters: %s", getName(), getClass().getName(), param_name,
setters.keySet());
this.handleAddUnknownParam(param_name, value);
}

/**
* This method is designed to be overwritten if a config group wants to provide
* custom handling for unknown parameters, e.g. for improved backwards compatibility.
* For example: It allows to convert (old-named) parameter values to different units and
* store them with the new name (old parameter could be km/h, new parameter could be m/s).
*
* The default implementation in {@link ReflectiveConfigGroup} will either store the
* unknown parameter or throw an exception, depending on the value of {@link #storeUnknownParameters}.
*
* If the method is overwritten, it might make sense to also overwrite {@link #handleGetUnknownValue(String)}.
*/
public void handleAddUnknownParam(final String paramName, final String value) {
Preconditions.checkArgument(this.storeUnknownParameters, "Module %s of type %s doesn't accept unknown parameters."
+ " Parameter %s is not part of the valid parameters: %s", getName(), getClass().getName(), paramName,
this.setters.keySet());

log.warn(
"Unknown parameter {} for group {}. Here are the valid parameter names: {}. Only the string value will be remembered.",
param_name, getName(), registeredParams);
super.addParam(param_name, value);
"Unknown parameter {} for group {}. Here are the valid parameter names: {}. Only the string value will be remembered.",
paramName, getName(), this.registeredParams);
super.addParam(paramName, value);
}

private void invokeSetter(final Method setter, final String value) {
Expand Down Expand Up @@ -438,13 +453,22 @@ public final String getValue(final String param_name) {
if (field != null) {
return getParamField(field);
}
return this.handleGetUnknownValue(param_name);
}

Preconditions.checkArgument(storeUnknownParameters, "Module %s of type %s doesn't store unknown parameters."
+ " Parameter %s is not part of the valid parameters: %s", getName(), getClass().getName(), param_name,
registeredParams);
/**
* This method is designed to be overwritten if a config group wants to provide
* custom handling for unknown parameters, e.g. for improved backwards compatibility.
*
* Also see {@link #handleAddUnknownParam(String, String)}
*/
public String handleGetUnknownValue(final String paramName) {
Preconditions.checkArgument(this.storeUnknownParameters, "Module %s of type %s doesn't store unknown parameters."
+ " Parameter %s is not part of the valid parameters: %s", getName(), getClass().getName(), paramName,
this.registeredParams);

log.warn("no getter found for param {}: trying parent method", param_name);
return super.getValue(param_name);
log.warn("no getter found for param {}: trying parent method", paramName);
return super.getValue(paramName);
}

private String invokeGetter(Method getter) {
Expand Down

0 comments on commit 719d2a2

Please sign in to comment.