From 8de640625c934832958aba06abe68ee6a3bcdc87 Mon Sep 17 00:00:00 2001 From: Den Bekker Date: Fri, 10 May 2024 19:58:43 +0300 Subject: [PATCH 1/4] Add descriptor for field scheme inside ClassInfo Byte-code allow you to have same field names if descriptors are different. Remapper wes crashing if so happened --- .../net/neoforged/art/api/ClassProvider.java | 14 ++++++++++++- .../art/internal/ClassProviderImpl.java | 20 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/neoforged/art/api/ClassProvider.java b/src/main/java/net/neoforged/art/api/ClassProvider.java index fbafa33..ef4eb85 100644 --- a/src/main/java/net/neoforged/art/api/ClassProvider.java +++ b/src/main/java/net/neoforged/art/api/ClassProvider.java @@ -166,8 +166,20 @@ public interface IClassInfo { * * @param name the field name * @return the field, or an empty optional if it doesn't exist + * @deprecated Use with descriptor + * @see ClassProvider#getField(String, String) */ - Optional getField(String name); //TODO: Desc? + @Deprecated + Optional getField(String name); + + /** + * Queries a field based on its field name and descriptor. + * + * @param name the field name + * @param desc the field descriptor + * @return the field, or an empty optional if it doesn't exist + */ + Optional getField(String name, String desc); /** * Returns all the methods declared in this class. diff --git a/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java b/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java index 5ef06f8..e69c99a 100644 --- a/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java +++ b/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java @@ -119,7 +119,7 @@ static class ClassInfo implements IClassInfo { if (!node.fields.isEmpty()) this.fields = Collections.unmodifiableMap(node.fields.stream().map(FieldInfo::new) - .collect(Collectors.toMap(FieldInfo::getName, Function.identity()))); + .collect(Collectors.toMap(f -> f.getName() + f.getDescriptor(), Function.identity()))); else this.fields = null; } @@ -179,9 +179,25 @@ public Collection getFields() { return fieldsView; } + @Deprecated @Override public Optional getField(String name) { - return fields == null ? Optional.empty() : Optional.ofNullable(this.fields.get(name)); + if (this.fields != null) { + for (Map.Entry entry : this.fields.entrySet()) { + if (entry.getKey().split(" ")[0].equals(name)) { + // Backwards compatibility not possible when duplication + // on field names, constructor was crashing without descriptor logic + // Let it be any in this case + return Optional.of(entry.getValue()); + } + } + } + return Optional.empty(); + } + + @Override + public Optional getField(String name, String desc) { + return this.fields == null ? Optional.empty() : Optional.ofNullable(this.fields.get(name + " " + desc)); } @Override From 4a29bc63c0becb131ecccb566c797fe051551744 Mon Sep 17 00:00:00 2001 From: Den Bekker Date: Sat, 11 May 2024 23:34:23 +0300 Subject: [PATCH 2/4] Make new method defaults to old one due breaking change --- src/main/java/net/neoforged/art/api/ClassProvider.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/neoforged/art/api/ClassProvider.java b/src/main/java/net/neoforged/art/api/ClassProvider.java index ef4eb85..890da8b 100644 --- a/src/main/java/net/neoforged/art/api/ClassProvider.java +++ b/src/main/java/net/neoforged/art/api/ClassProvider.java @@ -179,7 +179,9 @@ public interface IClassInfo { * @param desc the field descriptor * @return the field, or an empty optional if it doesn't exist */ - Optional getField(String name, String desc); + default Optional getField(String name, String desc) { + return this.getField(name); + } /** * Returns all the methods declared in this class. From 7daf20d396716c19719d3dfd0e0557af250ee731 Mon Sep 17 00:00:00 2001 From: Den Bekker Date: Sat, 11 May 2024 23:39:34 +0300 Subject: [PATCH 3/4] Add raw storage for fields, changed storing separator Added raw storage "fieldName -> info" to keep performance Previous separator space changed to dot separator Changed runtime class fields retrieving also --- .../art/internal/ClassProviderImpl.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java b/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java index e69c99a..f8b51e8 100644 --- a/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java +++ b/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java @@ -95,7 +95,10 @@ static class ClassInfo implements IClassInfo { private final Access access; private final String superName; private final List interfaces; + // "fieldName descriptor" -> "info" private final Map fields; + // "fieldName" -> "info" + private final Map fieldsRaw; private Collection fieldsView; private final Map methods; private Collection methodsView; @@ -117,11 +120,15 @@ static class ClassInfo implements IClassInfo { this.methods = null; - if (!node.fields.isEmpty()) + if (!node.fields.isEmpty()) { this.fields = Collections.unmodifiableMap(node.fields.stream().map(FieldInfo::new) - .collect(Collectors.toMap(f -> f.getName() + f.getDescriptor(), Function.identity()))); - else + .collect(Collectors.toMap(f -> f.getName() + "." + f.getDescriptor(), Function.identity()))); + this.fieldsRaw = Collections.unmodifiableMap(node.fields.stream().map(FieldInfo::new) + .collect(Collectors.toMap(FieldInfo::getName, Function.identity(), (a, b) -> a))); + } else { this.fields = null; + this.fieldsRaw = null; + } } ClassInfo(Class node) { @@ -139,10 +146,14 @@ static class ClassInfo implements IClassInfo { Field[] flds = node.getDeclaredFields(); if (flds.length > 0) { - this.fields = Collections.unmodifiableMap(Arrays.asList(flds).stream().map(FieldInfo::new) - .collect(Collectors.toMap(FieldInfo::getName, Function.identity()))); - } else + this.fields = Collections.unmodifiableMap(Arrays.stream(flds).map(FieldInfo::new) + .collect(Collectors.toMap(f -> f.getName() + "." + f.getDescriptor(), Function.identity()))); + this.fieldsRaw = Collections.unmodifiableMap(Arrays.stream(flds).map(FieldInfo::new) + .collect(Collectors.toMap(FieldInfo::getName, Function.identity(), (a, b) -> a))); + } else { this.fields = null; + this.fieldsRaw = null; + } } private static String nameToBytecode(Class cls) { @@ -182,22 +193,12 @@ public Collection getFields() { @Deprecated @Override public Optional getField(String name) { - if (this.fields != null) { - for (Map.Entry entry : this.fields.entrySet()) { - if (entry.getKey().split(" ")[0].equals(name)) { - // Backwards compatibility not possible when duplication - // on field names, constructor was crashing without descriptor logic - // Let it be any in this case - return Optional.of(entry.getValue()); - } - } - } - return Optional.empty(); + return this.fieldsRaw == null ? Optional.empty() : Optional.ofNullable(this.fieldsRaw.get(name)); } @Override public Optional getField(String name, String desc) { - return this.fields == null ? Optional.empty() : Optional.ofNullable(this.fields.get(name + " " + desc)); + return this.fields == null ? Optional.empty() : Optional.ofNullable(this.fields.get(name + "." + desc)); } @Override From bfe6694873cf30a70e8bb63f3142cd3faff25bea Mon Sep 17 00:00:00 2001 From: Den Bekker Date: Sat, 11 May 2024 23:51:19 +0300 Subject: [PATCH 4/4] Fixed style to be same as previous indentations in this class --- .../java/net/neoforged/art/internal/ClassProviderImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java b/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java index f8b51e8..8a4334f 100644 --- a/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java +++ b/src/main/java/net/neoforged/art/internal/ClassProviderImpl.java @@ -122,9 +122,9 @@ static class ClassInfo implements IClassInfo { if (!node.fields.isEmpty()) { this.fields = Collections.unmodifiableMap(node.fields.stream().map(FieldInfo::new) - .collect(Collectors.toMap(f -> f.getName() + "." + f.getDescriptor(), Function.identity()))); + .collect(Collectors.toMap(f -> f.getName() + "." + f.getDescriptor(), Function.identity()))); this.fieldsRaw = Collections.unmodifiableMap(node.fields.stream().map(FieldInfo::new) - .collect(Collectors.toMap(FieldInfo::getName, Function.identity(), (a, b) -> a))); + .collect(Collectors.toMap(FieldInfo::getName, Function.identity(), (a, b) -> a))); } else { this.fields = null; this.fieldsRaw = null; @@ -149,7 +149,7 @@ static class ClassInfo implements IClassInfo { this.fields = Collections.unmodifiableMap(Arrays.stream(flds).map(FieldInfo::new) .collect(Collectors.toMap(f -> f.getName() + "." + f.getDescriptor(), Function.identity()))); this.fieldsRaw = Collections.unmodifiableMap(Arrays.stream(flds).map(FieldInfo::new) - .collect(Collectors.toMap(FieldInfo::getName, Function.identity(), (a, b) -> a))); + .collect(Collectors.toMap(FieldInfo::getName, Function.identity(), (a, b) -> a))); } else { this.fields = null; this.fieldsRaw = null;