Support Duration in generated meta-data
Update the configuration properties annotation processor to deal with `Duration` based default values. For example a field that defaults to `Duration.ofSeconds(10)` will have a meta-data default value of `10s`. See gh-11080
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.boot.configurationprocessor;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -424,8 +425,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
String.format("Enable the %s endpoint.", endpointId),
|
||||
(enabledByDefault == null ? true : enabledByDefault), null));
|
||||
this.metadataCollector.add(ItemMetadata.newProperty(endpointKey(endpointId),
|
||||
"cache.time-to-live", Long.class.getName(), type, null,
|
||||
"Maximum time in milliseconds that a response can be cached.", 0, null));
|
||||
"cache.time-to-live", Duration.class.getName(), type, null,
|
||||
"Maximum time that a response can be cached.", 0, null));
|
||||
}
|
||||
|
||||
private String endpointKey(String suffix) {
|
||||
|
||||
@@ -76,7 +76,7 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
WRAPPER_TYPES = Collections.unmodifiableMap(types);
|
||||
}
|
||||
|
||||
private static final Map<Class<?>, Object> defaultTypeValues;
|
||||
private static final Map<Class<?>, Object> DEFAULT_TYPE_VALUES;
|
||||
|
||||
static {
|
||||
Map<Class<?>, Object> values = new HashMap<>();
|
||||
@@ -85,10 +85,10 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
values.put(Short.class, (short) 0);
|
||||
values.put(Integer.class, 0);
|
||||
values.put(Long.class, (long) 0);
|
||||
defaultTypeValues = Collections.unmodifiableMap(values);
|
||||
DEFAULT_TYPE_VALUES = Collections.unmodifiableMap(values);
|
||||
}
|
||||
|
||||
private static final Map<String, Object> wellKnownStaticFinals;
|
||||
private static final Map<String, Object> WELL_KNOWN_STATIC_FINALS;
|
||||
|
||||
static {
|
||||
Map<String, Object> values = new HashMap<>();
|
||||
@@ -98,7 +98,22 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
values.put("StandardCharsets.UTF_8", "UTF-8");
|
||||
values.put("StandardCharsets.UTF_16", "UTF-16");
|
||||
values.put("StandardCharsets.US_ASCII", "US-ASCII");
|
||||
wellKnownStaticFinals = Collections.unmodifiableMap(values);
|
||||
WELL_KNOWN_STATIC_FINALS = Collections.unmodifiableMap(values);
|
||||
}
|
||||
|
||||
private static final String DURATION_OF = "Duration.of";
|
||||
|
||||
private static final Map<String, String> DURATION_SUFFIX;
|
||||
|
||||
static {
|
||||
Map<String, String> values = new HashMap<>();
|
||||
values.put("Nanos", "ns");
|
||||
values.put("Millis", "ms");
|
||||
values.put("Seconds", "s");
|
||||
values.put("Minutes", "m");
|
||||
values.put("Hours", "h");
|
||||
values.put("Days", "d");
|
||||
DURATION_SUFFIX = Collections.unmodifiableMap(values);
|
||||
}
|
||||
|
||||
private final Map<String, Object> fieldValues = new HashMap<>();
|
||||
@@ -119,7 +134,7 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
private Object getValue(VariableTree variable) throws Exception {
|
||||
ExpressionTree initializer = variable.getInitializer();
|
||||
Class<?> wrapperType = WRAPPER_TYPES.get(variable.getType());
|
||||
Object defaultValue = defaultTypeValues.get(wrapperType);
|
||||
Object defaultValue = DEFAULT_TYPE_VALUES.get(wrapperType);
|
||||
if (initializer != null) {
|
||||
return getValue(initializer, defaultValue);
|
||||
}
|
||||
@@ -134,7 +149,7 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
}
|
||||
Object factoryValue = expression.getFactoryValue();
|
||||
if (factoryValue != null) {
|
||||
return factoryValue;
|
||||
return getFactoryValue(expression, factoryValue);
|
||||
}
|
||||
List<? extends ExpressionTree> arrayValues = expression.getArrayExpression();
|
||||
if (arrayValues != null) {
|
||||
@@ -152,11 +167,22 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
|
||||
return this.staticFinals.get(expression.toString());
|
||||
}
|
||||
if (expression.getKind().equals("MEMBER_SELECT")) {
|
||||
return wellKnownStaticFinals.get(expression.toString());
|
||||
return WELL_KNOWN_STATIC_FINALS.get(expression.toString());
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
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 ? null : factoryValue + suffix);
|
||||
}
|
||||
return factoryValue;
|
||||
}
|
||||
|
||||
public Map<String, Object> getFieldValues() {
|
||||
return this.fieldValues;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user