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; }
private JDefinedClass buildTemplateConstraint(String name) { try { JDefinedClass tplConstraint = codeModel._class(Config.CFG.getBasePackageName() + ".annot."+name, ClassType.ANNOTATION_TYPE_DECL); tplConstraint.annotate(Documented.class); tplConstraint.annotate(Retention.class).param("value", RetentionPolicy.RUNTIME); tplConstraint.annotate(Target.class).paramArray("value").param(ElementType.TYPE).param(ElementType.ANNOTATION_TYPE).param(ElementType.FIELD).param(ElementType.METHOD); // Using direct as I don't know how to build default { } with code model tplConstraint.direct("\n" + " Class<?>[] groups() default {};\n" + " String message() default \"Invalid value\";\n" + " Class<? extends Payload>[] payload() default {};\n"); // Hack to force the import of javax.validation.Payload tplConstraint.javadoc().addThrows((JClass) codeModel._ref(Payload.class)).add("Force import"); return tplConstraint; } catch (JClassAlreadyExistsException e) { throw new RuntimeException("Tried to create an already existing class: " + name, e); } }
/** * (non-Javadoc) * * @see com.github.nest.arcteryx.meta.beans.IConstraintViolation#getConstraintSeverity() */ @SuppressWarnings("unchecked") @Override public ConstraintSeverity getConstraintSeverity() { Set<Class<? extends Payload>> payloads = this.getViolation().getConstraintDescriptor().getPayload(); if (payloads == null || payloads.size() == 0) { return ConstraintSeverity.defaultSeverity(); } else { // get first one Class<? extends Payload> payloadClass = payloads.iterator().next(); if (HibernateSeverity.INFO.class == payloadClass) { return ConstraintSeverity.INFO; } else if (HibernateSeverity.WARN.class == payloadClass) { return ConstraintSeverity.WARN; } else if (HibernateSeverity.ERROR.class == payloadClass) { return ConstraintSeverity.ERROR; } else if (HibernateSeverity.FATAL.class == payloadClass) { return ConstraintSeverity.FATAL; } else { return ConstraintSeverity.defaultSeverity(); } } }
private String errorMessage(ConstraintViolation<?> error) { Set<Class<? extends Payload>> payload = error.getConstraintDescriptor().getPayload(); if (payload.contains(NoLogging.class)) { return error.getPropertyPath().toString() + ", " + error.getMessage(); } else { return error.getPropertyPath().toString() + ", " + error.getMessage() + ", was: " + error.getInvalidValue(); } }
/** * Calculate a localized string message for a constraint violation. * <p> * If a labeled payload is present, the label of the payload is used as * template. Although not required, you are encouraged to use * {@link ValidationMessage} as base class for such payloads. If no labeled * payload is present and the message has the form "{......}", the string * without the brackets is used as key and the corresponding resource string * is used as template. Otherwise, the message is used directly as template. * * <p> * When the template is determined, it is used to create a * {@link PatternString} . The arguments are the standard validation * arguments (all attributes of the constraint annotation) plus * "invalidValue" containing the value which has been validated. * * <p> * finally the PatternString is resolved and returned as message. * */ public LString getMessage(ConstraintViolation<?> violation) { // arguments for the message Map<String, Object> args = new HashMap<>(); args.putAll(violation.getConstraintDescriptor().getAttributes()); args.put("invalidValue", violation.getInvalidValue()); // check for labeled payload { Optional<TranslatedString> pattern = violation.getConstraintDescriptor().getPayload().stream() .filter(Payload.class::isAssignableFrom).map(x -> labelUtil.type(x).tryLabel()) .filter(x -> x.isPresent()).map(x -> x.get()).findFirst(); if (pattern.isPresent()) // we found a labeled payload. Use payload label as pattern return new PatternString(patternStringResolver, pattern.get(), args); } // check for resource key reference String messageTemplate = violation.getMessageTemplate(); if (messageTemplate.startsWith("{") && messageTemplate.endsWith("}")) { return new PatternString(patternStringResolver, new TranslatedString(translatedStringResolver, messageTemplate.substring(1, messageTemplate.length() - 1)), args); } // fallback: just use the string as is as pattern return new PatternString(patternStringResolver, LString.of(messageTemplate), args); }
protected ConstraintDescriptorImpl(final T pannotation, final Set<Class<?>> pgroups, final Set<Class<? extends Payload>> ppayload, final List<Class<? extends ConstraintValidator<T, ?>>> pconstraintValidatorClasses, final Map<String, Object> pattributes, final Set<ConstraintDescriptor<?>> pcomposingConstraints, final boolean preportAsSingleViolation, final ElementType pelementType, final ConstraintOrigin pdefinedOn) { super(pannotation, pgroups, ppayload, pconstraintValidatorClasses, pattributes, pcomposingConstraints, preportAsSingleViolation, pelementType, pdefinedOn); }
/** * convert severity * * @param constraint * @return */ protected Class<? extends Payload> convertSeverity(C constraint) { ConstraintSeverity severity = constraint.getSeverity(); if (ConstraintSeverity.INFO.equals(severity)) { return HibernateSeverity.INFO.class; } else if (ConstraintSeverity.WARN.equals(severity)) { return HibernateSeverity.WARN.class; } else if (ConstraintSeverity.ERROR.equals(severity)) { return HibernateSeverity.ERROR.class; } else if (ConstraintSeverity.FATAL.equals(severity)) { return HibernateSeverity.FATAL.class; } else { return HibernateSeverity.INFO.class; } }
@Override public Set<Class<? extends Payload>> getPayload() { throw new UnsupportedOperationException(); }
@SuppressWarnings("unchecked") @Override public Class<? extends Payload>[] payload() { return new Class[0]; }
@Override public Class<? extends Payload>[] payload() { throw new UnsupportedOperationException(); }
@Required @Override public ConstraintDescriptor<?> getConstraintDescriptor() { String methodName = "getConstraintDescriptor"; Method method; try { method = MockConstraintViolation.class.getMethod(methodName, new Class<?>[] {}); } catch (NoSuchMethodException | SecurityException e) { throw new IllegalStateException("Failed to get the method: " + methodName, e); } Required annotation = method.getAnnotation(Required.class); return new ConstraintDescriptor<Annotation>() { @Override public Annotation getAnnotation() { return annotation; } @Override public String getMessageTemplate() { return null; } @Override public Set<Class<?>> getGroups() { return DfCollectionUtil.newHashSet(ClientError.class); } @Override public Set<Class<? extends Payload>> getPayload() { return null; } @Override public ConstraintTarget getValidationAppliesTo() { return null; } @Override public List<Class<? extends ConstraintValidator<Annotation, ?>>> getConstraintValidatorClasses() { return null; } @Override public Map<String, Object> getAttributes() { return null; } @Override public Set<ConstraintDescriptor<?>> getComposingConstraints() { return null; } @Override public boolean isReportAsSingleViolation() { return false; } @Override public ValidateUnwrappedValue getValueUnwrapping() { return null; } @Override public <U> U unwrap(Class<U> type) { return null; } }; }
public Builder<T> setPayload(final Class<? extends Payload>[] classes) { this.setPayload(new HashSet<>(Arrays.asList(classes))); return this; }
public Builder<T> setPayload(final Set<Class<? extends Payload>> payload) { this.payload = payload; return this; }
@Override public Set<Class<? extends Payload>> getPayload() { return this.payload; }
@Override public Set<Class<? extends Payload>> getPayload() { return Collections.emptySet(); }
Class<? extends Payload>[] payload() default {};