Package: ReflectionUtils$MethodProcessorSupport
ReflectionUtils$MethodProcessorSupport
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ReflectionUtils.MethodProcessorSupport() |
|
|
|
|
|
||||||||||||||||||||
filter(Class) |
|
|
|
|
|
||||||||||||||||||||
process(Method) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.messagebus.spi;
28:
29: import java.lang.annotation.Annotation;
30: import java.lang.reflect.Method;
31: import javax.annotation.Nonnull;
32: import java.util.ArrayList;
33: import java.util.Collections;
34: import java.util.List;
35:
36: /***********************************************************************************************************************
37: *
38: * @author Fabrizio Giudici
39: *
40: **********************************************************************************************************************/
41: public class ReflectionUtils
42: {
43: /*******************************************************************************************************************
44: *
45: *
46: *
47: ******************************************************************************************************************/
48: public static interface MethodProcessor
49: {
50: enum FilterResult { ACCEPT, IGNORE }
51:
52: public FilterResult filter (@Nonnull Class<?> clazz);
53:
54: public void process (@Nonnull Method method);
55: }
56:
57: /*******************************************************************************************************************
58: *
59: *
60: *
61: ******************************************************************************************************************/
62: public static class MethodProcessorSupport implements MethodProcessor
63: {
64: @Override @Nonnull
65: public FilterResult filter (@Nonnull final Class<?> clazz)
66: {
67: return FilterResult.ACCEPT;
68: }
69:
70: @Override
71: public void process (@Nonnull final Method method)
72: {
73: }
74: }
75:
76: /*******************************************************************************************************************
77: *
78: * Navigates the hierarchy of the given object, top down, and applies the {@link MethodProcessor} to all the
79: * methods of each class, if not filtered out by the processor itself.
80: *
81: * @param object the object at the bottom of the hierarchy
82: * @param processor the processor
83: *
84: ******************************************************************************************************************/
85: public static void forEachMethodInTopDownHierarchy (@Nonnull final Object object,
86: @Nonnull final MethodProcessor processor)
87: {
88: for (final Class<?> clazz : getClassHierarchy(object.getClass()))
89: {
90: for (final Method method : clazz.getDeclaredMethods())
91: {
92: processor.process(method);
93: }
94: }
95: }
96:
97: /*******************************************************************************************************************
98: *
99: * Navigates the hierarchy of the given object, bottom up, and applies the {@link MethodProcessor} to all the
100: * methods of each class, if not filtered out by the processor itself.
101: *
102: * @param object the object at the bottom of the hierarchy
103: * @param processor the processor
104: *
105: ******************************************************************************************************************/
106: public static void forEachMethodInBottomUpHierarchy (@Nonnull final Object object,
107: @Nonnull final MethodProcessor processor)
108: {
109: final List<Class<?>> hierarchy = getClassHierarchy(object.getClass());
110: Collections.reverse(hierarchy);
111:
112: for (final Class<?> clazz : hierarchy)
113: {
114: if (processor.filter(clazz) == MethodProcessor.FilterResult.ACCEPT)
115: {
116: for (final Method method : clazz.getDeclaredMethods())
117: {
118: processor.process(method);
119: }
120: }
121: }
122: }
123:
124: /*******************************************************************************************************************
125: *
126: * Returns the hierarchy of the given class, top down.
127: *
128: * @param clazz the clazz at the bottom of the hierarchy
129: * @return the hierarchy
130: *
131: ******************************************************************************************************************/
132: @Nonnull
133: private static List<Class<?>> getClassHierarchy (@Nonnull final Class<?> clazz)
134: {
135: final List<Class<?>> hierarchy = new ArrayList<>();
136:
137: for (Class<?> ancestor = clazz; ancestor != null; ancestor = ancestor.getSuperclass())
138: {
139: hierarchy.add(0, ancestor);
140: }
141:
142: return hierarchy;
143: }
144:
145: /*******************************************************************************************************************
146: *
147: * Checks whether an array of annotations contains an annotation of the given type.
148: *
149: * @param annotations the annotations to check
150: * @param annotationClass the type of the required annotation
151: * @return true if the annotation is found
152: *
153: ******************************************************************************************************************/
154: public static boolean containsAnnotation (@Nonnull final Annotation[] annotations,
155: @Nonnull final Class<?> annotationClass)
156: {
157: for (final Annotation annotation : annotations)
158: {
159: if (annotationClass.isAssignableFrom(annotation.getClass()))
160: {
161: return true;
162: }
163: }
164:
165: return false;
166: }
167: }