I have been tasked with implementing an @Username and @Password annotations. This generally might not be a problem except that the password and username cannot be the same -- meaning that these 2 tags are interdependent.

The question is: Without the containing class to reference, how on EARTH will I get reference to the other field in the class? Do annotations/spring have something I am unaware of to make this possible? Seems to me it has likely been done before. Example below:

public class PasswordTest {

@Username
private String userName; // set at a later point

@Password
private String password; // set later

// .. constructor

}
private ValidatorFactory factory;
private Validator validator;



factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();


PasswordTest t = new PasswordTest("user","password");
Set<ConstraintViolation<PasswordTest>> violations = validator.validate(t);

This is using hibernate validation.