本文整理匯總了Java中javax.validation.ConstraintValidator類的典型用法代碼示例。如果您正苦於以下問題:Java ConstraintValidator類的具體用法?Java ConstraintValidator怎麽用?Java ConstraintValidator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ConstraintValidator類屬於javax.validation包,在下文中一共展示了ConstraintValidator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: ConstraintDescriptorImpl
import javax.validation.ConstraintValidator; //導入依賴的package包/類
protected ConstraintDescriptorImpl(final T annotation, final Set<Class<?>> groups,
final Set<Class<? extends Payload>> payload,
final List<Class<? extends ConstraintValidator<T, ?>>> constraintValidatorClasses,
final Map<String, Object> attributes, final Set<ConstraintDescriptor<?>> composingConstraints,
final boolean reportAsSingleViolation, final ElementType elementType,
final ConstraintOrigin definedOn) {
super();
this.annotation = annotation;
this.groups = groups;
this.payload = payload;
this.constraintValidatorClasses = constraintValidatorClasses;
this.attributes = attributes;
this.composingConstraints = composingConstraints;
this.reportAsSingleViolation = reportAsSingleViolation;
this.elementType = elementType;
this.definedOn = definedOn;
}
示例2: getTypeOfConstraintValidator
import javax.validation.ConstraintValidator; //導入依賴的package包/類
/**
* Finds the type that a constraint validator will check.
*
* <p>
* This type comes from the first parameter of the isValid() method on the constraint validator.
* However, this is a bit tricky because ConstraintValidator has a parameterized type. When using
* Java reflection, we will see multiple isValid() methods, including one that checks
* java.lang.Object.
* </p>
*
* <p>
* Strategy: for now, assume there are at most two isValid() methods. If there are two, assume one
* of them has a type that is assignable from the other. (Most likely, one of them will be
* java.lang.Object.)
* </p>
*
* @throws IllegalStateException if there isn't any isValid() method or there are more than two.
*/
static <T extends Annotation> Class<?> getTypeOfConstraintValidator(
final Class<? extends ConstraintValidator<T, ?>> constraintClass) {
int candidateCount = 0;
Class<?> result = null;
for (final Method method : constraintClass.getMethods()) {
if (method.getName().equals("isValid") && method.getParameterTypes().length == 2
&& method.getReturnType().isAssignableFrom(Boolean.TYPE)) {
final Class<?> firstArgType = method.getParameterTypes()[0];
if (result == null || result.isAssignableFrom(firstArgType)) {
result = firstArgType;
}
candidateCount++;
}
}
if (candidateCount == 0) {
throw new IllegalStateException("ConstraintValidators must have a isValid method");
} else if (candidateCount > 2) {
throw new IllegalStateException(
"ConstraintValidators must have no more than two isValid methods");
}
return result;
}
示例3: getValidatorForType
import javax.validation.ConstraintValidator; //導入依賴的package包/類
static <A extends Annotation> ImmutableSet<Class<? extends ConstraintValidator<A, ?>>> //
getValidatorForType(final Class<?> ptype,
final List<Class<? extends ConstraintValidator<A, ?>>> constraintValidatorClasses) {
final Class<?> type = Primitives.wrap(ptype);
final Map<Class<?>, Class<? extends ConstraintValidator<A, ?>>> map = Maps.newHashMap();
for (final Class<? extends ConstraintValidator<A, ?>> conClass : constraintValidatorClasses) {
final Class<?> aType = Primitives.wrap(getTypeOfConstraintValidator(conClass));
if (aType.isAssignableFrom(type)) {
map.put(aType, conClass);
}
}
// TODO(nchalko) implement per spec
// Handle Arrays and Generics
final Set<Class<?>> best = Util.findBestMatches(type, map.keySet());
final Predicate<Class<?>> inBest = key -> best.contains(key);
return ImmutableSet.copyOf(Maps.filterKeys(map, inBest).values());
}
示例4: localValidatorFactoryBean
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
HibernateValidatorConfigurationStrategy strategy = Optional.ofNullable(hibernateValidatorConfigurationStrategy)
.orElseGet(() -> configuration -> {
});
List<? extends ConstraintValidator> validators = Optional.ofNullable(this.validators)
.orElseGet(Collections::emptyList);
return new CustomLocalValidatorFactoryBean(strategy, validators);
}
示例5: addConstraint
import javax.validation.ConstraintValidator; //導入依賴的package包/類
private void addConstraint(ConstraintMapping mapping, ConstraintValidator validator) {
Stream.of(validator.getClass().getGenericInterfaces())
.filter(genericInterface -> genericInterface instanceof ParameterizedType)
.map(genericInterface -> (ParameterizedType) genericInterface)
.filter(genericInterface -> isConstraintValidator(genericInterface.getRawType()))
.forEach(constraintValidatorInterface -> {
@SuppressWarnings("unchecked")
Class<? extends Annotation> a = (Class<? extends Annotation>) constraintValidatorInterface.getActualTypeArguments()[0];
mapping.constraintDefinition(a).validatedBy(getValidatorClass(validator));
});
}
示例6: buildTemplateConstraintValidator
import javax.validation.ConstraintValidator; //導入依賴的package包/類
private JDefinedClass buildTemplateConstraintValidator(String name, JDefinedClass constraint, Class<?> param) {
try {
JClass cv = (JClass) codeModel._ref(ConstraintValidator.class);
cv = cv.narrow(constraint, (JClass) codeModel._ref(param));
JDefinedClass validator = constraint._class(JMod.STATIC | JMod.PUBLIC, name);
validator._implements(cv);
validator.method(JMod.PUBLIC, void.class, "initialize").param(constraint, "parameters");
JMethod isValid = validator.method(JMod.PUBLIC, boolean.class, "isValid");
isValid.param(Object.class, "value");
isValid.param(ConstraintValidatorContext.class, "context");
return validator;
} catch (JClassAlreadyExistsException e) {
throw new RuntimeException("Tried to create an already existing class: " + name, e);
}
}
示例7: validatePropertyConstraints
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> void validatePropertyConstraints(
final MinijaxConstraintValidatorContext<T> context,
final PropertyDescriptor property,
final Object value) {
for (final ConstraintDescriptor constraint : property.getConstraintDescriptors()) {
final ConstraintValidator validator = ((MinijaxConstraintDescriptor) constraint).getValidator();
if (!validator.isValid(value, context)) {
context.buildViolation(constraint, value);
}
}
}
示例8: validatePropertyElementConstraints
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
private <T> void validatePropertyElementConstraints(
final MinijaxConstraintValidatorContext<T> context,
final PropertyDescriptor property,
final Object value) {
for (final ContainerElementTypeDescriptor descriptor : property.getConstrainedContainerElementTypes()) {
for (final ConstraintDescriptor constraint : descriptor.getConstraintDescriptors()) {
final ConstraintValidator validator = ((MinijaxConstraintDescriptor) constraint).getValidator();
if (value instanceof List) {
validateList(context, constraint, validator, (List) value);
} else if (value instanceof Iterable) {
validateIterable(context, constraint, validator, (Iterable) value);
} else if (value instanceof Map && descriptor.getTypeArgumentIndex() == 0) {
validateMapKeys(context, constraint, validator, (Map<?, ?>) value);
} else if (value instanceof Map) {
validateMapValues(context, constraint, validator, (Map<?, ?>) value);
} else if (value instanceof Optional) {
validateOptional(context, constraint, validator, (Optional) value);
}
}
}
}
示例9: validateList
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> void validateList(
final MinijaxConstraintValidatorContext<T> context,
final ConstraintDescriptor constraint,
final ConstraintValidator validator,
final List list) {
for (int i = 0; i < list.size(); i++) {
if (!validator.isValid(list.get(i), context)) {
context.buildViolation(constraint, list.get(i), "[" + i + "].<list element>");
}
}
}
示例10: validateIterable
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> void validateIterable(
final MinijaxConstraintValidatorContext<T> context,
final ConstraintDescriptor constraint,
final ConstraintValidator validator,
final Iterable iterable) {
for (final Object element : iterable) {
if (!validator.isValid(element, context)) {
context.buildViolation(constraint, element, "[].<iterable element>");
}
}
}
示例11: validateMapKeys
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> void validateMapKeys(
final MinijaxConstraintValidatorContext<T> context,
final ConstraintDescriptor constraint,
final ConstraintValidator validator,
final Map<?, ?> map) {
for (final Object element : map.keySet()) {
if (!validator.isValid(element, context)) {
context.buildViolation(constraint, element, "<K>[].<map key>");
}
}
}
示例12: validateMapValues
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> void validateMapValues(
final MinijaxConstraintValidatorContext<T> context,
final ConstraintDescriptor constraint,
final ConstraintValidator validator,
final Map<?, ?> map) {
for (final Entry<?, ?> entry : map.entrySet()) {
if (!validator.isValid(entry.getValue(), context)) {
context.buildViolation(constraint, entry.getValue(), "[" + entry.getKey() + "].<map value>");
}
}
}
示例13: validateOptional
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> void validateOptional(
final MinijaxConstraintValidatorContext<T> context,
final ConstraintDescriptor constraint,
final ConstraintValidator validator,
final Optional optional) {
if (optional.isPresent() && !validator.isValid(optional.get(), context)) {
context.buildViolation(constraint, optional.get());
}
}
示例14: buildDeclaredValidator
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T extends Annotation> MinijaxConstraintDescriptor<T> buildDeclaredValidator(final T annotation, final Class validatedBy) {
final Class<? extends ConstraintValidator<T, ?>> c = validatedBy;
try {
return new MinijaxConstraintDescriptor<>(annotation, c.getConstructor().newInstance());
} catch (final ReflectiveOperationException ex) {
throw new ValidationException(ex);
}
}
示例15: getInstance
import javax.validation.ConstraintValidator; //導入依賴的package包/類
@Override
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> type) {
T result = null;
try {
result = this.beanFactory.getBean(type);
} catch(BeansException e) {
// ignore, as we initialize the validator below
}
if( result == null ) {
result = this.delegate.getInstance(type);
this.beanFactory.initializeBean(result, result.getClass() + "#" + result.hashCode());
}
return result;
}