本文整理汇总了Java中javax.validation.Path类的典型用法代码示例。如果您正苦于以下问题:Java Path类的具体用法?Java Path怎么用?Java Path使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Path类属于javax.validation包,在下文中一共展示了Path类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getStringBasedPath
import javax.validation.Path; //导入依赖的package包/类
private String getStringBasedPath(Path.Node traversableProperty, Path pathToTraversableObject) {
StringBuilder path = new StringBuilder( );
for ( Path.Node node : pathToTraversableObject ) {
if (node.getName() != null) {
path.append( node.getName() ).append( "." );
}
}
if ( traversableProperty.getName() == null ) {
throw new AssertionFailure(
"TraversableResolver being passed a traversableProperty with null name. pathToTraversableObject: "
+ path.toString() );
}
path.append( traversableProperty.getName() );
return path.toString();
}
示例2: setupConstraintViolation
import javax.validation.Path; //导入依赖的package包/类
private ConstraintViolation<Object> setupConstraintViolation(Class offendingObjectClass, String path, Class<? extends Annotation> annotationClass, String message) {
ConstraintViolation<Object> mockConstraintViolation = mock(ConstraintViolation.class);
Path mockPath = mock(Path.class);
doReturn(path).when(mockPath).toString();
Annotation mockAnnotation = mock(Annotation.class);
doReturn(annotationClass).when(mockAnnotation).annotationType();
ConstraintDescriptor<?> mockConstraintDescriptor = mock(ConstraintDescriptor.class);
doReturn(mockAnnotation).when(mockConstraintDescriptor).getAnnotation();
when(mockConstraintViolation.getPropertyPath()).thenReturn(mockPath);
doReturn(mockConstraintDescriptor).when(mockConstraintViolation).getConstraintDescriptor();
when(mockConstraintViolation.getMessage()).thenReturn(message);
doReturn(offendingObjectClass).when(mockConstraintViolation).getRootBeanClass();
return mockConstraintViolation;
}
示例3: setupConstraintViolation
import javax.validation.Path; //导入依赖的package包/类
private ConstraintViolation<Object> setupConstraintViolation(String path, Class<? extends Annotation> annotationClass, String message) {
ConstraintViolation<Object> mockConstraintViolation = mock(ConstraintViolation.class);
Path mockPath = mock(Path.class);
doReturn(path).when(mockPath).toString();
Annotation mockAnnotation = mock(Annotation.class);
doReturn(annotationClass).when(mockAnnotation).annotationType();
ConstraintDescriptor<?> mockConstraintDescriptor = mock(ConstraintDescriptor.class);
doReturn(mockAnnotation).when(mockConstraintDescriptor).getAnnotation();
when(mockConstraintViolation.getPropertyPath()).thenReturn(mockPath);
doReturn(mockConstraintDescriptor).when(mockConstraintViolation).getConstraintDescriptor();
when(mockConstraintViolation.getMessage()).thenReturn(message);
return mockConstraintViolation;
}
示例4: getValidationErrorResponse
import javax.validation.Path; //导入依赖的package包/类
private List<ValidationMessage> getValidationErrorResponse(ConstraintViolationException constraintViolationException) {
final List<ValidationMessage> validationErrors = new ArrayList<>();
LOG.error("Got validation errors", constraintViolationException);
for (ConstraintViolation<?> violationSet : constraintViolationException.getConstraintViolations()) {
List<String> propertyList = new ArrayList<>();
Iterator<Path.Node> propertyIterator = violationSet
.getPropertyPath().iterator();
while (propertyIterator.hasNext()) {
propertyList.add(propertyIterator.next().getName());
}
// add violations errors in response
validationErrors.add(ValidationMessage.builder()
.entity(violationSet.getRootBeanClass().getName())
// remove { and }
.messageTemplate(violationSet.getMessageTemplate().replaceAll("^[{]|[}]$", ""))
.propertyList(propertyList).build());
}
return validationErrors;
}
示例5: validateAdditionalRules
import javax.validation.Path; //导入依赖的package包/类
public void validateAdditionalRules(ValidationErrors errors) {
// all previous validations return no errors
if (crossFieldValidate && errors.isEmpty()) {
BeanValidation beanValidation = AppBeans.get(BeanValidation.NAME);
Validator validator = beanValidation.getValidator();
Set<ConstraintViolation<Entity>> violations = validator.validate(getItem(), UiCrossFieldChecks.class);
violations.stream()
.filter(violation -> {
Path propertyPath = violation.getPropertyPath();
Path.Node lastNode = Iterables.getLast(propertyPath);
return lastNode.getKind() == ElementKind.BEAN;
})
.forEach(violation -> errors.add(violation.getMessage()));
}
}
示例6: makePath
import javax.validation.Path; //导入依赖的package包/类
private String makePath ( final ConstraintViolation<Object> entry )
{
final StringBuilder sb = new StringBuilder ();
final Path p = entry.getPropertyPath ();
for ( final Node n : p )
{
if ( sb.length () > 0 )
{
sb.append ( '.' );
}
sb.append ( n.getName () );
}
return sb.toString ();
}
示例7: convert
import javax.validation.Path; //导入依赖的package包/类
/**
* @see org.tools.hqlbuilder.common.interfaces.ValidationExceptionConverter#convert(java.lang.Exception)
*/
@Override
public ValidationException convert(Exception e) {
javax.validation.ConstraintViolationException ex = (ConstraintViolationException) e;
List<org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue> ivs = new ArrayList<org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue>();
for (javax.validation.ConstraintViolation<?> iv : ex.getConstraintViolations()) {
Object bean = iv.getLeafBean();
Class<?> beanClass = iv.getRootBeanClass();
String message = iv.getMessage();
Path path = iv.getPropertyPath();
Iterator<javax.validation.Path.Node> it = path.iterator();
Path.Node node = it.next();
while (it.hasNext()) {
node = it.next();
}
String propertyName = String.valueOf(node);
String propertyPath = String.valueOf(path);
Object rootBean = iv.getRootBean();
Object value = iv.getInvalidValue();
org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue tmp = new org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue(
bean, beanClass, message, propertyName, propertyPath, rootBean, value);
ivs.add(tmp);
}
return new ValidationException(ex.getMessage(), ivs);
}
示例8: convert
import javax.validation.Path; //导入依赖的package包/类
/**
* @see org.tools.hqlbuilder.common.interfaces.ValidationExceptionConverter#convert(java.lang.Exception)
*/
@Override
public ValidationException convert(Exception e) {
javax.validation.ConstraintViolationException ex = (ConstraintViolationException) e;
List<org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue> ivs = new ArrayList<>();
for (javax.validation.ConstraintViolation<?> iv : ex.getConstraintViolations()) {
Object bean = iv.getLeafBean();
Class<?> beanClass = iv.getRootBeanClass();
String message = iv.getMessage();
Path path = iv.getPropertyPath();
Iterator<javax.validation.Path.Node> it = path.iterator();
Path.Node node = it.next();
while (it.hasNext()) {
node = it.next();
}
String propertyName = String.valueOf(node);
String propertyPath = String.valueOf(path);
Object rootBean = iv.getRootBean();
Object value = iv.getInvalidValue();
org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue tmp = new org.tools.hqlbuilder.common.exceptions.ValidationException.InvalidValue(
bean, beanClass, message, propertyName, propertyPath, rootBean, value);
ivs.add(tmp);
}
return new ValidationException(ex.getMessage(), ivs);
}
示例9: testExtractViolationInfoFromNode
import javax.validation.Path; //导入依赖的package包/类
/**
* Test of extractViolationInfoFromNode method, of class ConstraintServices.
*/
@Test
public void testExtractViolationInfoFromNode() {
System.out.println("extractViolationInfoFromNode");
Path.Node node = mock(Path.Node.class);
when(node.getKind()).thenReturn(ElementKind.METHOD).thenReturn(ElementKind.PARAMETER).thenReturn(ElementKind.PROPERTY);
when(node.getName()).thenReturn("arg0").thenReturn("prop");
doReturn(5).when(instance).getIndexFromArgname(anyString());
ConstraintViolation cv = new ConstraintViolation();
instance.extractViolationInfoFromNode(node, cv);
assertThat(cv.getMessage()).isNull();
assertThat(cv.getIndex()).isEqualTo(0);
assertThat(cv.getProp()).isNull();
cv = new ConstraintViolation();
instance.extractViolationInfoFromNode(node, cv);
assertThat(cv.getMessage()).isNull();
assertThat(cv.getIndex()).isEqualTo(5);
assertThat(cv.getProp()).isNull();
cv = new ConstraintViolation();
instance.extractViolationInfoFromNode(node, cv);
assertThat(cv.getMessage()).isNull();
assertThat(cv.getIndex()).isEqualTo(0);
assertThat(cv.getProp()).isEqualTo("prop");
}
示例10: instantiate
import javax.validation.Path; //导入依赖的package包/类
/**
* instantiate a ConstraintViolationImpl.
*
* @param streamReader serialized stream reader to take data from
* @return ConstraintViolationImpl
* @throws SerializationException if deserialization fails
*/
public static ConstraintViolationImpl<Object> instantiate(
final SerializationStreamReader streamReader) throws SerializationException {
final String messageTemplate = null;
final String interpolatedMessage = streamReader.readString();
final Class<Object> rootBeanClass = null;
final Object rootBean = null;
final Object leafBeanInstance = null;
final Object value = null;
final Path propertyPath = (Path) streamReader.readObject();
final ConstraintDescriptor<?> constraintDescriptor = null;
final ElementType elementType = null;
final Map<String, Object> messageParameters = new HashMap<>();
final Map<String, Object> expressionVariables = new HashMap<>();
return (ConstraintViolationImpl<Object>) ConstraintViolationImpl.forBeanValidation(
messageTemplate, messageParameters, expressionVariables, interpolatedMessage, rootBeanClass,
rootBean, leafBeanInstance, value, propertyPath, constraintDescriptor, elementType, null);
}
开发者ID:ManfredTremmel,项目名称:gwt-bean-validators,代码行数:26,代码来源:ConstraintViolationImpl_CustomFieldSerializer.java
示例11: ConstraintViolationImpl
import javax.validation.Path; //导入依赖的package包/类
private ConstraintViolationImpl(final String messageTemplate,
final Map<String, Object> messageParameters, final Map<String, Object> expressionVariables,
final String interpolatedMessage, final Class<T> rootBeanClass, final T rootBean,
final Object leafBeanInstance, final Object value, final Path propertyPath,
final ConstraintDescriptor<?> constraintDescriptor, final ElementType elementType,
final Object[] executableParameters, final Object executableReturnValue,
final Object dynamicPayload) {
this.messageTemplate = messageTemplate;
this.messageParameters = messageParameters;
this.expressionVariables = expressionVariables;
this.interpolatedMessage = interpolatedMessage;
this.rootBean = rootBean;
this.value = value;
this.propertyPath = propertyPath;
this.leafBeanInstance = leafBeanInstance;
this.constraintDescriptor = constraintDescriptor;
this.rootBeanClass = rootBeanClass;
this.elementType = elementType;
this.executableParameters = executableParameters;
this.executableReturnValue = executableReturnValue;
this.dynamicPayload = dynamicPayload;
// pre-calculate hash code, the class is immutable and hashCode is needed often
this.hashCodeValue = this.createHashCode();
}
示例12: as
import javax.validation.Path; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T extends Path.Node> T as(final Class<T> nodeType) throws ClassCastException { // NOPMD
if (this.kind == ElementKind.BEAN && nodeType == BeanNode.class
|| this.kind == ElementKind.CONSTRUCTOR && nodeType == ConstructorNode.class
|| this.kind == ElementKind.CROSS_PARAMETER && nodeType == CrossParameterNode.class
|| this.kind == ElementKind.METHOD && nodeType == MethodNode.class
|| this.kind == ElementKind.PARAMETER && nodeType == ParameterNode.class
|| this.kind == ElementKind.PROPERTY && (nodeType == PropertyNode.class
|| nodeType == org.hibernate.validator.path.PropertyNode.class)
|| this.kind == ElementKind.RETURN_VALUE && nodeType == ReturnValueNode.class
|| this.kind == ElementKind.CONTAINER_ELEMENT && (nodeType == ContainerElementNode.class
|| nodeType == org.hibernate.validator.path.ContainerElementNode.class)) {
return (T) this;
}
throw LOG.getUnableToNarrowNodeTypeException(this.getClass(), this.kind, nodeType);
}
示例13: createConstraintViolations
import javax.validation.Path; //导入依赖的package包/类
private static ConstraintViolation<Object> createConstraintViolations(
LanguageProvider languageProvider, ClassInfo classInfo,
Object domainObject, ValidationViolation validationViolation) {
String messageTemplateKey = validationViolation.getMessageTemplateKey();
String messageTemplateDefault = validationViolation
.getMessageTemplateInEnglish();
String messageTemplate = languageProvider.getText(messageTemplateKey,
messageTemplateDefault);
Object invalidValue = validationViolation.getInvalidValue();
String message = String.format(messageTemplate, invalidValue);
Path path = PathImpl.create(classInfo.getSimpleName());
@SuppressWarnings("unchecked")
Class<Object> rootBeanClass = (Class<Object>) domainObject.getClass();
ConstraintDescriptor<?> constraintDescriptor = null;
ElementType elementType = null;
ConstraintViolationImpl<Object> constraintViolation = new ConstraintViolationImpl<Object>(
messageTemplate, message, domainObject, domainObject, path,
domainObject, constraintDescriptor, rootBeanClass, elementType);
return constraintViolation;
}
示例14: toResponse
import javax.validation.Path; //导入依赖的package包/类
@Override
public Response toResponse(ConstraintViolationException exception) {
LOGGER.debug("Validation constraint violation {}", exception.getConstraintViolations());
ValidationMessage validationMessage = new ValidationMessage();
Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
Multimap<String, String> errors = ArrayListMultimap.create();
for (ConstraintViolation<?> cv : violations) {
String name = StreamSupport.stream(cv.getPropertyPath().spliterator(), false)
.map(Path.Node::getName)
.reduce((first, second) -> second)
.orElseGet(() -> cv.getPropertyPath().toString());
errors.put(name, cv.getMessage());
}
validationMessage.setErrors(errors.asMap());
return Response.status(Response.Status.BAD_REQUEST)
.entity(validationMessage)
.build();
}
示例15: getResponseStatus
import javax.validation.Path; //导入依赖的package包/类
/**
* Determine the response status (400 or 500) from the given BV exception.
*
* @param violation BV exception.
* @return response status (400 or 500).
*/
public static Response.Status getResponseStatus(final ConstraintViolationException violation) {
final Iterator<ConstraintViolation<?>> iterator = violation.getConstraintViolations().iterator();
if (iterator.hasNext()) {
for (final Path.Node node : iterator.next().getPropertyPath()) {
final ElementKind kind = node.getKind();
if (ElementKind.RETURN_VALUE.equals(kind)) {
return Response.Status.INTERNAL_SERVER_ERROR;
}
}
}
return Response.Status.BAD_REQUEST;
}