/** * Returns an annotation by type from a method. * * @param method is the method to find * @param annotationClass is the type of annotation * @param <A> is the type of annotation * @return annotation if it is found */ public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationClass) { A annotation = method.getAnnotation(annotationClass); if (annotation == null) { for (Annotation metaAnnotation : method.getAnnotations()) { annotation = metaAnnotation.annotationType().getAnnotation(annotationClass); if (annotation != null) { return annotation; } } Method superclassMethod = getOverriddenMethod(method); if (superclassMethod != null) { annotation = getAnnotation(superclassMethod, annotationClass); } } return annotation; }
public static Annotation[][] getParameterAnnotations(Method method) { Annotation[][] methodAnnotations = method.getParameterAnnotations(); Method overriddenmethod = getOverriddenMethod(method); if (overriddenmethod != null) { Annotation[][] overriddenAnnotations = overriddenmethod .getParameterAnnotations(); for (int i = 0; i < methodAnnotations.length; i++) { List<Type> types = new ArrayList<>(); for (int j = 0; j < methodAnnotations[i].length; j++) { types.add(methodAnnotations[i][j].annotationType()); } for (int j = 0; j < overriddenAnnotations[i].length; j++) { if (!types.contains(overriddenAnnotations[i][j] .annotationType())) { methodAnnotations[i] = ArrayUtils.add( methodAnnotations[i], overriddenAnnotations[i][j]); } } } } return methodAnnotations; }
public static String extractOperationMethod(Method method, Iterator<OpenAPIExtension> chain) { if (method.getAnnotation(javax.ws.rs.GET.class) != null) { return GET_METHOD; } else if (method.getAnnotation(javax.ws.rs.PUT.class) != null) { return PUT_METHOD; } else if (method.getAnnotation(javax.ws.rs.POST.class) != null) { return POST_METHOD; } else if (method.getAnnotation(javax.ws.rs.DELETE.class) != null) { return DELETE_METHOD; } else if (method.getAnnotation(javax.ws.rs.OPTIONS.class) != null) { return OPTIONS_METHOD; } else if (method.getAnnotation(javax.ws.rs.HEAD.class) != null) { return HEAD_METHOD; } else if (method.getAnnotation(DELETE.class) != null) { return DELETE_METHOD; } else if (method.getAnnotation(HttpMethod.class) != null) { HttpMethod httpMethod = method.getAnnotation(HttpMethod.class); return httpMethod.value().toLowerCase(); } else if (!StringUtils.isEmpty(getHttpMethodFromCustomAnnotations(method))) { return getHttpMethodFromCustomAnnotations(method); } else if ((ReflectionUtils.getOverriddenMethod(method)) != null) { return extractOperationMethod(ReflectionUtils.getOverriddenMethod(method), chain); } else if (chain != null && chain.hasNext()) { return chain.next().extractOperationMethod(method, chain); } else { return null; } }
@Test public void getOverriddenMethodTest() throws NoSuchMethodException { final Method method1 = ReflectionUtils.getOverriddenMethod( Child.class.getMethod("parametrizedMethod1", Integer.class)); Assert.assertNotNull(method1); Assert.assertEquals(method1.getParameterTypes()[0], Number.class); final Method method2 = ReflectionUtils.getOverriddenMethod( Child.class.getMethod("parametrizedMethod2", Long.class)); Assert.assertNotNull(method2); Assert.assertEquals(method2.getParameterTypes()[0], Number.class); final Method method3 = ReflectionUtils.getOverriddenMethod( Child.class.getMethod("parametrizedMethod3", Long.class)); Assert.assertNull(method3); Assert.assertNull(ReflectionUtils.getOverriddenMethod(Object.class.getMethod("equals", Object.class))); }
/** * Returns a List of repeatable annotations by type from a method. * * @param method is the method to find * @param annotationClass is the type of annotation * @param <A> is the type of annotation * @return List of repeatable annotations if it is found */ public static <A extends Annotation> List<A> getRepeatableAnnotations(Method method, Class<A> annotationClass) { A[] annotations = method.getAnnotationsByType(annotationClass); if (annotations == null || annotations.length == 0) { for (Annotation metaAnnotation : method.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return Arrays.asList(annotations); } } Method superclassMethod = getOverriddenMethod(method); if (superclassMethod != null) { return getRepeatableAnnotations(superclassMethod, annotationClass); } } if (annotations == null) { return null; } return Arrays.asList(annotations); }
public static Annotation[][] getParameterAnnotations(Method method) { Annotation[][] methodAnnotations = method.getParameterAnnotations(); Method overriddenmethod = getOverriddenMethod(method); if (overriddenmethod != null) { Annotation[][] overriddenAnnotations = overriddenmethod .getParameterAnnotations(); for (int i = 0; i < methodAnnotations.length; i++) { List<Type> types = new ArrayList<>(); for (int j = 0; j < methodAnnotations[i].length; j++) { types.add(methodAnnotations[i][j].annotationType()); } for (int j = 0; j < overriddenAnnotations[i].length; j++) { if (!types.contains(overriddenAnnotations[i][j] .annotationType())) { methodAnnotations[i] = ArrayUtils.add( methodAnnotations[i], overriddenAnnotations[i][j]); } } } } return methodAnnotations; }
/** * Returns an annotation by type from a method. * * @param method is the method to find * @param annotationClass is the type of annotation * @param <A> is the type of annotation * @return annotation if it is found */ public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationClass) { A annotation = method.getAnnotation(annotationClass); if (annotation == null) { for (Annotation metaAnnotation : method.getAnnotations()) { annotation = metaAnnotation.annotationType().getAnnotation(annotationClass); if (annotation != null) { return annotation; } } Method superclassMethod = getOverriddenMethod(method); if (superclassMethod != null) { annotation = getAnnotation(superclassMethod, annotationClass); } } return annotation; }
public static String extractOperationMethod(Method method, Iterator<OpenAPIExtension> chain) { if (method.getAnnotation(javax.ws.rs.GET.class) != null) { return GET_METHOD; } else if (method.getAnnotation(javax.ws.rs.PUT.class) != null) { return PUT_METHOD; } else if (method.getAnnotation(javax.ws.rs.POST.class) != null) { return POST_METHOD; } else if (method.getAnnotation(javax.ws.rs.DELETE.class) != null) { return DELETE_METHOD; } else if (method.getAnnotation(javax.ws.rs.OPTIONS.class) != null) { return OPTIONS_METHOD; } else if (method.getAnnotation(javax.ws.rs.HEAD.class) != null) { return HEAD_METHOD; } else if (method.getAnnotation(DELETE.class) != null) { return DELETE_METHOD; } else if (method.getAnnotation(HttpMethod.class) != null) { HttpMethod httpMethod = method.getAnnotation(HttpMethod.class); return httpMethod.value().toLowerCase(); } else if (!StringUtils.isEmpty(getHttpMethodFromCustomAnnotations(method))) { return getHttpMethodFromCustomAnnotations(method); } else if ((ReflectionUtils.getOverriddenMethod(method)) != null) { return extractOperationMethod(ReflectionUtils.getOverriddenMethod(method), chain); } else if (chain != null && chain.hasNext()) { return chain.next().extractOperationMethod(method, chain); } else { return null; } }
/** * Returns a List of repeatable annotations by type from a method. * * @param method is the method to find * @param annotationClass is the type of annotation * @param <A> is the type of annotation * @return List of repeatable annotations if it is found */ public static <A extends Annotation> List<A> getRepeatableAnnotations(Method method, Class<A> annotationClass) { A[] annotations = method.getAnnotationsByType(annotationClass); if (annotations == null || annotations.length == 0) { for (Annotation metaAnnotation : method.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return Arrays.asList(annotations); } } Method superclassMethod = getOverriddenMethod(method); if (superclassMethod != null) { return getRepeatableAnnotations(superclassMethod, annotationClass); } } if (annotations == null) { return null; } return Arrays.asList(annotations); }
Method overriddenmethod = ReflectionUtils.getOverriddenMethod(method);