annotation = getAnnotation(superClass, annotationClass); annotation = getAnnotation(anInterface, annotationClass); if (annotation != null) { return annotation;
/** * 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 <A extends Annotation> List<A> getRepeatableAnnotations(Class<?> cls, Class<A> annotationClass) { A[] annotations = getRepeatableAnnotationsArray(cls, annotationClass); if (annotations == null || annotations.length == 0) { return null; } return Arrays.asList(annotations); }
/** * 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); }
if (!ReflectionUtils.isConstructorCompatible(constructor) && !ReflectionUtils.isInject(Arrays.asList(constructor.getDeclaredAnnotations()))) { continue;
final javax.ws.rs.Path apiPath = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Path.class); io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.responses.ApiResponse.class); List<io.swagger.v3.oas.annotations.security.SecurityScheme> apiSecurityScheme = ReflectionUtils.getRepeatableAnnotations(cls, io.swagger.v3.oas.annotations.security.SecurityScheme.class); List<io.swagger.v3.oas.annotations.security.SecurityRequirement> apiSecurityRequirements = ReflectionUtils.getRepeatableAnnotations(cls, io.swagger.v3.oas.annotations.security.SecurityRequirement.class); ExternalDocumentation apiExternalDocs = ReflectionUtils.getAnnotation(cls, ExternalDocumentation.class); io.swagger.v3.oas.annotations.tags.Tag[] apiTags = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.tags.Tag.class); io.swagger.v3.oas.annotations.servers.Server[] apiServers = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.servers.Server.class); javax.ws.rs.Consumes classConsumes = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Consumes.class); javax.ws.rs.Produces classProduces = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Produces.class); OpenAPIDefinition openAPIDefinition = ReflectionUtils.getAnnotation(cls, OpenAPIDefinition.class); javax.ws.rs.Produces methodProduces = ReflectionUtils.getAnnotation(method, javax.ws.rs.Produces.class); javax.ws.rs.Consumes methodConsumes = ReflectionUtils.getAnnotation(method, javax.ws.rs.Consumes.class); if (ReflectionUtils.isOverriddenMethod(method, cls)) { continue; javax.ws.rs.Path methodPath = ReflectionUtils.getAnnotation(method, javax.ws.rs.Path.class); io.swagger.v3.oas.annotations.Operation apiOperation = ReflectionUtils.getAnnotation(method, io.swagger.v3.oas.annotations.Operation.class); JsonView jsonViewAnnotation; JsonView jsonViewAnnotationForRequestBody; jsonViewAnnotationForRequestBody = null;
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; }
if (methodOpt.isDefined()) { Method method = methodOpt.get(); Annotation[][] paramAnnotations = ReflectionUtils.getParameterAnnotations(method); Consumes methodConsumes = ReflectionUtils.getAnnotation(method, Consumes.class); Consumes classConsumes = ReflectionUtils.getAnnotation(method.getDeclaringClass(), Consumes.class); JsonView jsonViewAnnotation = ReflectionUtils.getAnnotation(method, JsonView.class);
/** * Checks if the method methodToFind is the overridden method from the superclass or superinterface. * * @param methodToFind is method to check * @param cls is method class * @return true if the method is overridden method */ public static boolean isOverriddenMethod(Method methodToFind, Class<?> cls) { Set<Class<?>> superClasses = new HashSet<>(); for (Class c : cls.getInterfaces()) { superClasses.add(c); } if (cls.getSuperclass() != null) { superClasses.add(cls.getSuperclass()); } for (Class<?> superClass : superClasses) { if (superClass != null && !(superClass.equals(Object.class))) { for (Method method : superClass.getMethods()) { if (method.getName().equals(methodToFind.getName()) && method.getReturnType().isAssignableFrom(methodToFind.getReturnType()) && Arrays.equals(method.getParameterTypes(), methodToFind.getParameterTypes()) && !Arrays.equals(method.getGenericParameterTypes(), methodToFind.getGenericParameterTypes())) { return true; } } if (isOverriddenMethod(methodToFind, superClass)) { return true; } } } return false; }
/** * Collects field-level parameters from class. * * @param cls is a class for collecting * @param components * @return the collection of supported parameters */ public static List<Parameter> collectFieldParameters(Class<?> cls, Components components, javax.ws.rs.Consumes classConsumes, JsonView jsonViewAnnotation) { final List<Parameter> parameters = new ArrayList<Parameter>(); for (Field field : ReflectionUtils.getDeclaredFields(cls)) { final List<Annotation> annotations = Arrays.asList(field.getAnnotations()); final Type genericType = field.getGenericType(); parameters.addAll(collectParameters(genericType, annotations, components, classConsumes, jsonViewAnnotation)); } return parameters; }
/** * Returns overridden method from superclass if it exists. If method was not found returns null. * * @param method is method to find * @return overridden method from superclass */ public static Method getOverriddenMethod(Method method) { Class<?> declaringClass = method.getDeclaringClass(); Class<?> superClass = declaringClass.getSuperclass(); Method result = null; if (superClass != null && !(superClass.equals(Object.class))) { result = findMethod(method, superClass); } if (result == null) { for (Class<?> anInterface : declaringClass.getInterfaces()) { result = findMethod(method, anInterface); if (result != null) { return result; } } } return result; }
@Test public void isConstructorCompatibleTest() throws NoSuchMethodException { Assert.assertFalse(ReflectionUtils.isConstructorCompatible(Child.class.getDeclaredConstructor())); Assert.assertTrue(ReflectionUtils.isConstructorCompatible(Child.class.getDeclaredConstructor(String.class))); }
@Test public void isInjectTest() throws NoSuchMethodException { final Method injectableMethod = Child.class.getMethod("injectableMethod"); Assert.assertTrue(ReflectionUtils.isInject(Arrays.asList(injectableMethod.getDeclaredAnnotations()))); final Method methodToFind = Child.class.getMethod("parametrizedMethod1", Integer.class); Assert.assertFalse(ReflectionUtils.isInject(Arrays.asList(methodToFind.getDeclaredAnnotations()))); }
final javax.ws.rs.Path apiPath = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Path.class); io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.responses.ApiResponse.class); List<io.swagger.v3.oas.annotations.security.SecurityScheme> apiSecurityScheme = ReflectionUtils.getRepeatableAnnotations(cls, io.swagger.v3.oas.annotations.security.SecurityScheme.class); List<io.swagger.v3.oas.annotations.security.SecurityRequirement> apiSecurityRequirements = ReflectionUtils.getRepeatableAnnotations(cls, io.swagger.v3.oas.annotations.security.SecurityRequirement.class); ExternalDocumentation apiExternalDocs = ReflectionUtils.getAnnotation(cls, ExternalDocumentation.class); io.swagger.v3.oas.annotations.tags.Tag[] apiTags = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.tags.Tag.class); io.swagger.v3.oas.annotations.servers.Server[] apiServers = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.servers.Server.class); javax.ws.rs.Consumes classConsumes = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Consumes.class); javax.ws.rs.Produces classProduces = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Produces.class); OpenAPIDefinition openAPIDefinition = ReflectionUtils.getAnnotation(cls, OpenAPIDefinition.class); javax.ws.rs.Produces methodProduces = ReflectionUtils.getAnnotation(method, javax.ws.rs.Produces.class); javax.ws.rs.Consumes methodConsumes = ReflectionUtils.getAnnotation(method, javax.ws.rs.Consumes.class); if (ReflectionUtils.isOverriddenMethod(method, cls)) { continue; javax.ws.rs.Path methodPath = ReflectionUtils.getAnnotation(method, javax.ws.rs.Path.class); io.swagger.v3.oas.annotations.Operation apiOperation = ReflectionUtils.getAnnotation(method, io.swagger.v3.oas.annotations.Operation.class); JsonView jsonViewAnnotation; JsonView jsonViewAnnotationForRequestBody; jsonViewAnnotationForRequestBody = 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); }
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 isOverriddenMethodTest() throws NoSuchMethodException { for (Method method : Child.class.getMethods()) { if ("parametrizedMethod1".equals(method.getName())) { final boolean result = ReflectionUtils.isOverriddenMethod(method, Child.class); final Class<?> first = method.getParameterTypes()[0]; if (Number.class.equals(first)) { Assert.assertTrue(result); } else if (Integer.class.equals(first)) { Assert.assertFalse(result); } } else if ("parametrizedMethod3".equals(method.getName())) { Assert.assertFalse(ReflectionUtils.isOverriddenMethod(method, Child.class)); } } for (Method method : Object.class.getMethods()) { if ("equals".equals(method.getName())) { Assert.assertFalse(ReflectionUtils.isOverriddenMethod(method, Object.class)); } } for (Method method : IParent.class.getMethods()) { if ("parametrizedMethod5".equals(method.getName())) { Assert.assertTrue(ReflectionUtils.isOverriddenMethod(method, IParent.class)); } else if ("parametrizedMethod2".equals(method.getName())) { Assert.assertFalse(ReflectionUtils.isOverriddenMethod(method, IParent.class)); } else { Assert.fail("Method not expected"); } } }
/** * Returns the list of declared fields from the class <code>cls</code> and its superclasses * excluding <code>Object</code> class. If the field from child class hides the field from superclass, * the field from superclass won't be added to the result list. * * @param cls is the processing class * @return list of Fields */ public static List<Field> getDeclaredFields(Class<?> cls) { if (cls == null || Object.class.equals(cls)) { return Collections.emptyList(); } final List<Field> fields = new ArrayList<Field>(); final Set<String> fieldNames = new HashSet<String>(); for (Field field : cls.getDeclaredFields()) { fields.add(field); fieldNames.add(field.getName()); } for (Field field : getDeclaredFields(cls.getSuperclass())) { if (!fieldNames.contains(field.getName())) { fields.add(field); } } return fields; }
if (!ReflectionUtils.isConstructorCompatible(constructor) && !ReflectionUtils.isInject(Arrays.asList(constructor.getDeclaredAnnotations()))) { continue;
@Test public void testFindMethodForNullClass() throws Exception { Method method = ReflectionUtilsTest.class.getMethod("testFindMethodForNullClass", (Class<?>[]) null); assertNull(ReflectionUtils.findMethod(method, null)); }