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 ab70aca6ac Utility class modernization
ab70aca6ac is described below
commit ab70aca6ac85bdd36ee9a9f44e3d750dbf9c795f
Author: James Bognar <[email protected]>
AuthorDate: Tue Nov 4 10:13:37 2025 -0500
Utility class modernization
---
.../juneau/common/reflect/ParameterInfo.java | 101 +--------------------
.../juneau/httppart/bean/RequestBeanMeta.java | 2 +-
.../org/apache/juneau/rest/arg/FormDataArg.java | 12 +--
.../java/org/apache/juneau/rest/arg/HeaderArg.java | 12 +--
.../java/org/apache/juneau/rest/arg/PathArg.java | 24 ++---
.../java/org/apache/juneau/rest/arg/QueryArg.java | 12 +--
.../juneau/common/reflect/ParamInfoTest.java | 47 +++++-----
.../org/apache/juneau/reflect/ParamInfoTest.java | 47 +++++-----
8 files changed, 85 insertions(+), 172 deletions(-)
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 c32cd0f496..8bfafbfaad 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
@@ -42,17 +42,8 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
private final int index;
private final ClassInfo type;
- @SuppressWarnings({"rawtypes", "unchecked"})
- private final Cache annotationCache =
- Cache.of(Class.class, Optional.class)
- .supplier(k -> opt(findAnnotation(k)))
- .build();
-
@SuppressWarnings({"rawtypes"})
- private final Cache foundAnnotations =
- Cache.of(Class.class, List.class)
- .supplier(this::findAnnotationInfosInternal)
- .build();
+ private final Cache foundAnnotations = Cache.of(Class.class,
List.class).supplier(this::findAnnotationInfosInternal).build();
private final Supplier<List<AnnotationInfo<Annotation>>> annotations =
memoize(this::_findAnnotations);
private final Supplier<List<ParameterInfo>> matchingParameters =
memoize(this::_findMatchingParameters);
@@ -65,6 +56,7 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
* @param index The parameter index.
* @param type The parameter type.
*/
+ // TODO - Investigate if we can construct ClassInfo directly from
parameter.
protected ParameterInfo(ExecutableInfo eInfo, Parameter p, int index,
ClassInfo type) {
super(p.getModifiers());
this.executable = eInfo;
@@ -182,78 +174,6 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
return
((MethodInfo)executable).getMatchingMethods().stream().map(m ->
m.getParameter(index)).toList();
}
- /**
- * Finds all annotations of the specified type using enhanced search.
- *
- * <p>
- * Search order:
- * <ol>
- * <li>Annotations on the parameter itself
- * <li>Annotations on the parameter's type (after unwrapping
Value/Optional)
- * <li>For methods: Annotations on matching parameters in parent
class methods (child-to-parent order)
- * </ol>
- *
- * <h5 class='section'>Example:</h5>
- * <p class='bjava'>
- * <jc>// Find all @MyAnnotation annotations with hierarchy
search</jc>
- * ParameterInfo <jv>pi</jv> = ...;
- * Stream<AnnotationInfo<MyAnnotation>>
<jv>annotations</jv> = <jv>pi</jv>.findAnnotations(MyAnnotation.<jk>class</jk>);
- * </p>
- *
- * @param <A> The annotation type to look for.
- * @param type The annotation type to look for.
- * @return A stream of matching annotations, or an empty stream if none
found.
- */
- @SuppressWarnings("unchecked")
- public <A extends Annotation> Stream<AnnotationInfo<A>>
findAnnotations(Class<A> type) {
- var paramType =
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class);
-
- if (executable.isConstructor()) {
- // For constructors: parameter annotations, then
parameter type annotations
- return Stream.concat(
- getAnnotations(type),
- paramType.getDeclaredAnnotationInfos().stream()
- .filter(x -> x.isType(type))
- .map(x -> (AnnotationInfo<A>)x)
- );
- } else {
- // For methods: parameter annotations, parameter type
annotations, then matching methods on parent classes
- var mi = (MethodInfo)executable;
- var matchingMethods = new ArrayList<MethodInfo>();
- mi.forEachMatchingParentFirst(null,
matchingMethods::add);
-
- return Stream.of(
- getAnnotations(type),
- paramType.getDeclaredAnnotationInfos().stream()
- .filter(x -> x.isType(type))
- .map(x -> (AnnotationInfo<A>)x),
- matchingMethods.stream()
- .flatMap(m ->
m.getParameter(index).getAnnotations(type))
- ).flatMap(s -> s);
- }
- }
-
- /**
- * Finds the annotation of the specified type defined on this method
parameter.
- *
- * <p>
- * If the annotation cannot be found on the immediate method, searches
methods with the same
- * signature on the parent classes or interfaces.
- * <br>The search is performed in child-to-parent order.
- *
- * <p>
- * If still not found, searches for the annotation on the return type
of the method.
- *
- * @param <A> The annotation type to look for.
- * @param type The annotation type to look for.
- * @return
- * The annotation if found, or <jk>null</jk> if not.
- */
- @SuppressWarnings("unchecked")
- public <A extends Annotation> A getAnnotation(Class<A> type) {
- return
(A)((Optional<Annotation>)annotationCache.get(type)).orElse(null);
- }
-
/**
* Finds all annotation infos of the specified type defined on this
method parameter.
*
@@ -440,7 +360,7 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
* The <jk>true</jk> if annotation if found.
*/
public <A extends Annotation> boolean hasAnnotation(Class<A> type) {
- return nn(getAnnotation(type));
+ return nn(findAnnotationInfo(type));
}
/**
@@ -714,7 +634,7 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
*
* <p>
* <b>Note:</b> This returns the simple array of annotations directly
present on the parameter.
- * For Juneau's enhanced annotation searching (through class
hierarchies), use {@link #getAnnotation(Class)} instead.
+ * For Juneau's enhanced annotation searching (through class
hierarchies), use {@link #findAnnotationInfo(Class)} instead.
*
* <h5 class='section'>Example:</h5>
* <p class='bjava'>
@@ -814,19 +734,6 @@ public class ParameterInfo extends ElementInfo implements
Annotatable {
return (executable.getSimpleName()) + "[" + index + "]";
}
- private <A extends Annotation> A findAnnotation(Class<A> type) {
- if (executable.isConstructor()) {
- for (var a2 : getAnnotations())
- if (type.isInstance(a2))
- return type.cast(a2);
- return
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class).getAnnotation(type);
- }
- var mi = (MethodInfo)executable;
- Value<A> v = Value.empty();
- mi.forEachMatchingParentFirst(x -> true, x ->
x.forEachParameterAnnotation(index, type, y -> true, y -> v.set(y)));
- return v.orElseGet(() ->
executable.getParameter(index).getParameterType().unwrap(Value.class,
Optional.class).getAnnotation(type));
- }
-
@SuppressWarnings("unchecked")
private <A extends Annotation> List<AnnotationInfo<A>>
findAnnotationInfosInternal(Class<A> type) {
var list = new ArrayList<AnnotationInfo<A>>();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
index 54500d32b8..d8bebebf25 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/bean/RequestBeanMeta.java
@@ -81,7 +81,7 @@ public class RequestBeanMeta {
}
Builder apply(ParameterInfo mpi) {
- return
apply(mpi.getParameterType().inner()).apply(mpi.getAnnotation(Request.class));
+ return
apply(mpi.getParameterType().inner()).apply(opt(mpi.findAnnotationInfo(Request.class)).map(x
-> x.inner()).orElse(null));
}
Builder apply(Request a) {
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/FormDataArg.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/FormDataArg.java
index a9532a5e8d..00d986dc4b 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/FormDataArg.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/FormDataArg.java
@@ -98,13 +98,13 @@ public class FormDataArg implements RestOpArg {
}
}
- if (classLevelFormData == null)
- return null;
+ if (classLevelFormData == null)
+ return null;
- // Get parameter-level @FormData
- FormData paramFormData = pi.getAnnotation(FormData.class);
- if (paramFormData == null)
- paramFormData =
pi.getParameterType().getAnnotation(FormData.class);
+ // Get parameter-level @FormData
+ FormData paramFormData =
opt(pi.findAnnotationInfo(FormData.class)).map(x -> x.inner()).orElse(null);
+ if (paramFormData == null)
+ paramFormData =
pi.getParameterType().getAnnotation(FormData.class);
if (paramFormData == null) {
// No parameter-level @FormData, use class-level as-is
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/HeaderArg.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/HeaderArg.java
index 29ec914beb..2ce27d2729 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/HeaderArg.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/HeaderArg.java
@@ -140,13 +140,13 @@ public class HeaderArg implements RestOpArg {
}
}
- if (classLevelHeader == null)
- return null;
+ if (classLevelHeader == null)
+ return null;
- // Get parameter-level @Header
- Header paramHeader = pi.getAnnotation(Header.class);
- if (paramHeader == null)
- paramHeader =
pi.getParameterType().getAnnotation(Header.class);
+ // Get parameter-level @Header
+ Header paramHeader = opt(pi.findAnnotationInfo(Header.class)).map(x ->
x.inner()).orElse(null);
+ if (paramHeader == null)
+ paramHeader = pi.getParameterType().getAnnotation(Header.class);
if (paramHeader == null) {
// No parameter-level @Header, use class-level as-is
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/PathArg.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/PathArg.java
index 5732a8d3b3..7089c77e8a 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/PathArg.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/PathArg.java
@@ -97,13 +97,13 @@ public class PathArg implements RestOpArg {
}
}
- if (classLevelPath == null)
- return null;
+ if (classLevelPath == null)
+ return null;
- // Get parameter-level @Path
- Path paramPath = pi.getAnnotation(Path.class);
- if (paramPath == null)
- paramPath =
pi.getParameterType().getAnnotation(Path.class);
+ // Get parameter-level @Path
+ Path paramPath = opt(pi.findAnnotationInfo(Path.class)).map(x ->
x.inner()).orElse(null);
+ if (paramPath == null)
+ paramPath = pi.getParameterType().getAnnotation(Path.class);
if (paramPath == null) {
// No parameter-level @Path, use class-level as-is
@@ -196,13 +196,13 @@ public class PathArg implements RestOpArg {
if (nn(p))
return p;
if (nn(pathMatcher)) {
- int idx = 0;
- int i = pi.getIndex();
- MethodInfo mi = pi.getMethod();
+ int idx = 0;
+ int i = pi.getIndex();
+ MethodInfo mi = pi.getMethod();
- for (int j = 0; j < i; j++)
- if
(nn(mi.getParameter(i).getAnnotation(Path.class)))
- idx++;
+ for (int j = 0; j < i; j++)
+ if
(nn(mi.getParameter(j).findAnnotationInfo(Path.class)))
+ idx++;
String[] vars = pathMatcher.getVars();
if (vars.length <= idx)
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/QueryArg.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/QueryArg.java
index a40b7d9611..38630834e5 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/QueryArg.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/arg/QueryArg.java
@@ -98,13 +98,13 @@ public class QueryArg implements RestOpArg {
}
}
- if (classLevelQuery == null)
- return null;
+ if (classLevelQuery == null)
+ return null;
- // Get parameter-level @Query
- Query paramQuery = pi.getAnnotation(Query.class);
- if (paramQuery == null)
- paramQuery =
pi.getParameterType().getAnnotation(Query.class);
+ // Get parameter-level @Query
+ Query paramQuery = opt(pi.findAnnotationInfo(Query.class)).map(x ->
x.inner()).orElse(null);
+ if (paramQuery == null)
+ paramQuery = pi.getParameterType().getAnnotation(Query.class);
if (paramQuery == null) {
// No parameter-level @Query, use class-level as-is
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/common/reflect/ParamInfoTest.java
b/juneau-utest/src/test/java/org/apache/juneau/common/reflect/ParamInfoTest.java
index 890f7d69ba..a134195de6 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/common/reflect/ParamInfoTest.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/common/reflect/ParamInfoTest.java
@@ -237,33 +237,35 @@ class ParamInfoTest extends TestBase {
check("", annotations(cc_cc, DA.class));
}
- @Test void getAnnotation() {
- check("@CA(5)", cb_a1.getAnnotation(CA.class));
- check("@CA(5)", cb_a2.getAnnotation(CA.class));
- check("@CA(5)", cc_a1.getAnnotation(CA.class));
- check("@CA(6)", cc_a2.getAnnotation(CA.class));
+ @Test void findAnnotationInfo() {
+ check("@CA(5)", cb_a1.findAnnotationInfo(CA.class).inner());
+ check("@CA(5)", cb_a2.findAnnotationInfo(CA.class).inner());
+ check("@CA(5)", cc_a1.findAnnotationInfo(CA.class).inner());
+ check("@CA(6)", cc_a2.findAnnotationInfo(CA.class).inner());
}
- @Test void getAnnotation_notFound() {
- check(null, cb_a1.getAnnotation(DA.class));
+ @Test void findAnnotationInfo_notFound() {
+ var ai = cb_a1.findAnnotationInfo(DA.class);
+ check(null, ai == null ? null : ai.inner());
}
- @Test void getAnnotation_constructor() {
- check("@CA(9)", cc_cc.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_constructor() {
+ check("@CA(9)", cc_cc.findAnnotationInfo(CA.class).inner());
}
- @Test void getAnnotation_notFound_constructor() {
- check(null, cc_cc.getAnnotation(DA.class));
+ @Test void findAnnotationInfo_notFound_constructor() {
+ var ai = cc_cc.findAnnotationInfo(DA.class);
+ check(null, ai == null ? null : ai.inner());
}
- @Test void getAnnotation_twice() {
- check("@CA(5)", cb_a1.getAnnotation(CA.class));
- check("@CA(5)", cb_a1.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_twice() {
+ check("@CA(5)", cb_a1.findAnnotationInfo(CA.class).inner());
+ check("@CA(5)", cb_a1.findAnnotationInfo(CA.class).inner());
}
- @Test void getAnnotation_twice_constructor() {
- check("@CA(9)", cc_cc.getAnnotation(CA.class));
- check("@CA(9)", cc_cc.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_twice_constructor() {
+ check("@CA(9)", cc_cc.findAnnotationInfo(CA.class).inner());
+ check("@CA(9)", cc_cc.findAnnotationInfo(CA.class).inner());
}
@Test void hasAnnotation() {
@@ -314,13 +316,14 @@ class ParamInfoTest extends TestBase {
check("", annotations(db_a1, CA.class));
}
- @Test void getAnnotation_inherited() {
- check("@DA(0)", db_a1.getAnnotation(DA.class));
- check("@DA(5)", dc_a1.getAnnotation(DA.class));
+ @Test void findAnnotationInfo_inherited() {
+ check("@DA(0)", db_a1.findAnnotationInfo(DA.class).inner());
+ check("@DA(5)", dc_a1.findAnnotationInfo(DA.class).inner());
}
- @Test void getAnnotation_inherited_notFound() {
- check(null, db_a1.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_inherited_notFound() {
+ var ai = db_a1.findAnnotationInfo(CA.class);
+ check(null, ai == null ? null : ai.inner());
}
//-----------------------------------------------------------------------------------------------------------------
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/reflect/ParamInfoTest.java
b/juneau-utest/src/test/java/org/apache/juneau/reflect/ParamInfoTest.java
index 92728b6700..bf8b9446b3 100644
--- a/juneau-utest/src/test/java/org/apache/juneau/reflect/ParamInfoTest.java
+++ b/juneau-utest/src/test/java/org/apache/juneau/reflect/ParamInfoTest.java
@@ -236,33 +236,35 @@ class ParamInfoTest extends TestBase {
check("", annotations(cc_cc, DA.class));
}
- @Test void getAnnotation() {
- check("@CA(5)", cb_a1.getAnnotation(CA.class));
- check("@CA(5)", cb_a2.getAnnotation(CA.class));
- check("@CA(5)", cc_a1.getAnnotation(CA.class));
- check("@CA(6)", cc_a2.getAnnotation(CA.class));
+ @Test void findAnnotationInfo() {
+ check("@CA(5)", cb_a1.findAnnotationInfo(CA.class).inner());
+ check("@CA(5)", cb_a2.findAnnotationInfo(CA.class).inner());
+ check("@CA(5)", cc_a1.findAnnotationInfo(CA.class).inner());
+ check("@CA(6)", cc_a2.findAnnotationInfo(CA.class).inner());
}
- @Test void getAnnotation_notFound() {
- check(null, cb_a1.getAnnotation(DA.class));
+ @Test void findAnnotationInfo_notFound() {
+ var ai = cb_a1.findAnnotationInfo(DA.class);
+ check(null, ai == null ? null : ai.inner());
}
- @Test void getAnnotation_constructor() {
- check("@CA(9)", cc_cc.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_constructor() {
+ check("@CA(9)", cc_cc.findAnnotationInfo(CA.class).inner());
}
- @Test void getAnnotation_notFound_constructor() {
- check(null, cc_cc.getAnnotation(DA.class));
+ @Test void findAnnotationInfo_notFound_constructor() {
+ var ai = cc_cc.findAnnotationInfo(DA.class);
+ check(null, ai == null ? null : ai.inner());
}
- @Test void getAnnotation_twice() {
- check("@CA(5)", cb_a1.getAnnotation(CA.class));
- check("@CA(5)", cb_a1.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_twice() {
+ check("@CA(5)", cb_a1.findAnnotationInfo(CA.class).inner());
+ check("@CA(5)", cb_a1.findAnnotationInfo(CA.class).inner());
}
- @Test void getAnnotation_twice_constructor() {
- check("@CA(9)", cc_cc.getAnnotation(CA.class));
- check("@CA(9)", cc_cc.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_twice_constructor() {
+ check("@CA(9)", cc_cc.findAnnotationInfo(CA.class).inner());
+ check("@CA(9)", cc_cc.findAnnotationInfo(CA.class).inner());
}
@Test void hasAnnotation() {
@@ -313,13 +315,14 @@ class ParamInfoTest extends TestBase {
check("", annotations(db_a1, CA.class));
}
- @Test void getAnnotation_inherited() {
- check("@DA(0)", db_a1.getAnnotation(DA.class));
- check("@DA(5)", dc_a1.getAnnotation(DA.class));
+ @Test void findAnnotationInfo_inherited() {
+ check("@DA(0)", db_a1.findAnnotationInfo(DA.class).inner());
+ check("@DA(5)", dc_a1.findAnnotationInfo(DA.class).inner());
}
- @Test void getAnnotation_inherited_notFound() {
- check(null, db_a1.getAnnotation(CA.class));
+ @Test void findAnnotationInfo_inherited_notFound() {
+ var ai = db_a1.findAnnotationInfo(CA.class);
+ check(null, ai == null ? null : ai.inner());
}
//-----------------------------------------------------------------------------------------------------------------