This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 72471edbf7 org.apache.juneau.common.reflect API improvements
72471edbf7 is described below
commit 72471edbf78ac57feb4bb6558768ef423c4e4fc0
Author: James Bognar <[email protected]>
AuthorDate: Mon Nov 17 11:07:51 2025 -0500
org.apache.juneau.common.reflect API improvements
---
.../juneau/common/reflect/AnnotationInfo.java | 56 ++++++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationInfo.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationInfo.java
index 4a80de0efe..f3b473ac7d 100644
---
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationInfo.java
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationInfo.java
@@ -529,6 +529,31 @@ public class AnnotationInfo<T extends Annotation> {
return
(Optional<Class<?>>)(Optional<?>)getMethod(methodName).filter(x ->
x.hasReturnType(Class.class)).map(x -> x.invoke(a));
}
+ /**
+ * Returns the value of the specified method on this annotation as a
class of a specific type.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bjava'>
+ * <jc>// Get a serializer class from an annotation</jc>
+ * Optional<Class<? <jk>extends</jk> Serializer>>
<jv>serializerClass</jv> =
+ *
<jv>annotationInfo</jv>.getClassValue(<js>"serializer"</js>,
Serializer.<jk>class</jk>);
+ * </p>
+ *
+ * @param <T> The expected supertype of the class.
+ * @param methodName The method name.
+ * @param type The expected supertype of the class value.
+ * @return An optional containing the value of the specified method
cast to the expected type,
+ * or empty if not found, not a class, or not assignable to the
expected type.
+ */
+ @SuppressWarnings("unchecked")
+ public <T> Optional<Class<? extends T>> getClassValue(String
methodName, Class<T> type) {
+ return getMethod(methodName)
+ .filter(x -> x.hasReturnType(Class.class))
+ .map(x -> (Class<?>)x.invoke(a))
+ .filter(type::isAssignableFrom)
+ .map(x -> (Class<? extends T>)x);
+ }
+
/**
* Returns the value of the specified method on this annotation as a
string array.
*
@@ -550,6 +575,37 @@ public class AnnotationInfo<T extends Annotation> {
return
(Optional<Class<?>[]>)(Optional<?>)getMethod(methodName).filter(x ->
x.hasReturnType(Class[].class)).map(x -> x.invoke(a));
}
+ /**
+ * Returns the value of the specified method on this annotation as a
class array of a specific type.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bjava'>
+ * <jc>// Get an array of serializer classes from an
annotation</jc>
+ * Optional<Class<? <jk>extends</jk> Serializer>[]>
<jv>serializerClasses</jv> =
+ *
<jv>annotationInfo</jv>.getClassArray(<js>"serializers"</js>,
Serializer.<jk>class</jk>);
+ * </p>
+ *
+ * @param <T> The expected supertype of the classes.
+ * @param methodName The method name.
+ * @param type The expected supertype of the class values.
+ * @return An optional containing the value of the specified method
cast to the expected type,
+ * or empty if not found, not a class array, or any element is
not assignable to the expected type.
+ */
+ @SuppressWarnings("unchecked")
+ public <T> Optional<Class<? extends T>[]> getClassArray(String
methodName, Class<T> type) {
+ return getMethod(methodName)
+ .filter(x -> x.hasReturnType(Class[].class))
+ .map(x -> (Class<?>[])x.invoke(a))
+ .filter(arr -> {
+ for (var c : arr) {
+ if (!type.isAssignableFrom(c))
+ return false;
+ }
+ return true;
+ })
+ .map(x -> (Class<? extends T>[])x);
+ }
+
/**
* Returns the return type of the specified method on this annotation.
*