diff --git a/src/main/java/net/neoforged/art/api/ClassProvider.java b/src/main/java/net/neoforged/art/api/ClassProvider.java index fbafa33..890da8b 100644 --- a/src/main/java/net/neoforged/art/api/ClassProvider.java +++ b/src/main/java/net/neoforged/art/api/ClassProvider.java @@ -166,8 +166,22 @@ 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 + */ + default Optional getField(String name, String desc) { + return this.getField(name); + } /** * 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..8a4334f 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(FieldInfo::getName, 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) { @@ -179,9 +190,15 @@ public Collection getFields() { return fieldsView; } + @Deprecated @Override public Optional getField(String name) { - return fields == null ? Optional.empty() : Optional.ofNullable(this.fields.get(name)); + 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)); } @Override