Skip to content

Instantly share code, notes, and snippets.

@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());
@ApplicationScoped
public class DbMessageSourceAdapter implements MessageSourceAdapter {
@Inject
private ViolationMessageDao violationMessageDao;
@Override
public String resolveMessage(String key, Locale locale) {
return violationMessageDao.findByKey(key);
}
}
<dependency>
<groupId>org.os890.bv.addon</groupId>
<artifactId>cdi-bv-label-addon</artifactId>
<version>1.0.0</version>
</dependency>
public class Person {
@Length(min = 1, max = 50, messageId = MY_LENGTH_MSG, propertyLabel = FIRST_NAME)
private String fName;
@Length(min = 2, max = 50, messageId = MY_LENGTH_MSG, propertyLabel = LAST_NAME)
private String lName;
}
public enum MyLabel implements LabelId {
FIRST_NAME("firstName"), LAST_NAME("lastName");
private final String key;
MyLabel(String key) {
this.key = "{" + key + "}";
}
@Override
@ReportAsSingleViolation
@Size
@Constraint(validatedBy = {})
@Target({FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface Length {
String message() default "{}"; //triggers the delegation to #messageId
Class<?>[] groups() default {};
public interface MessageId {
String getId();
}
@ProfileA
//further cdi-annotation/s
public class BeanProfileA {
}
@Profile("X")
//further cdi annotation/s
public class BeanProfileX {
}
public class MultiProfileExtension implements Extension {
private Set<String> activeProfileNames = new HashSet<>();
protected void determineActiveProfiles(@Observes BeforeBeanDiscovery beforeBeanDiscovery) {
String activeProfileString =
ConfigResolver.getPropertyValue("active-profiles", "Production");
Collections.addAll(activeProfileNames, activeProfileString.split(","));
}
protected void matchActiveProfiles(@Observes ProcessAnnotatedType pat, BeanManager bm) {