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 bc59511320 Utility class modernization
bc59511320 is described below
commit bc595113209c39f55d9cf11a1dad2210646997ef
Author: James Bognar <[email protected]>
AuthorDate: Fri Nov 7 17:42:22 2025 -0500
Utility class modernization
---
.../juneau/common/reflect/AnnotationProvider.java | 33 +++++++++++++++++++--
.../apache/juneau/common/reflect/ClassInfo.java | 34 ----------------------
.../juneau/common/reflect/ParameterInfo.java | 4 +--
.../java/org/apache/juneau/BeanPropertyMeta.java | 10 +++----
.../main/java/org/apache/juneau/BeanRegistry.java | 2 +-
.../src/main/java/org/apache/juneau/ClassMeta.java | 4 +--
.../rest/swagger/BasicSwaggerProviderSession.java | 6 ++--
7 files changed, 44 insertions(+), 49 deletions(-)
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider.java
index 7823a477d0..f0a9f12824 100644
---
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider.java
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/AnnotationProvider.java
@@ -874,13 +874,42 @@ public class AnnotationProvider {
* @param action The action to perform on each matching annotation.
*/
public <A extends Annotation> void forEachMethodAnnotation(Class<A>
type, MethodInfo mi, Predicate<A> filter, Consumer<A> action) {
- mi.getDeclaringClass().forEachAnnotation(this, type, filter,
action);
+ forEachClassAnnotation(type, mi.getDeclaringClass(), filter,
action);
rstream(mi.getMatchingMethods())
.flatMap(m -> m.getDeclaredAnnotationInfos().stream())
.map(AnnotationInfo::inner)
.filter(type::isInstance)
.map(type::cast)
.forEach(a -> consumeIf(filter, action, a));
- mi.getReturnType().unwrap(Value.class,
Optional.class).forEachAnnotation(this, type, filter, action);
+ forEachClassAnnotation(type,
mi.getReturnType().unwrap(Value.class, Optional.class), filter, action);
+ }
+
+ /**
+ * Iterates through annotations on a class hierarchy.
+ *
+ * <p>
+ * This traverses annotations in parent-first order from:
+ * <ol>
+ * <li>Package annotations
+ * <li>Interface hierarchy (parent-first)
+ * <li>Class hierarchy (parent-first)
+ * </ol>
+ *
+ * @param <A> The annotation type.
+ * @param type The annotation type to search for.
+ * @param ci The class info to traverse.
+ * @param filter Optional filter to apply to annotations. Can be
<jk>null</jk>.
+ * @param action The action to perform on each matching annotation.
+ */
+ public <A extends Annotation> void forEachClassAnnotation(Class<A>
type, ClassInfo ci, Predicate<A> filter, Consumer<A> action) {
+ A t2 = ci.getPackageAnnotation(type);
+ if (nn(t2))
+ consumeIf(filter, action, t2);
+ var interfaces2 = ci.getInterfaces();
+ for (int i = interfaces2.size() - 1; i >= 0; i--)
+ findDeclaredParentFirst(type,
interfaces2.get(i).inner()).map(x -> x.inner()).filter(x -> filter == null ||
filter.test(x)).forEach(x -> action.accept(x));
+ var parents2 = ci.getParents();
+ for (int i = parents2.size() - 1; i >= 0; i--)
+ findDeclaredParentFirst(type,
parents2.get(i).inner()).map(x -> x.inner()).filter(x -> filter == null ||
filter.test(x)).forEach(x -> action.accept(x));
}
}
\ No newline at end of file
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 ae91c751a9..393d4b1077 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
@@ -292,40 +292,6 @@ public class ClassInfo extends ElementInfo implements
Annotatable {
}
- /**
- * Performs an action on all matching annotations on this class and
superclasses/interfaces.
- *
- * <p>
- * Annotations are appended in the following orders:
- * <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 <A> The annotation type to look for.
- * @param annotationProvider The annotation provider.
- * @param type The annotation to look for.
- * @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 <A extends Annotation> ClassInfo
forEachAnnotation(AnnotationProvider annotationProvider, Class<A> type,
Predicate<A> filter, Consumer<A> action) {
- if (annotationProvider == null)
- throw unsupportedOp();
- A t2 = getPackageAnnotation(type);
- if (nn(t2))
- consumeIf(filter, action, t2);
- var interfaces2 = interfaces.get();
- for (int i = interfaces2.size() - 1; i >= 0; i--)
- annotationProvider.findDeclaredParentFirst(type,
interfaces2.get(i).inner()).map(x -> x.inner()).filter(x -> filter == null ||
filter.test(x)).forEach(x -> action.accept(x));
- var parents2 = parents.get();
- for (int i = parents2.size() - 1; i >= 0; i--)
- annotationProvider.findDeclaredParentFirst(type,
parents2.get(i).inner()).map(x -> x.inner()).filter(x -> filter == null ||
filter.test(x)).forEach(x -> action.accept(x));
- 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/ParameterInfo.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ParameterInfo.java
index 24c1851884..fac0dd1687 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
@@ -775,14 +775,14 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
if (executable.isConstructor()) {
var ci =
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class);
var annotationInfos = getAnnotationInfos();
- ci.forEachAnnotation(ap, a, filter, action);
+ ap.forEachClassAnnotation(a, ci, filter, action);
for (var ai : annotationInfos)
if (a.isInstance(ai.inner()))
consumeIf(filter, action,
a.cast(ai.inner()));
} else {
var mi = (MethodInfo)executable;
var ci =
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class);
- ci.forEachAnnotation(ap, a, filter, action);
+ ap.forEachClassAnnotation(a, ci, filter, action);
rstream(mi.getMatchingMethods()).forEach(x -> {
x.getParameter(index).getAnnotationInfos().stream()
.filter(ai -> a.isInstance(ai.inner()))
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
index bdba1ed0f5..cc55bacc40 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
@@ -679,10 +679,10 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
BeanContext bc = beanContext;
if (a == null)
return l;
-
getBeanMeta().getClassMeta().getInfo().forEachAnnotation(bc.getAnnotationProvider(),
a, x -> true, x -> l.add(x));
+ bc.getAnnotationProvider().forEachClassAnnotation(a,
getBeanMeta().getClassMeta().getInfo(), x -> true, x -> l.add(x));
if (nn(field)) {
bc.getAnnotationProvider().find(a, field).map(x ->
x.inner()).filter(x -> true).forEach(x -> l.add(x));
-
ClassInfo.of(field.getType()).forEachAnnotation(bc.getAnnotationProvider(), a,
x -> true, x -> l.add(x));
+ bc.getAnnotationProvider().forEachClassAnnotation(a,
ClassInfo.of(field.getType()), x -> true, x -> l.add(x));
}
if (nn(getter)) {
// Walk up the inheritance hierarchy for the getter
method
@@ -690,7 +690,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
bc.getAnnotationProvider().find(a,
parentGetter).map(x -> x.inner()).filter(x -> true).forEach(x -> l.add(x));
});
bc.getAnnotationProvider().find(a, getter).map(x ->
x.inner()).filter(x -> true).forEach(x -> l.add(x));
-
ClassInfo.of(getter.getReturnType()).forEachAnnotation(bc.getAnnotationProvider(),
a, x -> true, x -> l.add(x));
+ bc.getAnnotationProvider().forEachClassAnnotation(a,
ClassInfo.of(getter.getReturnType()), x -> true, x -> l.add(x));
}
if (nn(setter)) {
// Walk up the inheritance hierarchy for the setter
method
@@ -698,7 +698,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
bc.getAnnotationProvider().find(a,
parentSetter).map(x -> x.inner()).filter(x -> true).forEach(x -> l.add(x));
});
bc.getAnnotationProvider().find(a, setter).map(x ->
x.inner()).filter(x -> true).forEach(x -> l.add(x));
-
ClassInfo.of(setter.getReturnType()).forEachAnnotation(bc.getAnnotationProvider(),
a, x -> true, x -> l.add(x));
+ bc.getAnnotationProvider().forEachClassAnnotation(a,
ClassInfo.of(setter.getReturnType()), x -> true, x -> l.add(x));
}
if (nn(extraKeys)) {
// Walk up the inheritance hierarchy for the extraKeys
method
@@ -706,7 +706,7 @@ public class BeanPropertyMeta implements
Comparable<BeanPropertyMeta> {
bc.getAnnotationProvider().find(a,
parentExtraKeys).map(x -> x.inner()).filter(x -> true).forEach(x -> l.add(x));
});
bc.getAnnotationProvider().find(a, extraKeys).map(x ->
x.inner()).filter(x -> true).forEach(x -> l.add(x));
-
ClassInfo.of(extraKeys.getReturnType()).forEachAnnotation(bc.getAnnotationProvider(),
a, x -> true, x -> l.add(x));
+ bc.getAnnotationProvider().forEachClassAnnotation(a,
ClassInfo.of(extraKeys.getReturnType()), x -> true, x -> l.add(x));
}
return l;
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
index c3ccf49c07..1486c78d40 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanRegistry.java
@@ -150,7 +150,7 @@ public class BeanRegistry {
});
} else {
Value<String> typeName = Value.empty();
-
ci.forEachAnnotation(beanContext.getAnnotationProvider(), Bean.class, x ->
isNotEmpty(x.typeName()), x -> typeName.set(x.typeName()));
+
beanContext.getAnnotationProvider().forEachClassAnnotation(Bean.class, ci, x ->
isNotEmpty(x.typeName()), x -> typeName.set(x.typeName()));
addToMap(typeName.orElseThrow(() -> new
BeanRuntimeException("Class ''{0}'' was passed to BeanRegistry but it doesn't
have a @Bean(typeName) annotation defined.", cn(c))),
beanContext.getClassMeta(c));
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
index 4b001e3e83..ce9cdc1d07 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
@@ -467,7 +467,7 @@ public class ClassMeta<T> implements Type {
private BeanFilter findBeanFilter(BeanContext bc) {
try {
List<Bean> ba = list();
-
info.forEachAnnotation(bc.getAnnotationProvider(), Bean.class, x -> true, x ->
ba.add(x));
+
bc.getAnnotationProvider().forEachClassAnnotation(Bean.class, info, x -> true,
x -> ba.add(x));
if (! ba.isEmpty())
return
BeanFilter.create(innerClass).applyAnnotations(ba).build();
} catch (Exception e) {
@@ -483,7 +483,7 @@ public class ClassMeta<T> implements Type {
private MarshalledFilter findMarshalledFilter(BeanContext bc) {
try {
List<Marshalled> ba = list();
-
info.forEachAnnotation(bc.getAnnotationProvider(), Marshalled.class, x -> true,
x -> ba.add(x));
+
bc.getAnnotationProvider().forEachClassAnnotation(Marshalled.class, info, x ->
true, x -> ba.add(x));
if (! ba.isEmpty())
return
MarshalledFilter.create(innerClass).applyAnnotations(ba).build();
} catch (Exception e) {
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java
index e83d07bf62..354a566085 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java
@@ -157,7 +157,7 @@ public class BasicSwaggerProviderSession {
// Combine it with @Rest(swagger)
List<Rest> restAnnotations = list();
- rci.forEachAnnotation(context.getAnnotationProvider(),
Rest.class, x -> true, x -> restAnnotations.add(x));
+
context.getAnnotationProvider().forEachClassAnnotation(Rest.class, rci, x ->
true, x -> restAnnotations.add(x));
for (var rr : restAnnotations) {
JsonMap sInfo = omSwagger.getMap("info", true);
@@ -435,9 +435,9 @@ public class BasicSwaggerProviderSession {
for (var eci : mi.getExceptionTypes()) {
if (eci.hasAnnotation(Response.class)) {
List<Response> la = list();
-
eci.forEachAnnotation(context.getAnnotationProvider(), Response.class, x ->
true, x -> la.add(x));
+
context.getAnnotationProvider().forEachClassAnnotation(Response.class, eci, x
-> true, x -> la.add(x));
List<StatusCode> la2 = list();
-
eci.forEachAnnotation(context.getAnnotationProvider(), StatusCode.class, x ->
true, x -> la2.add(x));
+
context.getAnnotationProvider().forEachClassAnnotation(StatusCode.class, eci, x
-> true, x -> la2.add(x));
Set<Integer> codes = getCodes(la2, 500);
for (var a : la) {
for (var code : codes) {