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 fafeef3 ClassInfo refactoring.
fafeef3 is described below
commit fafeef37e185d28387eae25b5e8e11246fffaeaa
Author: JamesBognar <[email protected]>
AuthorDate: Mon Jan 31 10:48:52 2022 -0500
ClassInfo refactoring.
---
.../java/org/apache/juneau/reflect/MethodInfo.java | 42 +++++---
.../java/org/apache/juneau/reflect/ParamInfo.java | 109 ++++++++-------------
.../java/org/apache/juneau/rest/RestContext.java | 9 +-
.../org/apache/juneau/reflect/MethodInfoTest.java | 4 +-
4 files changed, 73 insertions(+), 91 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/MethodInfo.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/MethodInfo.java
index 353fae1..3f1ae46 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/MethodInfo.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/MethodInfo.java
@@ -142,25 +142,38 @@ public final class MethodInfo extends ExecutableInfo
implements Comparable<Metho
}
/**
- * Finds all declared methods with the same name and arguments on all
superclasses and interfaces.
+ * Consumes all matching declared methods with the same name and
arguments on all superclasses and interfaces.
*
- * @return
- * All matching methods including this method itself.
- * <br>Methods are ordered from child-to-parent order.
+ * <p>
+ * Methods are accessed from child-to-parent order.
+ *
+ * @param predicate The predicate.
+ * @param consumer The consumer.
+ * @return This object.
*/
- public List<Method> getMatching() {
- return new UnmodifiableArray<>(_getMatching());
+ public MethodInfo getMatching(Predicate<Method> predicate,
Consumer<Method> consumer) {
+ for (Method m : _getMatching())
+ if (predicate.test(m))
+ consumer.accept(m);
+ return this;
}
/**
- * Convenience method for retrieving values in {@link #getMatching()}
in parent-to-child order.
+ * Consumes all matching declared methods with the same name and
arguments on all superclasses and interfaces.
*
- * @return
- * All matching methods including this method itself.
- * <br>Methods are ordered from parent-to-child order.
+ * <p>
+ * Methods are accessed from parent-to-child order.
+ *
+ * @param predicate The predicate.
+ * @param consumer The consumer.
+ * @return This object.
*/
- public List<Method> getMatchingParentFirst() {
- return new UnmodifiableArray<>(_getMatching(), true);
+ public MethodInfo getMatchingParentFirst(Predicate<Method> predicate,
Consumer<Method> consumer) {
+ Method[] m = _getMatching();
+ for (int i = m.length-1; i >= 0; i--)
+ if (predicate.test(m[i]))
+ consumer.accept(m[i]);
+ return this;
}
private static List<Method> findMatching(List<Method> l, Method m,
Class<?> c) {
@@ -316,8 +329,9 @@ public final class MethodInfo extends ExecutableInfo
implements Comparable<Metho
@SuppressWarnings("unchecked")
public <A extends Annotation> MethodInfo
getAnnotations(AnnotationProvider annotationProvider, Class<A> type,
Predicate<A> predicate, Consumer<A> consumer) {
declaringClass.getAnnotations(annotationProvider, type,
predicate, consumer);
- for (Method m2 : getMatchingParentFirst())
- for (Annotation a2 : m2.getDeclaredAnnotations())
+ Method[] m = _getMatching();
+ for (int i = m.length-1; i >= 0; i--)
+ for (Annotation a2 : m[i].getDeclaredAnnotations())
if (type.isInstance(a2) &&
predicate.test((A)a2))
consumer.accept((A)a2);
getReturnType().unwrap(Value.class,Optional.class).getAnnotations(annotationProvider,
type, predicate, consumer);
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ParamInfo.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ParamInfo.java
index eee8b3b..7d80912 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ParamInfo.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ParamInfo.java
@@ -173,11 +173,14 @@ public final class ParamInfo {
return
eInfo.getParamType(index).unwrap(Value.class,Optional.class).getAnnotation(a);
}
MethodInfo mi = (MethodInfo)eInfo;
- for (Method m2 : mi.getMatching())
- for (Annotation a2 :
m2.getParameterAnnotations()[index])
+ Value<T> v = Value.empty();
+ mi.getMatchingParentFirst(x -> true, x -> {
+ for (Annotation a2 :
x.getParameterAnnotations()[index])
if (a.isInstance(a2))
- return (T)a2;
- return
eInfo.getParamType(index).unwrap(Value.class,Optional.class).getAnnotation(a);
+ v.set((T)a2);
+
+ });
+ return v.orElseGet(() ->
eInfo.getParamType(index).unwrap(Value.class,Optional.class).getAnnotation(a));
}
/**
@@ -195,7 +198,7 @@ public final class ParamInfo {
* @return This object.
*/
public <A extends Annotation> ParamInfo getAnnotations(Class<A> type,
Predicate<A> predicate, Consumer<A> consumer) {
- return getAnnotations(AnnotationProvider.DEFAULT, type, true,
predicate, consumer);
+ return getAnnotations(AnnotationProvider.DEFAULT, type,
predicate, consumer);
}
/**
@@ -211,88 +214,54 @@ public final class ParamInfo {
* @param predicate The predicate.
* @return A list of all matching annotations found or an empty list if
none found.
*/
- public <A extends Annotation> A getAnnotation(Class<A> type,
Predicate<A> predicate) {
- return getAnnotation(type, true, predicate);
- }
-
@SuppressWarnings("unchecked")
- private <A extends Annotation> ParamInfo
getAnnotations(AnnotationProvider ap, Class<A> a, boolean parentFirst,
Predicate<A> predicate, Consumer<A> consumer) {
+ public <A extends Annotation> A getAnnotation(Class<A> type,
Predicate<A> predicate) {
if (eInfo.isConstructor) {
ClassInfo ci =
eInfo.getParamType(index).unwrap(Value.class,Optional.class);
- Annotation[] annotations =
eInfo.getParameterAnnotations(index);
- if (parentFirst) {
- ci.getAnnotations(ap, a, predicate, consumer);
- for (Annotation a2 : annotations)
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- consumer.accept((A)a2);
- } else {
- for (Annotation a2 : annotations)
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- consumer.accept((A)a2);
- ci.getAnnotations(ap, a, predicate, consumer);
- }
+ A o = ci.getAnnotation(type, predicate);
+ if (o != null)
+ return o;
+ for (Annotation a2 :
eInfo.getParameterAnnotations(index))
+ if (type.isInstance(a2) &&
predicate.test((A)a2))
+ return (A)a2;
} else {
MethodInfo mi = (MethodInfo)eInfo;
ClassInfo ci =
eInfo.getParamType(index).unwrap(Value.class,Optional.class);
- if (parentFirst) {
- ci.getAnnotations(ap, a, predicate, consumer);
- for (Method m2 : mi.getMatchingParentFirst())
- for (Annotation a2 :
m2.getParameterAnnotations()[index])
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- consumer.accept((A)a2);
- } else {
- for (Method m2 : mi.getMatching())
- for (Annotation a2 :
m2.getParameterAnnotations()[index])
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- consumer.accept((A)a2);
- ci.getAnnotations(ap, a, predicate, consumer);
- }
+ A o = ci.getAnnotation(type, predicate);
+ if (o != null)
+ return o;
+ Value<A> v = Value.empty();
+ mi.getMatchingParentFirst(x -> true, x -> {
+ for (Annotation a2 :
x.getParameterAnnotations()[index])
+ if (type.isInstance(a2) &&
predicate.test((A)a2))
+ v.set((A)a2);
+ });
+ return v.orElse(null);
}
- return this;
+ return null;
}
@SuppressWarnings("unchecked")
- private <A extends Annotation> A getAnnotation(Class<A> a, boolean
parentFirst, Predicate<A> predicate) {
+ private <A extends Annotation> ParamInfo
getAnnotations(AnnotationProvider ap, Class<A> a, Predicate<A> predicate,
Consumer<A> consumer) {
if (eInfo.isConstructor) {
ClassInfo ci =
eInfo.getParamType(index).unwrap(Value.class,Optional.class);
Annotation[] annotations =
eInfo.getParameterAnnotations(index);
- if (parentFirst) {
- A o = ci.getAnnotation(a, predicate);
- if (o != null)
- return o;
- for (Annotation a2 : annotations)
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- return (A)a2;
- } else {
- for (Annotation a2 : annotations)
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- return (A)a2;
- A o = ci.getAnnotation(a, predicate);
- if (o != null)
- return o;
- }
+ ci.getAnnotations(ap, a, predicate, consumer);
+ for (Annotation a2 : annotations)
+ if (a.isInstance(a2) && predicate.test((A)a2))
+ consumer.accept((A)a2);
} else {
MethodInfo mi = (MethodInfo)eInfo;
ClassInfo ci =
eInfo.getParamType(index).unwrap(Value.class,Optional.class);
- if (parentFirst) {
- A o = ci.getAnnotation(a, predicate);
- if (o != null)
- return o;
- for (Method m2 : mi.getMatchingParentFirst())
- for (Annotation a2 :
m2.getParameterAnnotations()[index])
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- return (A)a2;
- } else {
- for (Method m2 : mi.getMatching())
- for (Annotation a2 :
m2.getParameterAnnotations()[index])
- if (a.isInstance(a2) &&
predicate.test((A)a2))
- return (A)a2;
- A o = ci.getAnnotation(a, predicate);
- if (o != null)
- return o;
- }
+ ci.getAnnotations(ap, a, predicate, consumer);
+ mi.getMatchingParentFirst(x -> true, x -> {
+ for (Annotation a2 :
x.getParameterAnnotations()[index])
+ if (a.isInstance(a2) &&
predicate.test((A)a2))
+ consumer.accept((A)a2);
+
+ });
}
- return null;
+ return this;
}
private synchronized Map<Class<?>,Optional<Annotation>> annotationMap()
{
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 3244463..b2a6c09 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
@@ -4050,13 +4050,10 @@ public class RestContext extends Context {
// Also include methods on @Rest-annotated
interfaces.
if (al.size() == 0) {
- for (Method mi2 : mi.getMatching()) {
- Class<?> ci2 =
mi2.getDeclaringClass();
- if (ci2.isInterface() &&
ci2.getAnnotation(Rest.class) != null) {
-
al.add(AnnotationInfo.of(MethodInfo.of(mi2), RestOpAnnotation.DEFAULT));
- }
- }
+ Predicate<Method>
isRestAnnotatedInterface = x -> x.getDeclaringClass().isInterface() &&
x.getDeclaringClass().getAnnotation(Rest.class) != null;
+
mi.getMatching(isRestAnnotatedInterface, x ->
al.add(AnnotationInfo.of(MethodInfo.of(x), RestOpAnnotation.DEFAULT)));
}
+
if (al.size() > 0) {
try {
if (mi.isNotPublic())
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/reflect/MethodInfoTest.java
b/juneau-utest/src/test/java/org/apache/juneau/reflect/MethodInfoTest.java
index 2bfef55..5cd0651 100644
--- a/juneau-utest/src/test/java/org/apache/juneau/reflect/MethodInfoTest.java
+++ b/juneau-utest/src/test/java/org/apache/juneau/reflect/MethodInfoTest.java
@@ -163,7 +163,9 @@ public class MethodInfoTest {
@Test
public void findMatchingMethods() throws Exception {
MethodInfo mi = MethodInfo.of(B3.class.getMethod("foo",
int.class));
- check("B3.foo(int),B2.foo(int),B1.foo(int)", mi.getMatching());
+ List<Method> l = new ArrayList<>();
+ mi.getMatching(x -> true, x -> l.add(x));
+ check("B3.foo(int),B2.foo(int),B1.foo(int)", l);
}