本文整理匯總了Java中javax.validation.Path.PropertyNode方法的典型用法代碼示例。如果您正苦於以下問題:Java Path.PropertyNode方法的具體用法?Java Path.PropertyNode怎麽用?Java Path.PropertyNode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.validation.Path
的用法示例。
在下文中一共展示了Path.PropertyNode方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getAnnotations
import javax.validation.Path; //導入方法依賴的package包/類
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];
}
示例2: getPropertyAnnotations
import javax.validation.Path; //導入方法依賴的package包/類
private static Annotation[] getPropertyAnnotations(ConstraintViolation<?> violation, Path.PropertyNode node) {
try {
Class<?> leafBeanClass = violation.getLeafBean().getClass();
Field field = leafBeanClass.getDeclaredField(node.getName());
return field.getAnnotations();
} catch (NoSuchFieldException e) {
throw new IllegalStateException(e);
}
}