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 b6a0b87 ClassInfo refactoring.
b6a0b87 is described below
commit b6a0b877c3682e6a0d2544039ff7d0036feb7dea
Author: JamesBognar <[email protected]>
AuthorDate: Sun Jan 23 16:18:44 2022 -0500
ClassInfo refactoring.
---
.../java/org/apache/juneau/BeanPropertyMeta.java | 10 +-
.../org/apache/juneau/reflect/AnnotationList.java | 8 +-
.../java/org/apache/juneau/reflect/ClassInfo.java | 119 ++++-----------------
.../juneau/reflection/AnnotationInfoTest.java | 75 -------------
.../apache/juneau/reflection/ClassInfoTest.java | 5 -
5 files changed, 28 insertions(+), 189 deletions(-)
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 febc7e0..3338e71 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
@@ -1111,23 +1111,23 @@ public final class BeanPropertyMeta {
BeanContext bc = beanContext;
if (a == null)
return l;
- getBeanMeta().getClassMeta().getInfo().appendAnnotations(l, a,
bc);
+ getBeanMeta().getClassMeta().getInfo().getAnnotations(a, bc, x
-> l.add(x));
if (field != null) {
l.addAll(bc.getAnnotations(a, field));
- ClassInfo.of(field.getType()).appendAnnotations(l, a,
bc);
+ ClassInfo.of(field.getType()).getAnnotations(a, bc, x
-> l.add(x));
}
if (getter != null) {
l.addAll(bc.getAnnotations(a, getter));
-
ClassInfo.of(getter.getReturnType()).appendAnnotations(l, a, bc);
+ ClassInfo.of(getter.getReturnType()).getAnnotations(a,
bc, x -> l.add(x));
}
if (setter != null) {
l.addAll(bc.getAnnotations(a, setter));
-
ClassInfo.of(setter.getReturnType()).appendAnnotations(l, a, bc);
+ ClassInfo.of(setter.getReturnType()).getAnnotations(a,
bc, x -> l.add(x));
}
if (extraKeys != null) {
l.addAll(bc.getAnnotations(a, extraKeys));
-
ClassInfo.of(extraKeys.getReturnType()).appendAnnotations(l, a, bc);
+
ClassInfo.of(extraKeys.getReturnType()).getAnnotations(a, bc, x -> l.add(x));
}
return l;
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/AnnotationList.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/AnnotationList.java
index 85bd50a..e3a5558 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/AnnotationList.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/AnnotationList.java
@@ -90,12 +90,12 @@ public class AnnotationList extends
ArrayList<AnnotationInfo<?>> {
/**
* Filters this list using the specified test.
*
- * @param test The test to use to filter this list.
+ * @param predicate The test to use to filter this list.
* @return A new list containing only the filtered elements.
*/
- public AnnotationList filter(Predicate<AnnotationInfo<?>> test) {
- AnnotationList al = new AnnotationList(null);
- stream().filter(test).forEach(x->al.add(x));
+ public AnnotationList filter(Predicate<AnnotationInfo<?>> predicate) {
+ AnnotationList al = new AnnotationList(predicate);
+ forEach(x -> al.add(x));
return al;
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ClassInfo.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ClassInfo.java
index a625c8b..b61d81f 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ClassInfo.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/reflect/ClassInfo.java
@@ -853,10 +853,8 @@ public final class ClassInfo {
* signature on the parent classes or interfaces.
* <br>The search is performed in child-to-parent order.
*
- * @param a
- * The annotation to search for.
- * @return
- * The annotation if found, or <jk>null</jk> if not.
+ * @param a The annotation to search for.
+ * @return The annotation if found, or <jk>null</jk> if not.
*/
public <T extends Annotation> T getLastAnnotation(Class<T> a) {
return getLastAnnotation(a, MetaProvider.DEFAULT);
@@ -866,15 +864,12 @@ public final class ClassInfo {
* Finds the annotation of the specified type defined on this class or
parent class/interface.
*
* <p>
- * If the annotation cannot be found on the immediate class, searches
methods with the same
- * signature on the parent classes or interfaces.
- * <br>The search is performed in child-to-parent order.
+ * If the annotation cannot be found on the immediate class, searches
methods with the same signature on the parent classes or interfaces. <br>
+ * The search is performed in child-to-parent order.
*
- * @param a
- * The annotation to search for.
+ * @param a The annotation to search for.
* @param mp The meta provider for looking up annotations on reflection
objects (classes, methods, fields, constructors).
- * @return
- * The annotation if found, or <jk>null</jk> if not.
+ * @return The annotation if found, or <jk>null</jk> if not.
*/
public <T extends Annotation> T getLastAnnotation(Class<T> a,
MetaProvider mp) {
return findAnnotation(a, mp);
@@ -883,10 +878,8 @@ public final class ClassInfo {
/**
* Returns <jk>true</jk> if this class has the specified annotation.
*
- * @param a
- * The annotation to search for.
- * @return
- * The <jk>true</jk> if annotation if found.
+ * @param a The annotation to search for.
+ * @return The <jk>true</jk> if annotation if found.
*/
public boolean hasAnnotation(Class<? extends Annotation> a) {
return getLastAnnotation(a) != null;
@@ -896,8 +889,7 @@ public final class ClassInfo {
* Returns the specified annotation only if it's been declared on this
class.
*
* <p>
- * More efficient than calling {@link Class#getAnnotation(Class)} since
it doesn't recursively look for the class
- * up the parent chain.
+ * More efficient than calling {@link Class#getAnnotation(Class)} since
it doesn't recursively look for the class up the parent chain.
*
* @param <T> The annotation class type.
* @param a The annotation class.
@@ -985,20 +977,6 @@ public final class ClassInfo {
}
/**
- * Same as getAnnotations(Class) except returns the annotations with
the accompanying class.
- *
- * <p>
- * Results are ordered parent-to-child.
- *
- * @param <T> The annotation class type.
- * @param a The annotation class type.
- * @return The found matches, or an empty list if annotation was not
found.
- */
- public <T extends Annotation> List<AnnotationInfo<T>>
getAnnotationInfos(Class<T> a) {
- return appendAnnotationInfos(new ArrayList<>(), a);
- }
-
- /**
* Constructs an {@link AnnotationList} of all annotations found on
this class.
*
* <p>
@@ -1045,7 +1023,9 @@ public final class ClassInfo {
* @return A new {@link AnnotationList} object on every call.
*/
public AnnotationList getAnnotationList(Predicate<AnnotationInfo<?>>
filter) {
- return appendAnnotationList(new AnnotationList(filter));
+ AnnotationList l = new AnnotationList(filter);
+ getAnnotationInfos(x -> l.add(x));
+ return l;
}
/**
@@ -1070,31 +1050,7 @@ public final class ClassInfo {
}
/**
- * Finds and appends the specified annotation on the specified class
and superclasses/interfaces to the specified
- * list.
- *
- * <p>
- * Annotations are appended in the following orders:
- * <ol>
- * <li>On the package of this class.
- * <li>On interfaces ordered child-to-parent.
- * <li>On parent classes ordered child-to-parent.
- * <li>On this class.
- * </ol>
- *
- * @param l The list of annotations.
- * @param a The annotation to search for.
- * @param mp The meta provider for looking up annotations on reflection
objects (classes, methods, fields, constructors).
- * @return The same list.
- */
- public <T extends Annotation> List<T> appendAnnotations(List<T> l,
Class<T> a, MetaProvider mp) {
- getAnnotations(a, mp, x -> l.add(x));
- return l;
- }
-
- /**
- * Finds and consumes the specified annotation on the specified class
and superclasses/interfaces to the specified
- * consumer.
+ * Finds and consumes the specified annotation on the specified class
and superclasses/interfaces to the specified consumer.
*
* <p>
* Annotations are appended in the following orders:
@@ -1143,34 +1099,6 @@ public final class ClassInfo {
}
/**
- * Finds and appends the specified annotation on the specified class
and superclasses/interfaces to the specified
- * list.
- *
- * <p>
- * Annotations are appended in the following orders:
- * <ol>
- * <li>On the package of this class.
- * <li>On interfaces ordered child-to-parent.
- * <li>On parent classes ordered child-to-parent.
- * <li>On this class.
- * </ol>
- *
- * @param l The list of annotations.
- * @param a The annotation to search for.
- * @return The same list.
- */
- public <T extends Annotation> List<AnnotationInfo<T>>
appendAnnotationInfos(List<AnnotationInfo<T>> l, Class<T> a) {
- addIfNotNull(l, getPackageAnnotationInfo(a));
- ClassInfo[] interfaces = _getInterfaces();
- for (int i = interfaces.length-1; i >= 0; i--)
- addIfNotNull(l,
interfaces[i].getDeclaredAnnotationInfo(a));
- ClassInfo[] parents = _getParents();
- for (int i = parents.length-1; i >= 0; i--)
- addIfNotNull(l,
parents[i].getDeclaredAnnotationInfo(a));
- return l;
- }
-
- /**
* Searches up the parent hierarchy of this class for the first
annotation in the list it finds.
*
* @param mp Metadata provider.
@@ -1195,24 +1123,22 @@ public final class ClassInfo {
return ci == null ? null : ci.getAnyLastAnnotation(mp,
annotations);
}
-
- AnnotationList appendAnnotationList(AnnotationList m) {
+ void getAnnotationInfos(Consumer<AnnotationInfo<?>> consumer) {
Package p = c.getPackage();
if (p != null)
for (Annotation a : p.getDeclaredAnnotations())
for (Annotation a2 : splitRepeated(a))
- m.add(AnnotationInfo.of(p, a2));
+ consumer.accept(AnnotationInfo.of(p,
a2));
ClassInfo[] interfaces = _getInterfaces();
for (int i = interfaces.length-1; i >= 0; i--)
for (Annotation a :
interfaces[i].c.getDeclaredAnnotations())
for (Annotation a2 : splitRepeated(a))
- m.add(AnnotationInfo.of(interfaces[i],
a2));
+
consumer.accept(AnnotationInfo.of(interfaces[i], a2));
ClassInfo[] parents = _getParents();
for (int i = parents.length-1; i >= 0; i--)
for (Annotation a :
parents[i].c.getDeclaredAnnotations())
for (Annotation a2 : splitRepeated(a))
- m.add(AnnotationInfo.of(parents[i],
a2));
- return m;
+
consumer.accept(AnnotationInfo.of(parents[i], a2));
}
/**
@@ -1925,7 +1851,6 @@ public final class ClassInfo {
return false;
}
-
/**
* Same as {@link #isParentOfFuzzyPrimitives(Class)} but takes in a
{@link ClassInfo}.
*
@@ -2169,8 +2094,8 @@ public final class ClassInfo {
if (rt.isArray()) {
ClassInfo rct = rt.getComponentType();
if
(rct.hasAnnotation(Repeatable.class)) {
- Repeatable r =
rct.getLastAnnotation(Repeatable.class) ;
- b = r.value().equals(c);
+ Repeatable r =
rct.getLastAnnotation(Repeatable.class);
+ b = r.value().equals(c);
}
}
}
@@ -2339,12 +2264,6 @@ public final class ClassInfo {
}
}
- private static <T> List<T> addIfNotNull(List<T> l, T o) {
- if (o != null)
- l.add(o);
- return l;
- }
-
//-----------------------------------------------------------------------------------------------------------------
// Other
//-----------------------------------------------------------------------------------------------------------------
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/reflection/AnnotationInfoTest.java
b/juneau-utest/src/test/java/org/apache/juneau/reflection/AnnotationInfoTest.java
index 06ec65d..4f99e98 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/reflection/AnnotationInfoTest.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/reflection/AnnotationInfoTest.java
@@ -14,13 +14,11 @@ package org.apache.juneau.reflection;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
-import static org.junit.Assert.*;
import static org.junit.runners.MethodSorters.*;
import static org.apache.juneau.assertions.Assertions.*;
import org.apache.juneau.annotation.*;
import java.lang.annotation.*;
-import java.util.function.*;
import org.apache.juneau.reflect.*;
import org.junit.*;
@@ -28,79 +26,6 @@ import org.junit.*;
@FixMethodOrder(NAME_ASCENDING)
public class AnnotationInfoTest {
- private static void check(String expected, Object o) {
- assertEquals(expected, TO_STRING.apply(o));
- }
-
- private static final Function<Object,String> TO_STRING = new
Function<Object,String>() {
- @Override
- public String apply(Object t) {
- if (t instanceof A)
- return "@A(" + ((A)t).value() + ")";
- if (t instanceof ClassInfo)
- return ((ClassInfo)t).getSimpleName();
- return t.toString();
- }
- };
-
- private static ClassInfo of(Class<?> c) {
- try {
- return ClassInfo.of(c);
- } catch (SecurityException e) {
- fail(e.getLocalizedMessage());
- }
- return null;
- }
-
-
//-----------------------------------------------------------------------------------------------------------------
- // Instantiation.
-
//-----------------------------------------------------------------------------------------------------------------
-
- @Documented
- @Target(TYPE)
- @Retention(RUNTIME)
- @Inherited
- public static @interface A {
- int value();
- }
-
- @A(1)
- static class B {}
- static ClassInfo b = of(B.class);
-
- @Test
- public void b01_getClassOn() {
- check("B", b.getAnnotationInfos(A.class).get(0).getClassOn());
- }
-
- @Test
- public void b02_getAnnotation() {
- check("@A(1)",
b.getAnnotationInfos(A.class).get(0).getAnnotation());
- }
-
-
//-----------------------------------------------------------------------------------------------------------------
- // Get value.
-
//-----------------------------------------------------------------------------------------------------------------
-
- @Target(TYPE)
- @Retention(RUNTIME)
- public static @interface C {
- String foo() default "x";
- }
-
- @C(foo="bar")
- public static class C1 {}
-
- @Test
- public void c01_getValue() {
- ClassInfo c1 = ClassInfo.of(C1.class);
- AnnotationInfo<?> ai = c1.getAnnotationInfos(C.class).get(0);
- assertString(ai.getValue(String.class, "foo")).is("bar");
- assertOptional(ai.getValue(Integer.class, "foo")).isNull();
- assertOptional(ai.getValue(String.class, "bar")).isNull();
- }
-
-
//-----------------------------------------------------------------------------------------------------------------
// Is in group.
//-----------------------------------------------------------------------------------------------------------------
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/reflection/ClassInfoTest.java
b/juneau-utest/src/test/java/org/apache/juneau/reflection/ClassInfoTest.java
index 4ac4b4d..69e3bc1 100644
--- a/juneau-utest/src/test/java/org/apache/juneau/reflection/ClassInfoTest.java
+++ b/juneau-utest/src/test/java/org/apache/juneau/reflection/ClassInfoTest.java
@@ -684,11 +684,6 @@ public class ClassInfoTest {
}
@Test
- public void getAnnotationInfosParentFirst() {
- check("@A(2),@A(1),@A(3),@A(5),@A(6),@A(7)",
g3.getAnnotationInfos(A.class));
- }
-
- @Test
public void getPackageAnnotation() {
check("@PA(10)", g3.getPackageAnnotation(PA.class));
}