@SuppressWarnings("PMD.EmptyCatchBlock") public static Method findAnnotatedMethod(Class<?> configClass, Class<? extends Annotation> annotation, String methodName, Class<?>... paramTypes) { try { Method method = configClass.getDeclaredMethod(methodName, paramTypes); if (method != null && method.isAnnotationPresent(annotation)) { return method; } } catch (NoSuchMethodException e) { // ignore } if (configClass.getSuperclass() != null) { Method managedMethod = findAnnotatedMethod( configClass.getSuperclass(), annotation, methodName, paramTypes); if (managedMethod != null) { return managedMethod; } } for (Class<?> iface : configClass.getInterfaces()) { Method managedMethod = findAnnotatedMethod(iface, annotation, methodName, paramTypes); if (managedMethod != null) { return managedMethod; } } return null; }
@SuppressWarnings("PMD.EmptyCatchBlock") public static Method findAnnotatedMethod(Class<?> configClass, Class<? extends Annotation> annotation, String methodName, Class<?>... paramTypes) { try { Method method = configClass.getDeclaredMethod(methodName, paramTypes); if (method != null && method.isAnnotationPresent(annotation)) { return method; } } catch (NoSuchMethodException e) { // ignore } if (configClass.getSuperclass() != null) { Method managedMethod = findAnnotatedMethod( configClass.getSuperclass(), annotation, methodName, paramTypes); if (managedMethod != null) { return managedMethod; } } for (Class<?> iface : configClass.getInterfaces()) { Method managedMethod = findAnnotatedMethod(iface, annotation, methodName, paramTypes); if (managedMethod != null) { return managedMethod; } } return null; }
/** * Find methods that are tagged with a given annotation somewhere in the hierarchy */ public static Collection<Method> findAnnotatedMethods(Class<?> type, Class<? extends Annotation> annotation) { List<Method> result = new ArrayList<>(); // gather all publicly available methods // this returns everything, even if it's declared in a parent for (Method method : type.getMethods()) { // skip methods that are used internally by the vm for implementing covariance, etc if (method.isSynthetic() || method.isBridge() || isStatic(method.getModifiers())) { continue; } // look for annotations recursively in super-classes or interfaces Method managedMethod = findAnnotatedMethod( type, annotation, method.getName(), method.getParameterTypes()); if (managedMethod != null) { result.add(managedMethod); } } return result; }
/** * Find methods that are tagged with a given annotation somewhere in the hierarchy */ public static Collection<Method> findAnnotatedMethods(Class<?> type, Class<? extends Annotation> annotation) { List<Method> result = new ArrayList<>(); // gather all publicly available methods // this returns everything, even if it's declared in a parent for (Method method : type.getMethods()) { // skip methods that are used internally by the vm for implementing covariance, etc if (method.isSynthetic() || method.isBridge() || isStatic(method.getModifiers())) { continue; } // look for annotations recursively in super-classes or interfaces Method managedMethod = findAnnotatedMethod( type, annotation, method.getName(), method.getParameterTypes()); if (managedMethod != null) { result.add(managedMethod); } } return result; }