Why are static reference types of methods not supported on annotation elements?

I would like to create my own DateTime validation annotation

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalQuery;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Constraint(validatedBy = DateTimeValidator.class)
public @interface DateTime {
    String message() default "{com.example.constraints.DateTime.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

    TemporalQuery<?>[] queries() default {ZonedDateTime::from, LocalDateTime::from};
}

public class DateTimeValidator implements ConstraintValidator<DateTime, String> {

    private DateTime dateTime;
    private TemporalQuery<?>[] queries;

    @Override
    public void initialize(DateTime dateTime) {
        queries = dateTime.queries();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        try {
            DateTimeFormatter.ISO_DATE_TIME.parseBest(s, queries);
        } catch (DateTimeParseException e) {
            return false;
        }
        return true;
    }
}

      

Is there a reason why static method references cannot be supported in annotation types? What would be the best way to achieve my goal differently?

+3


source to share





All Articles