lundi 27 novembre 2017

How to get the value of associate field using reflection

class A {

     private String someField;

     @validation
     private String annotatedField;
}

I'm implementing a custom constraint validation annotation @validation to validate annotatedField. I need to know the value of someField to satisfy the logic of my validation.

Validator implements ConstraintValidator<validation, String>{

      @Override
      public void initialize(validation constraintAnnotation) {

      }

      @Override
      public boolean isValid(String annotatedField, ConstraintValidatorContext context) {

           if (StringUtils.isBlank(annotatedField)) {
                return true;
           }

           String someField; // get some someField value given the annotatedField

      }
}

Is there a way to do this using reflection?





Aucun commentaire:

Enregistrer un commentaire