Skip to content

Instantly share code, notes, and snippets.

@os890
Created June 22, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save os890/ead4a1fe3e379f04e457c7a717cd082e to your computer and use it in GitHub Desktop.
Save os890/ead4a1fe3e379f04e457c7a717cd082e to your computer and use it in GitHub Desktop.
@ApplicationScoped
public class EnumLabelsResolver implements LabelResolver {
@Override
public List<String> resolveLabelsFor(ConstraintDescriptor<?> constraintDescriptor) {
List<String> result = new ArrayList<>();
for (Method annotationMethod : constraintDescriptor.getAnnotation().annotationType().getDeclaredMethods()) {
if (LabelId.class.isAssignableFrom(annotationMethod.getReturnType())) {
try {
LabelId labelId = (LabelId) annotationMethod.invoke(constraintDescriptor.getAnnotation());
result.add(labelId.getId());
} catch (Exception e) {
throw ExceptionUtils.throwAsRuntimeException(e);
}
}
}
return result;
}
}
@ApplicationScoped
public class EnumMessageTemplateResolver implements MessageTemplateResolver {
@Override
public String resolveMessageTemplateFor(ConstraintDescriptor<?> constraintDescriptor) {
for (Method annotationMethod : constraintDescriptor.getAnnotation().annotationType().getDeclaredMethods()) {
if (MessageId.class.isAssignableFrom(annotationMethod.getReturnType())) {
try {
MessageId messageId = (MessageId) annotationMethod.invoke(constraintDescriptor.getAnnotation());
return "{" + messageId.getId() + "}";
} catch (Exception e) {
throw ExceptionUtils.throwAsRuntimeException(e);
}
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment