Use proper format for TypeReference
This commit updates BasicJsonWriter to handle TypeReferences and generate an appropriate format for a class name. Specifically, an inner class should be separated by a dollar sign, not a dot. Closes gh-28312
This commit is contained in:
@@ -23,6 +23,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Very basic json writer for the purposes of translating runtime hints to native
|
||||
* configuration.
|
||||
@@ -129,6 +132,9 @@ class BasicJsonWriter {
|
||||
else if (value instanceof List<?> list) {
|
||||
writeArray(list, false);
|
||||
}
|
||||
else if (value instanceof TypeReference typeReference) {
|
||||
this.writer.print(quote(toName(typeReference)));
|
||||
}
|
||||
else if (value instanceof CharSequence string) {
|
||||
this.writer.print(quote(escape(string)));
|
||||
}
|
||||
@@ -144,6 +150,22 @@ class BasicJsonWriter {
|
||||
return "\"" + name + "\"";
|
||||
}
|
||||
|
||||
private String toName(TypeReference typeReference) {
|
||||
StringBuilder names = new StringBuilder();
|
||||
buildName(typeReference, names);
|
||||
return typeReference.getPackageName() + "." + names;
|
||||
}
|
||||
|
||||
private void buildName(@Nullable TypeReference type, StringBuilder sb) {
|
||||
if (type == null) {
|
||||
return;
|
||||
}
|
||||
String typeName = (type.getEnclosingType() != null) ? "$" + type.getSimpleName() : type.getSimpleName();
|
||||
sb.insert(0, typeName);
|
||||
buildName(type.getEnclosingType(), sb);
|
||||
}
|
||||
|
||||
|
||||
private static String escape(CharSequence input) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
input.chars().forEach(c -> {
|
||||
|
||||
@@ -42,7 +42,7 @@ class JavaSerializationHintsWriter {
|
||||
|
||||
private Map<String, Object> toAttributes(TypeReference typeReference) {
|
||||
LinkedHashMap<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("name", typeReference.getCanonicalName());
|
||||
attributes.put("name", typeReference);
|
||||
return attributes;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ class ReflectionHintsWriter {
|
||||
|
||||
private Map<String, Object> toAttributes(TypeHint hint) {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("name", hint.getType().getCanonicalName());
|
||||
attributes.put("name", hint.getType());
|
||||
handleCondition(attributes, hint);
|
||||
handleCategories(attributes, hint.getMemberCategories());
|
||||
handleFields(attributes, hint.fields());
|
||||
@@ -63,7 +63,7 @@ class ReflectionHintsWriter {
|
||||
private void handleCondition(Map<String, Object> attributes, TypeHint hint) {
|
||||
if (hint.getReachableType() != null) {
|
||||
Map<String, Object> conditionAttributes = new LinkedHashMap<>();
|
||||
conditionAttributes.put("typeReachable", hint.getReachableType().getCanonicalName());
|
||||
conditionAttributes.put("typeReachable", hint.getReachableType());
|
||||
attributes.put("condition", conditionAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user