mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-03-28 04:07:01 -05:00
Make annotations data a bit nicer to use (#1391)
* Make annotations data a bit nicer to use * Add copy constructors
This commit is contained in:
@@ -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<String, ClassAnnotationData> 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<String, ClassAnnotationData> classes, String n
|
||||
return result;
|
||||
}
|
||||
|
||||
static <K, V> Map<K, V> copyMap(Map<K, V> map, UnaryOperator<V> valueCopier) {
|
||||
Map<K, V> result = LinkedHashMap.newLinkedHashMap(map.size());
|
||||
map.forEach((key, value) -> result.put(key, valueCopier.apply(value)));
|
||||
return result;
|
||||
}
|
||||
|
||||
static List<AnnotationNode> copyAnnotations(List<AnnotationNode> annotations) {
|
||||
List<AnnotationNode> 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<TypeAnnotationNode> copyTypeAnnotations(List<TypeAnnotationNode> annotations) {
|
||||
List<TypeAnnotationNode> 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);
|
||||
|
||||
@@ -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<String> annotationsToRemove();
|
||||
List<AnnotationNode> annotationsToAdd();
|
||||
Set<TypeAnnotationKey> typeAnnotationsToRemove();
|
||||
List<TypeAnnotationNode> typeAnnotationsToAdd();
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public record ClassAnnotationData(
|
||||
List<TypeAnnotationNode> typeAnnotationsToAdd,
|
||||
Map<String, GenericAnnotationData> fields,
|
||||
Map<String, MethodAnnotationData> 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<String> newAnnotationsToRemove = new LinkedHashSet<>(annotationsToRemove);
|
||||
newAnnotationsToRemove.addAll(other.annotationsToRemove);
|
||||
|
||||
@@ -46,7 +46,7 @@ public record GenericAnnotationData(
|
||||
Set<TypeAnnotationKey> typeAnnotationsToRemove,
|
||||
@SerializedName("type_add")
|
||||
List<TypeAnnotationNode> 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<String> newAnnotationToRemove = new LinkedHashSet<>(annotationsToRemove);
|
||||
newAnnotationToRemove.addAll(other.annotationsToRemove);
|
||||
|
||||
@@ -49,7 +49,7 @@ public record MethodAnnotationData(
|
||||
@SerializedName("type_add")
|
||||
List<TypeAnnotationNode> typeAnnotationsToAdd,
|
||||
Map<Integer, GenericAnnotationData> 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<String> newAnnotationsToRemove = new LinkedHashSet<>(annotationsToRemove);
|
||||
newAnnotationsToRemove.addAll(other.annotationsToRemove);
|
||||
|
||||
Reference in New Issue
Block a user