Skip to content

Commit

Permalink
Add method that allows the resolution of type names of annotations ev…
Browse files Browse the repository at this point in the history
…en if the annotations themselves are not resolvable.
  • Loading branch information
raphw committed Aug 17, 2023
1 parent 1d835b9 commit 76d90b1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -92,6 +93,14 @@ public interface AnnotationList extends FilterableList<AnnotationDescription, An
*/
TypeList asTypeList();

/**
* Returns a list of the names of the annotation types. This list might contain the names of
* annotations that are not otherwise resolvable.
*
* @return A list of binary names of the represented annotations.
*/
List<String> asTypeNames();

/**
* An abstract base implementation of an annotation list.
*/
Expand Down Expand Up @@ -185,6 +194,17 @@ public TypeList asTypeList() {
return new TypeList.Explicit(annotationTypes);
}

/**
* {@inheritDoc}
*/
public List<String> asTypeNames() {
List<String> typeNames = new ArrayList<String>(size());
for (AnnotationDescription annotation : this) {
typeNames.add(annotation.getAnnotationType().getName());
}
return typeNames;
}

@Override
protected AnnotationList wrap(List<AnnotationDescription> values) {
return new Explicit(values);
Expand Down Expand Up @@ -375,5 +395,12 @@ public AnnotationList visibility(ElementMatcher<? super RetentionPolicy> matcher
public TypeList asTypeList() {
return new TypeList.Empty();
}

/**
* {@inheritDoc}
*/
public List<String> asTypeNames() {
return Collections.emptyList();
}
}
}
35 changes: 33 additions & 2 deletions byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6189,7 +6189,7 @@ protected static AnnotationList asListOfNullable(TypePool typePool, @MaybeNull L
*
* @param typePool The type pool to be used for looking up linked types.
* @param tokens The tokens to represent in the list.
* @return A list of the loadable annotations.
* @return A list of the represented annotations.
*/
protected static AnnotationList asList(TypePool typePool, List<? extends AnnotationToken> tokens) {
List<AnnotationDescription> annotationDescriptions = new ArrayList<AnnotationDescription>(tokens.size());
Expand All @@ -6199,7 +6199,7 @@ protected static AnnotationList asList(TypePool typePool, List<? extends Annotat
annotationDescriptions.add(resolution.resolve());
}
}
return new AnnotationList.Explicit(annotationDescriptions);
return new UnresolvedAnnotationList(annotationDescriptions, tokens);
}

/**
Expand Down Expand Up @@ -6268,6 +6268,37 @@ public S load() {
return AnnotationInvocationHandler.of(annotationType.getClassLoader(), annotationType, values);
}
}

/**
* A list of annotations which allows for resolving the names of the annotations even if the annotations cannot be resolved.
*/
private static class UnresolvedAnnotationList extends AnnotationList.Explicit {

/**
* The list of represented annotation tokens.
*/
private final List<? extends AnnotationToken> tokens;

/**
* Creates a list of unresolved annotations.
*
* @param annotationDescriptions The list of represented annotation descriptions.
* @param tokens The list of represented annotation tokens.
*/
private UnresolvedAnnotationList(List<? extends AnnotationDescription> annotationDescriptions, List<? extends AnnotationToken> tokens) {
super(annotationDescriptions);
this.tokens = tokens;
}

@Override
public List<String> asTypeNames() {
List<String> typeNames = new ArrayList<String>(tokens.size());
for (AnnotationToken token : tokens) {
typeNames.add(token.getBinaryName());
}
return typeNames;
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public void testAsTypeList() throws Exception {
assertThat(asList(getFirst()).asTypeList(), is(Collections.singletonList(asElement(getFirst()).getAnnotationType())));
}

@Test
public void testAsTypeNames() throws Exception {
assertThat(asList(getFirst()).asTypeNames(), is(Collections.singletonList(asElement(getFirst()).getAnnotationType().getName())));
}

@Retention(RetentionPolicy.RUNTIME)
@Inherited
protected @interface Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public void testAnnotationVisibility() throws Exception {
public void testAsTypeList() throws Exception {
assertThat(new AnnotationList.Empty().asTypeList().size(), is(0));
}

@Test
public void testAsTypeNames() throws Exception {
assertThat(new AnnotationList.Empty().asTypeNames().size(), is(0));
}
}

0 comments on commit 76d90b1

Please sign in to comment.