From f051fb632877de6dc838767c60b80bc68ad9391d Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 24 Feb 2024 09:01:45 -0500 Subject: [PATCH] impr: Optimize annotation printer creation --- .../assembler/printer/MemberPrinter.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/jasm-composition-jvm/src/main/java/me/darknet/assembler/printer/MemberPrinter.java b/jasm-composition-jvm/src/main/java/me/darknet/assembler/printer/MemberPrinter.java index 1562511a..21b72a5c 100644 --- a/jasm-composition-jvm/src/main/java/me/darknet/assembler/printer/MemberPrinter.java +++ b/jasm-composition-jvm/src/main/java/me/darknet/assembler/printer/MemberPrinter.java @@ -9,6 +9,8 @@ import dev.xdark.blw.classfile.Signed; import org.jetbrains.annotations.Nullable; +import java.util.List; + public record MemberPrinter( @Nullable Annotated annotated, @Nullable Signed signed, @Nullable Accessible accessible, Type type ) { @@ -53,20 +55,23 @@ public PrintContext printDeclaration(PrintContext ctx) { return ctx; } - public AnnotationPrinter printAnnotation(int index) { + /** + * @param index Index into all annotations, where indices are based on the combined list of annotations. + * @return Printer for the annotation. {@code null} if the index does not point to a known annotation. + */ + public @Nullable AnnotationPrinter printAnnotation(int index) { if (annotated != null) { - // go through the annotations - for (int i = 0; i < annotated.visibleRuntimeAnnotations().size(); i++) { - if (i == index) { - return new JvmAnnotationPrinter(annotated.visibleRuntimeAnnotations().get(i)); - } - } - for (int i = annotated.visibleRuntimeAnnotations().size(); i < annotated.invisibleRuntimeAnnotations() - .size() + annotated.visibleRuntimeAnnotations().size(); i++) { - if (i == index) { - return new JvmAnnotationPrinter(annotated.invisibleRuntimeAnnotations().get(i)); - } - } + // First check visible annotations. + List visibleAnnos = annotated.visibleRuntimeAnnotations(); + int runtimeAnnotationCount = visibleAnnos.size(); + if (index < runtimeAnnotationCount) + return new JvmAnnotationPrinter(visibleAnnos.get(index)); + + // Next check invisible annotations, offsetting the index by the number of visible annotations. + List invisibleAnnos = annotated.invisibleRuntimeAnnotations(); + int targetInvisibleIndex = index - runtimeAnnotationCount; + if (targetInvisibleIndex < invisibleAnnos.size()) + return new JvmAnnotationPrinter(invisibleAnnos.get(targetInvisibleIndex)); } return null; }