Skip to content

Commit

Permalink
impr: Optimize annotation printer creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Feb 24, 2024
1 parent 5c3985b commit f051fb6
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down Expand Up @@ -53,20 +55,23 @@ public PrintContext<?> printDeclaration(PrintContext<?> ctx) {
return ctx;
}

public AnnotationPrinter printAnnotation(int index) {
/**
* @param index Index into <i>all</i> 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<Annotation> 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<Annotation> invisibleAnnos = annotated.invisibleRuntimeAnnotations();
int targetInvisibleIndex = index - runtimeAnnotationCount;
if (targetInvisibleIndex < invisibleAnnos.size())
return new JvmAnnotationPrinter(invisibleAnnos.get(targetInvisibleIndex));
}
return null;
}
Expand Down

0 comments on commit f051fb6

Please sign in to comment.