2

Is there an annotation for checking that two variables of an object have the same value?

Somethin like this:

public class User{

    private String id;

    @Size(min = 3, max = 18)
    private String username;

    @Size(min = 5, max = 32)
    private String password;

    @Equals("password")
    private String password2;

}    
krinklesaurus
  • 1,606
  • 3
  • 21
  • 38
  • http://stackoverflow.com/questions/9284450/jsr-303-validation-if-one-field-equals-something-then-these-other-fields-sho – Stefan Jul 29 '15 at 12:56
  • Thanks a lot!!! Even better: http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303 – krinklesaurus Jul 29 '15 at 13:00

1 Answers1

1

Generally speaking (as in: as general as in your question), the answer is: no.

And there can't be one - as it is not clear what "checking" would mean in that context. When should it happen; what should happen, and so on.

The only "general checking" I am aware of would be the checker-framework

But when you ask the same question for specific frameworks, that deal with data; then sure, such annotations can be created and used. In our project, we do have "property annotations" for a specific class of objects; and a whole lot of "framework" code around that to provide specific semantics when you are dealing with these kinds of objects.

In other words: depending on your "use case", there might be existing framworks (like Apache Spring) that provide some kind of annotation-based checking.

GhostCat
  • 137,827
  • 25
  • 176
  • 248