in dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/meta/BeanMeta.java [136:224]
public static void resolvePropertyMap(
RestToolKit toolKit, String prefix, Class<?> type, boolean flatten, Map<String, PropertyMeta> propertyMap) {
if (type == null || type == Object.class || TypeUtils.isSystemType(type)) {
return;
}
Set<String> pbFields = null;
if (HAS_PB && Message.class.isAssignableFrom(type)) {
try {
Descriptor descriptor =
(Descriptor) type.getMethod("getDescriptor").invoke(null);
pbFields = descriptor.getFields().stream()
.map(FieldDescriptor::getName)
.collect(Collectors.toSet());
} catch (Exception ignored) {
}
}
Set<String> allNames = new LinkedHashSet<>();
Map<String, Field> fieldMap = new LinkedHashMap<>();
if (pbFields == null) {
for (Field field : type.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())
|| Modifier.isTransient(field.getModifiers())
|| field.isSynthetic()) {
continue;
}
if (!field.isAccessible()) {
field.setAccessible(true);
}
fieldMap.put(field.getName(), field);
allNames.add(field.getName());
}
}
Map<String, Method> getMethodMap = new LinkedHashMap<>();
Map<String, Method> setMethodMap = new LinkedHashMap<>();
for (Method method : type.getDeclaredMethods()) {
int modifiers = method.getModifiers();
if ((modifiers & (Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STATIC)) == Modifier.PUBLIC) {
String name = method.getName();
int count = method.getParameterCount();
if (count == 0) {
Class<?> returnType = method.getReturnType();
if (returnType == Void.TYPE) {
continue;
}
if (name.length() > 3 && name.startsWith("get")) {
name = toName(name, 3);
if (pbFields == null || pbFields.contains(name)) {
getMethodMap.put(name, method);
allNames.add(name);
}
} else if (name.length() > 2 && name.startsWith("is") && returnType == Boolean.TYPE) {
if (pbFields == null || pbFields.contains(name)) {
name = toName(name, 2);
getMethodMap.put(name, method);
allNames.add(name);
}
} else if (fieldMap.containsKey(name)) {
// For record class
getMethodMap.put(name, method);
allNames.add(name);
}
} else if (count == 1) {
if (name.length() > 3 && name.startsWith("set")) {
name = toName(name, 3);
setMethodMap.put(name, method);
allNames.add(name);
}
}
}
}
for (String name : allNames) {
Field field = fieldMap.get(name);
Method getMethod = getMethodMap.get(name);
Method setMethod = setMethodMap.get(name);
int visibility = pbFields == null
? (setMethod == null ? 0 : 1) << 2 | (getMethod == null ? 0 : 1) << 1 | (field == null ? 0 : 1)
: 0b011;
PropertyMeta meta = new PropertyMeta(toolKit, field, getMethod, setMethod, prefix, name, visibility);
propertyMap.put(meta.getName(), meta);
}
if (flatten) {
resolvePropertyMap(toolKit, prefix, type.getSuperclass(), true, propertyMap);
}
}