com.wavemaker.tools.apidocs.tools.parser.util.ConstraintsUtil Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (C) 2022-2023 WaveMaker, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.wavemaker.tools.apidocs.tools.parser.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import jakarta.validation.Constraint;
import org.apache.commons.lang3.ClassUtils;
import com.google.common.collect.Sets;
/**
* @author Uday Shankar
*/
public class ConstraintsUtil {
private static Set ignoredFields = Sets.newHashSet("groups", "payload");
public static List getConstraintAnnotations(Field field) {
List annotationList = new ArrayList<>();
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
Class extends Annotation> annotationClass = annotation.annotationType();
Annotation[] annotationClassAnnotations = annotationClass.getAnnotations();
boolean isConstraintClass = false;
for (Annotation annotationClassAnnotation : annotationClassAnnotations) {
if (annotationClassAnnotation.annotationType().equals(Constraint.class)) {
isConstraintClass = true;
}
}
if (isConstraintClass) {
annotationList.add(annotation);
}
}
return annotationList;
}
public static Map getConstraintAnnotationParametersMap(Annotation annotation) {
Class extends Annotation> annotationClass = annotation.annotationType();
Method[] methods = annotationClass.getDeclaredMethods();
Map parametersMap = new LinkedHashMap<>();
for (Method method : methods) {
if (method.isDefault()) {
continue;
}
String methodName = method.getName();
try {
Object value = method.invoke(annotation);
if (canConstraintMemberBeAdded(method, value)) {
parametersMap.put(methodName, value);
}
} catch (Exception e) {
throw new RuntimeException("Failed to get constraint annotation parameters for method [" + method + "]", e);
}
}
return parametersMap;
}
private static boolean canConstraintMemberBeAdded(Method method, Object value) {
if (value == null || ignoredFields.contains(method.getName())) {
return false;
}
if (ClassUtils.isPrimitiveOrWrapper(value.getClass())) {
return true;
} else {
return !Objects.deepEquals(value, method.getDefaultValue());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy