Skip to content

Commit 24e4180

Browse files
committed
Rename annotation to FilterMatcher / performance refactoring
1 parent 072f882 commit 24e4180

File tree

15 files changed

+357
-109
lines changed

15 files changed

+357
-109
lines changed

core/src/main/java/io/micronaut/core/annotation/AnnotationMetadata.java

+61
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.annotation.concurrent.Immutable;
3030
import java.lang.annotation.Annotation;
3131
import java.lang.annotation.Repeatable;
32+
import java.lang.reflect.Array;
3233
import java.util.*;
3334
import java.util.function.Function;
3435
import java.util.stream.Collectors;
@@ -657,6 +658,66 @@ default <E extends Enum> Optional<E> enumValue(@Nonnull Class<? extends Annotati
657658
return enumValue(annotation.getName(), member, enumType);
658659
}
659660

661+
662+
/**
663+
* The enum values for the given annotation.
664+
*
665+
* @param annotation The annotation
666+
* @param enumType The enum type
667+
* @param <E> The enum type
668+
* @return An array of enum values
669+
*/
670+
default <E extends Enum> E[] enumValues(@Nonnull String annotation, Class<E> enumType) {
671+
ArgumentUtils.requireNonNull("annotation", annotation);
672+
return enumValues(annotation, VALUE_MEMBER, enumType);
673+
}
674+
675+
/**
676+
* The enum values for the given annotation.
677+
*
678+
* @param annotation The annotation
679+
* @param member The annotation member
680+
* @param enumType The enum type
681+
* @param <E> The enum type
682+
* @return An array of enum values
683+
*/
684+
default <E extends Enum> E[] enumValues(@Nonnull String annotation, @Nonnull String member, Class<E> enumType) {
685+
ArgumentUtils.requireNonNull("annotation", annotation);
686+
ArgumentUtils.requireNonNull("member", member);
687+
688+
return (E[]) Array.newInstance(enumType, 0);
689+
}
690+
691+
/**
692+
* The enum values for the given annotation.
693+
*
694+
* @param annotation The annotation
695+
* @param enumType The enum type
696+
* @param <E> The enum type
697+
* @return An array of enum values
698+
*/
699+
default <E extends Enum> E[] enumValues(@Nonnull Class<? extends Annotation> annotation, Class<E> enumType) {
700+
ArgumentUtils.requireNonNull("annotation", annotation);
701+
702+
return enumValues(annotation, VALUE_MEMBER, enumType);
703+
}
704+
705+
/**
706+
* The enum values for the given annotation.
707+
*
708+
* @param annotation The annotation
709+
* @param member The annotation member
710+
* @param enumType The enum type
711+
* @param <E> The enum type
712+
* @return An array of enum values
713+
*/
714+
default <E extends Enum> E[] enumValues(@Nonnull Class<? extends Annotation> annotation, @Nonnull String member, Class<E> enumType) {
715+
ArgumentUtils.requireNonNull("annotation", annotation);
716+
ArgumentUtils.requireNonNull("member", member);
717+
718+
return enumValues(annotation.getName(), member, enumType);
719+
}
720+
660721
/**
661722
* The value of the annotation as a Class.
662723
*

core/src/main/java/io/micronaut/core/annotation/AnnotationMetadataDelegate.java

+30
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@
3232
*/
3333
public interface AnnotationMetadataDelegate extends AnnotationMetadataProvider, AnnotationMetadata {
3434

35+
@Override
36+
default boolean hasSimpleAnnotation(@Nullable String annotation) {
37+
return getAnnotationMetadata().hasSimpleAnnotation(annotation);
38+
}
39+
40+
@Override
41+
default boolean hasSimpleDeclaredAnnotation(@Nullable String annotation) {
42+
return getAnnotationMetadata().hasSimpleDeclaredAnnotation(annotation);
43+
}
44+
45+
@Override
46+
default <E extends Enum> E[] enumValues(@Nonnull String annotation, Class<E> enumType) {
47+
return getAnnotationMetadata().enumValues(annotation, enumType);
48+
}
49+
50+
@Override
51+
default <E extends Enum> E[] enumValues(@Nonnull String annotation, @Nonnull String member, Class<E> enumType) {
52+
return getAnnotationMetadata().enumValues(annotation, member, enumType);
53+
}
54+
55+
@Override
56+
default <E extends Enum> E[] enumValues(@Nonnull Class<? extends Annotation> annotation, Class<E> enumType) {
57+
return getAnnotationMetadata().enumValues(annotation, enumType);
58+
}
59+
60+
@Override
61+
default <E extends Enum> E[] enumValues(@Nonnull Class<? extends Annotation> annotation, @Nonnull String member, Class<E> enumType) {
62+
return getAnnotationMetadata().enumValues(annotation, member, enumType);
63+
}
64+
3565
@Override
3666
default <T> Class<T>[] classValues(@Nonnull String annotation) {
3767
return getAnnotationMetadata().classValues(annotation, VALUE_MEMBER);

core/src/main/java/io/micronaut/core/annotation/AnnotationValue.java

+31-17
Original file line numberDiff line numberDiff line change
@@ -222,25 +222,11 @@ public <E extends Enum> Optional<E> enumValue(@Nonnull String member, @Nonnull C
222222
@SuppressWarnings("unchecked")
223223
public <E extends Enum> E[] enumValues(@Nonnull String member, @Nonnull Class<E> enumType) {
224224
ArgumentUtils.requireNonNull("enumType", enumType);
225-
List<E> list = new ArrayList<>();
226225
if (StringUtils.isNotEmpty(member)) {
227226
Object rawValue = values.get(member);
228-
if (rawValue != null) {
229-
if (rawValue.getClass().isArray()) {
230-
int len = Array.getLength(rawValue);
231-
for (int i = 0; i < len; i++) {
232-
convertToEnum(enumType, Array.get(rawValue, i)).ifPresent(list::add);
233-
}
234-
} else if (rawValue instanceof Iterable) {
235-
for (Object o : (Iterable) rawValue) {
236-
convertToEnum(enumType, o).ifPresent(list::add);
237-
}
238-
} else if (enumType.isAssignableFrom(rawValue.getClass())) {
239-
list.add((E) rawValue);
240-
}
241-
}
227+
return resolveEnumValues(enumType, rawValue);
242228
}
243-
return list.toArray((E[]) Array.newInstance(enumType, 0));
229+
return (E[]) Array.newInstance(enumType, 0);
244230
}
245231

246232
/**
@@ -921,6 +907,34 @@ public static <T extends Annotation> AnnotationValueBuilder<T> builder(Class<T>
921907
return null;
922908
}
923909

910+
/**
911+
* The enum values for the given enum type and raw value.
912+
* @param enumType The enum type
913+
* @param rawValue The raw value
914+
* @param <E> The enum generic type
915+
* @return An array of enum values
916+
*/
917+
@Internal
918+
public static @Nonnull <E extends Enum> E[] resolveEnumValues(@Nonnull Class<E> enumType, @Nullable Object rawValue) {
919+
if (rawValue == null) {
920+
return (E[]) Array.newInstance(enumType, 0);
921+
}
922+
List<E> list = new ArrayList<>();
923+
if (rawValue.getClass().isArray()) {
924+
int len = Array.getLength(rawValue);
925+
for (int i = 0; i < len; i++) {
926+
convertToEnum(enumType, Array.get(rawValue, i)).ifPresent(list::add);
927+
}
928+
} else if (rawValue instanceof Iterable) {
929+
for (Object o : (Iterable) rawValue) {
930+
convertToEnum(enumType, o).ifPresent(list::add);
931+
}
932+
} else if (enumType.isAssignableFrom(rawValue.getClass())) {
933+
list.add((E) rawValue);
934+
}
935+
return list.toArray((E[]) Array.newInstance(enumType, 0));
936+
}
937+
924938
/**
925939
* The string[] values for the given value.
926940
* @param strs The strings
@@ -1012,7 +1026,7 @@ private ConvertibleValues<Object> newConvertibleValues(Map<CharSequence, Object>
10121026
return rawValue;
10131027
}
10141028

1015-
private <T extends Enum> Optional<T> convertToEnum(Class<T> enumType, Object o) {
1029+
private static <T extends Enum> Optional<T> convertToEnum(Class<T> enumType, Object o) {
10161030
if (enumType.isInstance(o)) {
10171031
return Optional.of((T) o);
10181032
} else {

core/src/main/java/io/micronaut/core/annotation/EmptyAnnotationMetadata.java

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.annotation.Nonnull;
2424
import javax.annotation.Nullable;
2525
import java.lang.annotation.Annotation;
26+
import java.lang.reflect.Array;
2627
import java.util.*;
2728

2829
/**
@@ -33,6 +34,27 @@
3334
*/
3435
@Internal
3536
final class EmptyAnnotationMetadata implements AnnotationMetadata {
37+
38+
@Override
39+
public <E extends Enum> E[] enumValues(@Nonnull String annotation, Class<E> enumType) {
40+
return (E[]) Array.newInstance(enumType, 0);
41+
}
42+
43+
@Override
44+
public <E extends Enum> E[] enumValues(@Nonnull String annotation, @Nonnull String member, Class<E> enumType) {
45+
return (E[]) Array.newInstance(enumType, 0);
46+
}
47+
48+
@Override
49+
public <E extends Enum> E[] enumValues(@Nonnull Class<? extends Annotation> annotation, Class<E> enumType) {
50+
return (E[]) Array.newInstance(enumType, 0);
51+
}
52+
53+
@Override
54+
public <E extends Enum> E[] enumValues(@Nonnull Class<? extends Annotation> annotation, @Nonnull String member, Class<E> enumType) {
55+
return (E[]) Array.newInstance(enumType, 0);
56+
}
57+
3658
@Nonnull
3759
@Override
3860
public List<String> getAnnotationNamesByStereotype(@Nullable String stereotype) {

0 commit comments

Comments
 (0)