From 501070a47a75ba0dbb28fff9cd6bb5c19a2b6140 Mon Sep 17 00:00:00 2001 From: Joseph Burton Date: Wed, 8 Oct 2025 12:01:23 +0100 Subject: [PATCH] Make annotations data a bit nicer to use (#1391) * Make annotations data a bit nicer to use * Add copy constructors --- .../extras/annotations/AnnotationsData.java | 40 +++++++++++++++++++ .../annotations/BaseAnnotationData.java | 38 ++++++++++++++++++ .../annotations/ClassAnnotationData.java | 17 +++++++- .../annotations/GenericAnnotationData.java | 15 ++++++- .../annotations/MethodAnnotationData.java | 16 +++++++- 5 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/BaseAnnotationData.java diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/AnnotationsData.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/AnnotationsData.java index b11cba57..51c79896 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/AnnotationsData.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/AnnotationsData.java @@ -27,11 +27,13 @@ package net.fabricmc.loom.configuration.providers.mappings.extras.annotations; import java.io.IOException; import java.io.Reader; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.function.UnaryOperator; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; @@ -69,6 +71,14 @@ public record AnnotationsData(Map classes, String n } } + public AnnotationsData(String namespace) { + this(new LinkedHashMap<>(), namespace); + } + + public AnnotationsData(AnnotationsData other) { + this(copyMap(other.classes, ClassAnnotationData::new), other.namespace); + } + public static AnnotationsData read(Reader reader) { JsonObject json = GSON.fromJson(reader, JsonObject.class); checkVersion(json); @@ -118,6 +128,36 @@ public record AnnotationsData(Map classes, String n return result; } + static Map copyMap(Map map, UnaryOperator valueCopier) { + Map result = LinkedHashMap.newLinkedHashMap(map.size()); + map.forEach((key, value) -> result.put(key, valueCopier.apply(value))); + return result; + } + + static List copyAnnotations(List annotations) { + List result = new ArrayList<>(annotations.size()); + + for (AnnotationNode annotation : annotations) { + AnnotationNode newAnnotation = new AnnotationNode(annotation.desc); + annotation.accept(newAnnotation); + result.add(newAnnotation); + } + + return result; + } + + static List copyTypeAnnotations(List annotations) { + List result = new ArrayList<>(annotations.size()); + + for (TypeAnnotationNode annotation : annotations) { + TypeAnnotationNode newAnnotation = new TypeAnnotationNode(annotation.typeRef, annotation.typePath, annotation.desc); + annotation.accept(newAnnotation); + result.add(newAnnotation); + } + + return result; + } + public AnnotationsData merge(AnnotationsData other) { if (!namespace.equals(other.namespace)) { throw new IllegalArgumentException("Cannot merge annotations from namespace " + other.namespace + " into annotations from namespace " + this.namespace); diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/BaseAnnotationData.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/BaseAnnotationData.java new file mode 100644 index 00000000..ef000b14 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/BaseAnnotationData.java @@ -0,0 +1,38 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2025 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.configuration.providers.mappings.extras.annotations; + +import java.util.List; +import java.util.Set; + +import org.objectweb.asm.tree.AnnotationNode; +import org.objectweb.asm.tree.TypeAnnotationNode; + +public interface BaseAnnotationData { + Set annotationsToRemove(); + List annotationsToAdd(); + Set typeAnnotationsToRemove(); + List typeAnnotationsToAdd(); +} diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/ClassAnnotationData.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/ClassAnnotationData.java index 14ed43c5..b5ba93e1 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/ClassAnnotationData.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/ClassAnnotationData.java @@ -52,7 +52,7 @@ public record ClassAnnotationData( List typeAnnotationsToAdd, Map fields, Map methods -) { +) implements BaseAnnotationData { public ClassAnnotationData { if (annotationsToRemove == null) { annotationsToRemove = new LinkedHashSet<>(); @@ -79,6 +79,21 @@ public record ClassAnnotationData( } } + public ClassAnnotationData() { + this(new LinkedHashSet<>(), new ArrayList<>(), new LinkedHashSet<>(), new ArrayList<>(), new LinkedHashMap<>(), new LinkedHashMap<>()); + } + + public ClassAnnotationData(ClassAnnotationData other) { + this( + new LinkedHashSet<>(other.annotationsToRemove), + AnnotationsData.copyAnnotations(other.annotationsToAdd), + new LinkedHashSet<>(other.typeAnnotationsToRemove), + AnnotationsData.copyTypeAnnotations(other.typeAnnotationsToAdd), + AnnotationsData.copyMap(other.fields, GenericAnnotationData::new), + AnnotationsData.copyMap(other.methods, MethodAnnotationData::new) + ); + } + ClassAnnotationData merge(ClassAnnotationData other) { Set newAnnotationsToRemove = new LinkedHashSet<>(annotationsToRemove); newAnnotationsToRemove.addAll(other.annotationsToRemove); diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/GenericAnnotationData.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/GenericAnnotationData.java index 94a2e96e..a0e96d2a 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/GenericAnnotationData.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/GenericAnnotationData.java @@ -46,7 +46,7 @@ public record GenericAnnotationData( Set typeAnnotationsToRemove, @SerializedName("type_add") List typeAnnotationsToAdd -) { +) implements BaseAnnotationData { public GenericAnnotationData { if (annotationsToRemove == null) { annotationsToRemove = new LinkedHashSet<>(); @@ -65,6 +65,19 @@ public record GenericAnnotationData( } } + public GenericAnnotationData() { + this(new LinkedHashSet<>(), new ArrayList<>(), new LinkedHashSet<>(), new ArrayList<>()); + } + + public GenericAnnotationData(GenericAnnotationData other) { + this( + new LinkedHashSet<>(other.annotationsToRemove), + AnnotationsData.copyAnnotations(other.annotationsToAdd), + new LinkedHashSet<>(other.typeAnnotationsToRemove), + AnnotationsData.copyTypeAnnotations(other.typeAnnotationsToAdd) + ); + } + GenericAnnotationData merge(GenericAnnotationData other) { Set newAnnotationToRemove = new LinkedHashSet<>(annotationsToRemove); newAnnotationToRemove.addAll(other.annotationsToRemove); diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/MethodAnnotationData.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/MethodAnnotationData.java index fde79765..deef4b45 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/MethodAnnotationData.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/annotations/MethodAnnotationData.java @@ -49,7 +49,7 @@ public record MethodAnnotationData( @SerializedName("type_add") List typeAnnotationsToAdd, Map parameters -) { +) implements BaseAnnotationData { public MethodAnnotationData { if (annotationsToRemove == null) { annotationsToRemove = new LinkedHashSet<>(); @@ -72,6 +72,20 @@ public record MethodAnnotationData( } } + public MethodAnnotationData() { + this(new LinkedHashSet<>(), new ArrayList<>(), new LinkedHashSet<>(), new ArrayList<>(), new LinkedHashMap<>()); + } + + public MethodAnnotationData(MethodAnnotationData other) { + this( + new LinkedHashSet<>(other.annotationsToRemove), + AnnotationsData.copyAnnotations(other.annotationsToAdd), + new LinkedHashSet<>(other.typeAnnotationsToRemove), + AnnotationsData.copyTypeAnnotations(other.typeAnnotationsToAdd), + AnnotationsData.copyMap(other.parameters, GenericAnnotationData::new) + ); + } + MethodAnnotationData merge(MethodAnnotationData other) { Set newAnnotationsToRemove = new LinkedHashSet<>(annotationsToRemove); newAnnotationsToRemove.addAll(other.annotationsToRemove);