Java Code Examples for javax.validation.Path#PropertyNode

The following examples show how to use javax.validation.Path#PropertyNode . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstraintViolations.java    From krazo with Apache License 2.0 6 votes vote down vote up
private static Annotation[] getPropertyAnnotations(ConstraintViolation<?> violation, Path.PropertyNode node) {

        Class<?> leafBeanClass = violation.getLeafBean().getClass();
        Set<Annotation> allAnnotations = new HashSet<>();
        try {

            Field field = leafBeanClass.getDeclaredField(node.getName());
            allAnnotations.addAll(Arrays.asList(field.getAnnotations()));

        } catch (NoSuchFieldException e) {
            // ignore for now
        }

        allAnnotations.addAll(readAndWriteMethodAnnotationsForField(leafBeanClass, node.getName()));

        return allAnnotations.toArray(new Annotation[0]);
    }
 
Example 2
Source File: ConstraintViolations.java    From ozark with Apache License 2.0 6 votes vote down vote up
private static Annotation[] getPropertyAnnotations(ConstraintViolation<?> violation, Path.PropertyNode node) {

        Class<?> leafBeanClass = violation.getLeafBean().getClass();
        Set<Annotation> allAnnotations = new HashSet<>();
        try {

            Field field = leafBeanClass.getDeclaredField(node.getName());
            allAnnotations.addAll(Arrays.asList(field.getAnnotations()));

        } catch (NoSuchFieldException e) {
            // ignore for now
        }

        allAnnotations.addAll(readAndWriteMethodAnnotationsForField(leafBeanClass, node.getName()));

        return allAnnotations.toArray(new Annotation[0]);
    }
 
Example 3
Source File: ConstraintException.java    From rest.vertx with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to produce some sensible message to make some informed decision
 *
 * @param definition           route definition to get parameter information
 * @param constraintViolations list of violations
 * @return message describing violation
 */
private static String createMessage(RouteDefinition definition, Set<? extends ConstraintViolation<?>> constraintViolations) {

	List<String> messages = new ArrayList<>();
	for (ConstraintViolation<?> violation : constraintViolations) {

		StringBuilder message = new StringBuilder();
		for (Path.Node next : violation.getPropertyPath()) {

			if (next instanceof Path.ParameterNode &&
			    next.getKind().equals(ElementKind.PARAMETER)) {

				Path.ParameterNode paramNode = (Path.ParameterNode) next;
				int index = paramNode.getParameterIndex();
				if (index < definition.getParameters().size()) {
					MethodParameter param = definition.getParameters().get(index);
					switch (param.getType()) {
						case body:
							message.append(param.toString());
							message.append(" ").append(param.getDataType().getSimpleName());
							break;

						default:
							message.append(param.toString());
							break;
					}

				}
			}

			if (next instanceof Path.PropertyNode &&
			    next.getKind().equals(ElementKind.PROPERTY)) {

				Path.PropertyNode propertyNode = (Path.PropertyNode) next;
				message.append(".").append(propertyNode.getName());
			}
		}

		message.append(": ").append(violation.getMessage());

		messages.add(message.toString());
	}

	return StringUtils.join(messages, ", ");
}
 
Example 4
Source File: ConstraintViolations.java    From krazo with Apache License 2.0 3 votes vote down vote up
private static Annotation[] getAnnotations(ConstraintViolation<?> violation) {


        // create a simple list of nodes from the path
        List<Path.Node> nodes = new ArrayList<>();
        for (Path.Node node : violation.getPropertyPath()) {
            nodes.add(node);
        }
        Path.Node lastNode = nodes.get(nodes.size() - 1);

        // the path refers to some property of the leaf bean
        if (lastNode.getKind() == ElementKind.PROPERTY) {

            Path.PropertyNode propertyNode = lastNode.as(Path.PropertyNode.class);
            return getPropertyAnnotations(violation, propertyNode);

        }

        // The path refers to a method parameter
        else if (lastNode.getKind() == ElementKind.PARAMETER && nodes.size() == 2) {

            Path.MethodNode methodNode = nodes.get(0).as(Path.MethodNode.class);
            Path.ParameterNode parameterNode = nodes.get(1).as(Path.ParameterNode.class);

            return getParameterAnnotations(violation, methodNode, parameterNode);

        }


        log.warning("Could not read annotations for path: " + violation.getPropertyPath().toString());
        return new Annotation[0];

    }
 
Example 5
Source File: ConstraintViolations.java    From ozark with Apache License 2.0 3 votes vote down vote up
private static Annotation[] getAnnotations(ConstraintViolation<?> violation) {


        // create a simple list of nodes from the path
        List<Path.Node> nodes = new ArrayList<>();
        for (Path.Node node : violation.getPropertyPath()) {
            nodes.add(node);
        }
        Path.Node lastNode = nodes.get(nodes.size() - 1);

        // the path refers to some property of the leaf bean
        if (lastNode.getKind() == ElementKind.PROPERTY) {

            Path.PropertyNode propertyNode = lastNode.as(Path.PropertyNode.class);
            return getPropertyAnnotations(violation, propertyNode);

        }

        // The path refers to a method parameter
        else if (lastNode.getKind() == ElementKind.PARAMETER && nodes.size() == 2) {

            Path.MethodNode methodNode = nodes.get(0).as(Path.MethodNode.class);
            Path.ParameterNode parameterNode = nodes.get(1).as(Path.ParameterNode.class);

            return getParameterAnnotations(violation, methodNode, parameterNode);

        }


        log.warning("Could not read annotations for path: " + violation.getPropertyPath().toString());
        return new Annotation[0];

    }