Add support for DataSize
This commit adds support for Spring Framework's `DataSize` allowing to express a size in bytes and other convenient units. Similar to the `Duration` support introduced previously, this commit adds transparent binding support as well as detection of default values in `@ConfigurationProperties`-annotated object. Closes gh-13974
This commit is contained in:
@@ -116,6 +116,20 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
DURATION_SUFFIX = Collections.unmodifiableMap(values);
|
||||
}
|
||||
|
||||
private static final String DATA_SIZE_OF = "DataSize.of";
|
||||
|
||||
private static final Map<String, String> DATA_SIZE_SUFFIX;
|
||||
|
||||
static {
|
||||
Map<String, String> values = new HashMap<>();
|
||||
values.put("Bytes", "B");
|
||||
values.put("KiloBytes", "KB");
|
||||
values.put("MegaBytes", "MB");
|
||||
values.put("GigaBytes", "GB");
|
||||
values.put("TeraBytes", "TB");
|
||||
DATA_SIZE_SUFFIX = Collections.unmodifiableMap(values);
|
||||
}
|
||||
|
||||
private final Map<String, Object> fieldValues = new HashMap<>();
|
||||
|
||||
private final Map<String, Object> staticFinals = new HashMap<>();
|
||||
@@ -173,16 +187,31 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
}
|
||||
|
||||
private Object getFactoryValue(ExpressionTree expression, Object factoryValue) {
|
||||
Object instance = expression.getInstance();
|
||||
if (instance != null && instance.toString().startsWith(DURATION_OF)) {
|
||||
String type = instance.toString();
|
||||
type = type.substring(DURATION_OF.length(), type.indexOf('('));
|
||||
String suffix = DURATION_SUFFIX.get(type);
|
||||
return (suffix != null) ? factoryValue + suffix : null;
|
||||
Object durationValue = getFactoryValue(expression, factoryValue, DURATION_OF,
|
||||
DURATION_SUFFIX);
|
||||
if (durationValue != null) {
|
||||
return durationValue;
|
||||
}
|
||||
Object dataSizeValue = getFactoryValue(expression, factoryValue, DATA_SIZE_OF,
|
||||
DATA_SIZE_SUFFIX);
|
||||
if (dataSizeValue != null) {
|
||||
return dataSizeValue;
|
||||
}
|
||||
return factoryValue;
|
||||
}
|
||||
|
||||
private Object getFactoryValue(ExpressionTree expression, Object factoryValue,
|
||||
String prefix, Map<String, String> suffixMapping) {
|
||||
Object instance = expression.getInstance();
|
||||
if (instance != null && instance.toString().startsWith(prefix)) {
|
||||
String type = instance.toString();
|
||||
type = type.substring(prefix.length(), type.indexOf('('));
|
||||
String suffix = suffixMapping.get(type);
|
||||
return (suffix != null) ? factoryValue + suffix : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> getFieldValues() {
|
||||
return this.fieldValues;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user