chaokunyang commented on code in PR #3021:
URL: https://github.com/apache/fory/pull/3021#discussion_r2625946285
##########
java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java:
##########
@@ -247,17 +248,46 @@ public List<Descriptor> getDescriptors(TypeResolver
resolver, Class<?> cls) {
SortedMap<Member, Descriptor> allDescriptorsMap =
resolver.getFory().getClassResolver().getAllDescriptorsMap(cls,
true);
Map<String, Descriptor> descriptorsMap = new HashMap<>();
+ Map<Short, Descriptor> tagToDescriptorMap = new HashMap<>();
+
+ // Build maps for both name-based and tag-based lookups
for (Map.Entry<Member, Descriptor> e : allDescriptorsMap.entrySet()) {
- if (descriptorsMap.put(
- e.getKey().getDeclaringClass().getName() + "." +
e.getKey().getName(), e.getValue())
- != null) {
+ String fullName = e.getKey().getDeclaringClass().getName() + "." +
e.getKey().getName();
+ Descriptor desc = e.getValue();
+ if (descriptorsMap.put(fullName, desc) != null) {
throw new IllegalStateException("Duplicate key");
}
+
+ // If the field has @ForyField annotation with tag ID, index by tag ID
+ if (desc.getForyField() != null) {
+ int tagId = desc.getForyField().id();
+ if (tagId >= 0) {
+ if (tagToDescriptorMap.containsKey((short) tagId)) {
+ throw new IllegalArgumentException(
+ "Duplicate tag id "
+ + tagId
+ + " for field "
+ + desc.getName()
+ + " in class "
+ + cls.getName());
+ }
+ tagToDescriptorMap.put((short) tagId, desc);
+ }
+ }
}
+
descriptors = new ArrayList<>(fieldsInfo.size());
for (FieldInfo fieldInfo : fieldsInfo) {
- Descriptor descriptor =
- descriptorsMap.get(fieldInfo.getDefinedClass() + "." +
fieldInfo.getFieldName());
+ Descriptor descriptor;
+
+ // Try to match by tag ID first if the FieldInfo has a tag
+ if (fieldInfo.hasTag()) {
Review Comment:
How about naming tag to `fieldId`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]