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 5ad2386722 Utility class modernization
5ad2386722 is described below
commit 5ad23867222ae1adeb9502987f5e474ef968e9bc
Author: James Bognar <[email protected]>
AuthorDate: Wed Nov 5 17:09:36 2025 -0500
Utility class modernization
---
.../juneau/common/reflect/AnnotationProvider2.java | 4 +-
.../apache/juneau/common/reflect/ClassInfo.java | 21 -----
.../apache/juneau/common/reflect/MethodInfo.java | 89 ++++++++++++----------
.../juneau/common/reflect/ParameterInfo.java | 2 +-
.../java/org/apache/juneau/rest/RestContext.java | 2 +-
.../juneau/common/reflect/MethodInfo_Test.java | 2 +-
6 files changed, 54 insertions(+), 66 deletions(-)
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider2.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider2.java
index 7e97c077c5..4855a01799 100644
---
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider2.java
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider2.java
@@ -297,9 +297,7 @@ public class AnnotationProvider2 {
assertArgNotNull("onClass", onClass);
var list = classDeclaredAnnotations.get(onClass);
// Iterate backwards to get parent-to-child order
- return java.util.stream.IntStream.range(0, list.size())
- .map(i -> list.size() - 1 - i)
- .mapToObj(list::get);
+ return rstream(list);
}
/**
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ClassInfo.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ClassInfo.java
index f48e8bcf51..651fe4a148 100644
---
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ClassInfo.java
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ClassInfo.java
@@ -449,27 +449,6 @@ public class ClassInfo extends ElementInfo implements
Annotatable {
return forEachAnnotation(null, type, filter, action);
}
- /**
- * Performs an action on all matching annotations on this
class/parents/package.
- *
- * <p>
- * Annotations are consumed in the following order:
- * <ol>
- * <li>On the package of this class.
- * <li>On interfaces ordered parent-to-child.
- * <li>On parent classes ordered parent-to-child.
- * <li>On this class.
- * </ol>
- *
- * @param filter A predicate to apply to the entries to determine if
action should be performed. Can be <jk>null</jk>.
- * @param action An action to perform on the entry.
- * @return This object.
- */
- public ClassInfo forEachAnnotationInfo(Predicate<AnnotationInfo<?>>
filter, Consumer<AnnotationInfo<?>> action) {
- AnnotationInfo.forEachAnnotationInfo(this, filter, action);
- return this;
- }
-
/**
* Returns all fields on this class and all parent classes.
*
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/MethodInfo.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/MethodInfo.java
index 64bdbf712f..edbbd79506 100644
---
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/MethodInfo.java
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/MethodInfo.java
@@ -16,6 +16,7 @@
*/
package org.apache.juneau.common.reflect;
+import static org.apache.juneau.common.utils.AssertionUtils.*;
import static org.apache.juneau.common.utils.CollectionUtils.*;
import static org.apache.juneau.common.utils.PredicateUtils.*;
import static org.apache.juneau.common.utils.Utils.*;
@@ -25,6 +26,7 @@ import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.*;
+import java.util.stream.*;
import org.apache.juneau.common.collections.*;
import org.apache.juneau.common.utils.*;
@@ -94,6 +96,9 @@ public class MethodInfo extends ExecutableInfo implements
Comparable<MethodInfo>
private final Supplier<List<MethodInfo>> matchingMethods =
memoize(this::_findMatchingMethods);
+ // All annotations on this method and parent overridden methods in
child-to-parent order.
+ private final Supplier<List<AnnotationInfo<Annotation>>>
annotationInfos = memoize(this::findAnnotationInfos);
+
/**
* Constructor.
*
@@ -146,6 +151,41 @@ public class MethodInfo extends ExecutableInfo implements
Comparable<MethodInfo>
return matchingMethods.get();
}
+ /**
+ * Returns all annotations on this method and parent overridden methods
in child-to-parent order.
+ *
+ * <p>
+ * Results include annotations from:
+ * <ul>
+ * <li>This method
+ * <li>Matching methods in parent classes
+ * <li>Matching methods in interfaces
+ * </ul>
+ *
+ * <p>
+ * List is unmodifiable.
+ *
+ * @return A list of all annotations on this method and overridden
methods.
+ */
+ public List<AnnotationInfo<Annotation>> getAnnotationInfos() {
+ return annotationInfos.get();
+ }
+
+ /**
+ * Returns all annotations of the specified type on this method and
parent overridden methods.
+ *
+ * @param <A> The annotation type.
+ * @param type The annotation type to filter by.
+ * @return A stream of matching annotation infos.
+ */
+ @SuppressWarnings("unchecked")
+ public <A extends Annotation> Stream<AnnotationInfo<A>>
getAnnotationInfos(Class<A> type) {
+ assertArgNotNull("type", type);
+ return getAnnotationInfos().stream()
+ .filter(a -> a.isType(type))
+ .map(a -> (AnnotationInfo<A>)a);
+ }
+
private List<MethodInfo> _findMatchingMethods() {
var result = new ArrayList<MethodInfo>();
result.add(this); // 1. This method
@@ -182,6 +222,12 @@ public class MethodInfo extends ExecutableInfo implements
Comparable<MethodInfo>
.forEach(pi -> addMatchingMethodsFromInterface(result,
pi));
}
+ private List<AnnotationInfo<Annotation>> findAnnotationInfos() {
+ var list = new ArrayList<AnnotationInfo<Annotation>>();
+ getMatching().forEach(m ->
list.addAll(m.getDeclaredAnnotationInfos()));
+ return u(list);
+ }
+
@Override /* Overridden from ExecutableInfo */
public MethodInfo accessible() {
super.accessible();
@@ -333,47 +379,12 @@ public class MethodInfo extends ExecutableInfo implements
Comparable<MethodInfo>
return forEachAnnotation(AnnotationProvider.DEFAULT, type,
filter, action);
}
- /**
- * Perform an action on all matching annotations on this method.
- *
- * @param filter A predicate to apply to the entries to determine if
action should be performed. Can be <jk>null</jk>.
- * @param action An action to perform on the entry.
- * @return This object.
- */
- public MethodInfo forEachAnnotationInfo(Predicate<AnnotationInfo<?>>
filter, Consumer<AnnotationInfo<?>> action) {
- AnnotationInfo.forEachAnnotationInfo(this, filter, action);
- return this;
- }
-
- /**
- * Performs an action on all matching declared methods with the same
name and arguments on all superclasses and interfaces.
- *
- * <p>
- * Methods are accessed from child-to-parent order.
- *
- * @param filter A predicate to apply to the entries to determine if
action should be performed. Can be <jk>null</jk>.
- * @param action An action to perform on the entry.
- * @return This object.
- */
- public MethodInfo forEachMatching(Predicate<MethodInfo> filter,
Consumer<MethodInfo> action) {
- for (var m : matchingCache.get())
- consumeIf(filter, action, m);
- return this;
+ public Stream<MethodInfo> getMatching() {
+ return matchingCache.get().stream();
}
- /**
- * Performs an action on all matching declared methods with the same
name and arguments on all superclasses and interfaces.
- *
- * <p>
- * Methods are accessed from parent-to-child order.
- *
- * @param filter A predicate to apply to the entries to determine if
action should be performed. Can be <jk>null</jk>.
- * @param action An action to perform on the entry.
- * @return This object.
- */
- public MethodInfo forEachMatchingParentFirst(Predicate<MethodInfo>
filter, Consumer<MethodInfo> action) {
- rstream(matchingCache.get()).forEach(m -> consumeIf(filter,
action, m));
- return this;
+ public Stream<MethodInfo> getMatchingParentFirst() {
+ return rstream(matchingCache.get());
}
/**
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ParameterInfo.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ParameterInfo.java
index 3acae2067f..c809310997 100644
---
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ParameterInfo.java
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ParameterInfo.java
@@ -766,7 +766,7 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
var mi = (MethodInfo)executable;
var ci =
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class);
ci.forEachAnnotation(ap, a, filter, action);
- mi.forEachMatchingParentFirst(x -> true, x -> {
+ mi.getMatchingParentFirst().forEach(x -> {
x.getParameter(index).getAnnotationInfos().stream()
.filter(ai -> a.isInstance(ai.inner()))
.map(ai -> a.cast(ai.inner()))
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
index f403acc196..7cbe61e5b9 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -4582,7 +4582,7 @@ public class RestContext extends Context {
// Also include methods on @Rest-annotated
interfaces.
if (al.isEmpty()) {
Predicate<MethodInfo>
isRestAnnotatedInterface = x -> x.getDeclaringClass().isInterface() &&
nn(x.getDeclaringClass().getAnnotation(Rest.class));
-
mi.forEachMatching(isRestAnnotatedInterface, x -> al.add(AnnotationInfo.of(x,
RestOpAnnotation.DEFAULT)));
+
mi.getMatching().filter(isRestAnnotatedInterface).forEach(x ->
al.add(AnnotationInfo.of(x, RestOpAnnotation.DEFAULT)));
}
if (al.size() > 0) {
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/common/reflect/MethodInfo_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/common/reflect/MethodInfo_Test.java
index 9f5285bc68..bc0b62a81a 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/common/reflect/MethodInfo_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/common/reflect/MethodInfo_Test.java
@@ -155,7 +155,7 @@ class MethodInfo_Test extends TestBase {
@Test void findMatchingMethods() throws Exception {
var mi = MethodInfo.of(B3.class.getMethod("foo", int.class));
var l = new ArrayList<MethodInfo>();
- mi.forEachMatching(x -> true, l::add);
+ mi.getMatching().forEach(l::add);
check("B3.foo(int),B2.foo(int),B1.foo(int)", l);
}