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 28a4f9cf68 Utility class modernization
28a4f9cf68 is described below
commit 28a4f9cf68735ea946990ac31e05afa2077d3998
Author: James Bognar <[email protected]>
AuthorDate: Thu Nov 6 17:30:50 2025 -0500
Utility class modernization
---
.../juneau/common/reflect/AnnotationProvider.java | 87 +++++-----------------
.../apache/juneau/common/reflect/ClassInfo.java | 39 ++++++++--
.../apache/juneau/common/reflect/MethodInfo.java | 8 +-
.../juneau/common/reflect/ParameterInfo.java | 54 +++++++++++++-
4 files changed, 113 insertions(+), 75 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 bfeb5a64ca..2dceb8bd77 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
@@ -18,6 +18,7 @@ package org.apache.juneau.common.reflect;
import static org.apache.juneau.common.utils.CollectionUtils.*;
import static org.apache.juneau.common.utils.PredicateUtils.*;
+import static org.apache.juneau.common.utils.ThrowableUtils.*;
import static org.apache.juneau.common.utils.Utils.*;
import java.lang.annotation.*;
@@ -42,135 +43,85 @@ public interface AnnotationProvider {
/**
* Default metadata provider.
+ *
+ * <p>
+ * This is no longer used. All callers have been updated to pass
non-null AnnotationProvider instances.
*/
AnnotationProvider DEFAULT = new AnnotationProvider() {
- // @formatter:off
- private final Cache2<Class<?>,Class<? extends
Annotation>,List<Annotation>> classAnnotationCache = Cache2.<Class<?>,Class<?
extends
Annotation>,List<Annotation>>create().disableCaching(DISABLE_ANNOTATION_CACHING).supplier((k1,
k2) -> u(l(k1.getAnnotationsByType(k2)))).build();
- private final Cache2<Class<?>,Class<? extends
Annotation>,List<Annotation>> declaredClassAnnotationCache =
Cache2.<Class<?>,Class<? extends
Annotation>,List<Annotation>>create().disableCaching(DISABLE_ANNOTATION_CACHING).supplier((k1,
k2) -> u(l(k1.getDeclaredAnnotationsByType(k2)))).build();
- private final Cache2<Method,Class<? extends
Annotation>,List<Annotation>> methodAnnotationCache = Cache2.<Method,Class<?
extends
Annotation>,List<Annotation>>create().disableCaching(DISABLE_ANNOTATION_CACHING).supplier((k1,
k2) -> u(l(k1.getAnnotationsByType(k2)))).build();
- private final Cache2<Field,Class<? extends
Annotation>,List<Annotation>> fieldAnnotationCache = Cache2.<Field,Class<?
extends
Annotation>,List<Annotation>>create().disableCaching(DISABLE_ANNOTATION_CACHING).supplier((k1,
k2) -> u(l(k1.getAnnotationsByType(k2)))).build();
- private final Cache2<Constructor<?>,Class<? extends
Annotation>,List<Annotation>> constructorAnnotationCache =
Cache2.<Constructor<?>,Class<? extends
Annotation>,List<Annotation>>create().disableCaching(DISABLE_ANNOTATION_CACHING).supplier((k1,
k2) -> u(l(k1.getAnnotationsByType(k2)))).build();
- // @formatter:on
-
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A firstAnnotation(Class<A> type,
Class<?> onClass, Predicate<A> filter) {
- return nn(type) && nn(onClass)
- ? annotations(type, onClass).stream().filter(a
-> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A firstAnnotation(Class<A> type,
Constructor<?> onConstructor, Predicate<A> filter) {
- return nn(type) && nn(onConstructor)
- ? annotations(type,
onConstructor).stream().filter(a -> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A firstAnnotation(Class<A> type,
Field onField, Predicate<A> filter) {
- return nn(type) && nn(onField)
- ? annotations(type, onField).stream().filter(a
-> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A firstAnnotation(Class<A> type,
Method onMethod, Predicate<A> filter) {
- return nn(type) && nn(onMethod)
- ? annotations(type, onMethod).stream().filter(a
-> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A
firstDeclaredAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter) {
- return nn(type) && nn(onClass)
- ? declaredAnnotations(type,
onClass).stream().filter(a -> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> void forEachAnnotation(Class<A>
type, Class<?> onClass, Predicate<A> filter, Consumer<A> action) {
- if (nn(type) && nn(onClass))
- annotations(type, onClass).stream().forEach(a
-> consumeIf(filter, action, a));
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> void forEachAnnotation(Class<A>
type, Constructor<?> onConstructor, Predicate<A> filter, Consumer<A> action) {
- if (nn(type) && nn(onConstructor))
- annotations(type,
onConstructor).stream().forEach(a -> consumeIf(filter, action, a));
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> void forEachAnnotation(Class<A>
type, Field onField, Predicate<A> filter, Consumer<A> action) {
- if (nn(type) && nn(onField))
- annotations(type, onField).stream().forEach(a
-> consumeIf(filter, action, a));
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> void forEachAnnotation(Class<A>
type, Method onMethod, Predicate<A> filter, Consumer<A> action) {
- if (nn(type) && nn(onMethod))
- annotations(type, onMethod).stream().forEach(a
-> consumeIf(filter, action, a));
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> void
forEachDeclaredAnnotation(Class<A> type, Class<?> onClass, Predicate<A> filter,
Consumer<A> action) {
- if (nn(type) && nn(onClass))
- declaredAnnotations(type,
onClass).stream().forEach(a -> consumeIf(filter, action, a));
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A lastAnnotation(Class<A> type,
Class<?> onClass, Predicate<A> filter) {
- return nn(type) && nn(onClass)
- ? rstream(annotations(type, onClass)).filter(a
-> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A lastAnnotation(Class<A> type,
Constructor<?> onConstructor, Predicate<A> filter) {
- return nn(type) && nn(onConstructor)
- ? rstream(annotations(type,
onConstructor)).filter(a -> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A lastAnnotation(Class<A> type,
Field onField, Predicate<A> filter) {
- return nn(type) && nn(onField)
- ? rstream(annotations(type, onField)).filter(a
-> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A lastAnnotation(Class<A> type,
Method onMethod, Predicate<A> filter) {
- return nn(type) && nn(onMethod)
- ? rstream(annotations(type, onMethod)).filter(a
-> test(filter, a)).findFirst().orElse(null)
- : null;
+ throw unsupportedOp();
}
@Override /* Overridden from AnnotationProvider */
public <A extends Annotation> A lastDeclaredAnnotation(Class<A>
type, Class<?> onClass, Predicate<A> filter) {
- return nn(type) && nn(onClass)
- ? rstream(declaredAnnotations(type,
onClass)).filter(a -> test(filter, a)).findFirst().orElse(null)
- : null;
- }
-
- private <A extends Annotation> List<A> annotations(Class<A>
type, Class<?> onClass) {
- return (List<A>)classAnnotationCache.get(onClass, type);
- }
-
- private <A extends Annotation> List<A> annotations(Class<A>
type, Constructor<?> onConstructor) {
- return
(List<A>)constructorAnnotationCache.get(onConstructor, type);
- }
-
- private <A extends Annotation> List<A> annotations(Class<A>
type, Field onField) {
- return (List<A>)fieldAnnotationCache.get(onField, type);
- }
-
- private <A extends Annotation> List<A> annotations(Class<A>
type, Method onMethod) {
- return (List<A>)methodAnnotationCache.get(onMethod,
type);
- }
-
- private <A extends Annotation> List<A>
declaredAnnotations(Class<A> type, Class<?> onClass) {
- return
(List<A>)declaredClassAnnotationCache.get(onClass, type);
+ throw unsupportedOp();
}
};
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 21ea48ec51..6ae22f3715 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
@@ -420,7 +420,7 @@ public class ClassInfo extends ElementInfo implements
Annotatable {
*/
public <A extends Annotation> ClassInfo
forEachAnnotation(AnnotationProvider annotationProvider, Class<A> type,
Predicate<A> filter, Consumer<A> action) {
if (annotationProvider == null)
- annotationProvider = AnnotationProvider.DEFAULT;
+ throw unsupportedOp();
A t2 = getPackageAnnotation(type);
if (nn(t2))
consumeIf(filter, action, t2);
@@ -1663,7 +1663,7 @@ public class ClassInfo extends ElementInfo implements
Annotatable {
*/
public <A extends Annotation> boolean hasAnnotation(AnnotationProvider
annotationProvider, Class<A> type) {
if (annotationProvider == null)
- annotationProvider = AnnotationProvider.DEFAULT;
+ throw unsupportedOp();
return nn(annotationProvider.firstAnnotation(type, c, x ->
true));
}
@@ -2174,7 +2174,7 @@ public class ClassInfo extends ElementInfo implements
Annotatable {
*/
public <A extends Annotation> A lastAnnotation(AnnotationProvider
annotationProvider, Class<A> type, Predicate<A> filter) {
if (annotationProvider == null)
- annotationProvider = AnnotationProvider.DEFAULT;
+ throw unsupportedOp();
A x = null;
x = annotationProvider.lastAnnotation(type, inner(), filter);
if (nn(x) && test(filter, x))
@@ -2215,7 +2215,36 @@ public class ClassInfo extends ElementInfo implements
Annotatable {
* @return This object.
*/
public <A extends Annotation> A lastAnnotation(Class<A> type,
Predicate<A> filter) {
- return lastAnnotation(null, type, filter);
+ // Inline implementation using reflection directly instead of
delegating to AnnotationProvider
+ if (!nn(type))
+ return null;
+
+ // Search annotations using reflection (reverse order for
"last")
+ var annotations = rstream(l(c.getAnnotationsByType(type)));
+ var result = annotations.filter(a -> test(filter,
a)).findFirst().orElse(null);
+ if (nn(result))
+ return result;
+
+ // Search parents
+ var parents2 = parents.get();
+ for (var parent : parents2) {
+ var parentAnnotations =
rstream(l(parent.inner().getAnnotationsByType(type)));
+ result = parentAnnotations.filter(a -> test(filter,
a)).findFirst().orElse(null);
+ if (nn(result))
+ return result;
+ }
+
+ // Search interfaces
+ var interfaces2 = interfaces.get();
+ for (var iface : interfaces2) {
+ var ifaceAnnotations =
rstream(l(iface.inner().getAnnotationsByType(type)));
+ result = ifaceAnnotations.filter(a -> test(filter,
a)).findFirst().orElse(null);
+ if (nn(result))
+ return result;
+ }
+
+ // Search package
+ return getPackageAnnotation(type);
}
/**
@@ -2309,7 +2338,7 @@ public class ClassInfo extends ElementInfo implements
Annotatable {
if (a == null)
return null;
if (ap == null)
- ap = AnnotationProvider.DEFAULT;
+ throw unsupportedOp();
A t = ap.firstDeclaredAnnotation(a, c, x -> true);
if (nn(t))
return t;
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 53877ea963..e32b32fbbf 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
@@ -606,7 +606,13 @@ public class MethodInfo extends ExecutableInfo implements
Comparable<MethodInfo>
*/
@Override
public <A extends Annotation> boolean hasAnnotation(Class<A> type) {
- return hasAnnotation(AnnotationProvider.DEFAULT, type);
+ // Inline implementation using reflection directly instead of
delegating to AnnotationProvider.DEFAULT
+ if (!nn(type))
+ return false;
+ for (var m2 : matchingCache.get())
+ if (m2.inner().getAnnotation(type) != null)
+ return true;
+ return false;
}
/**
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 c809310997..88f96734fd 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
@@ -134,7 +134,59 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
* @return This object.
*/
public <A extends Annotation> ParameterInfo forEachAnnotation(Class<A>
type, Predicate<A> filter, Consumer<A> action) {
- return forEachAnnotation(AnnotationProvider.DEFAULT, type,
filter, action);
+ // Inline implementation using reflection directly instead of
delegating to AnnotationProvider.DEFAULT
+ if (!nn(type))
+ return this;
+
+ if (executable.isConstructor) {
+ // For constructors: search parameter type hierarchy
and parameter annotations
+ var ci =
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class);
+ // Search class hierarchy using reflection (package ->
interfaces -> parents -> class)
+ var packageAnn = ci.getPackageAnnotation(type);
+ if (nn(packageAnn))
+ consumeIf(filter, action, packageAnn);
+ // Get annotations from interfaces (reverse order)
+ var interfaces2 = ci.getInterfaces();
+ for (int i = interfaces2.size() - 1; i >= 0; i--)
+ for (var ann :
interfaces2.get(i).inner().getDeclaredAnnotationsByType(type))
+ consumeIf(filter, action, ann);
+ // Get annotations from parent classes (reverse order)
+ var parents2 = ci.getParents();
+ for (int i = parents2.size() - 1; i >= 0; i--)
+ for (var ann :
parents2.get(i).inner().getDeclaredAnnotationsByType(type))
+ consumeIf(filter, action, ann);
+ // Get annotations directly from parameter
+ var annotationInfos = getAnnotationInfos();
+ for (var ai : annotationInfos)
+ if (type.isInstance(ai.inner()))
+ consumeIf(filter, action,
type.cast(ai.inner()));
+ } else {
+ // For methods: search parameter type hierarchy and
matching parent methods
+ var mi = (MethodInfo)executable;
+ var ci =
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class);
+ // Search class hierarchy using reflection (package ->
interfaces -> parents -> class)
+ var packageAnn = ci.getPackageAnnotation(type);
+ if (nn(packageAnn))
+ consumeIf(filter, action, packageAnn);
+ // Get annotations from interfaces (reverse order)
+ var interfaces2 = ci.getInterfaces();
+ for (int i = interfaces2.size() - 1; i >= 0; i--)
+ for (var ann :
interfaces2.get(i).inner().getDeclaredAnnotationsByType(type))
+ consumeIf(filter, action, ann);
+ // Get annotations from parent classes (reverse order)
+ var parents2 = ci.getParents();
+ for (int i = parents2.size() - 1; i >= 0; i--)
+ for (var ann :
parents2.get(i).inner().getDeclaredAnnotationsByType(type))
+ consumeIf(filter, action, ann);
+ // Get annotations from matching parent methods'
parameters
+ mi.getMatchingParentFirst().forEach(x -> {
+
x.getParameter(index).getAnnotationInfos().stream()
+ .filter(ai ->
type.isInstance(ai.inner()))
+ .map(ai -> type.cast(ai.inner()))
+ .forEach(ann -> consumeIf(filter,
action, ann));
+ });
+ }
+ return this;
}
@SuppressWarnings("unchecked")