Is it somehow possible to annotate a Java Method in that way that i later can give another Method a Field Identifier or something like that, so that this Method can call the right one?
I know that normally you would do this with interfaces, but in my case this would be a immense count of interfaces… I need to use this in Entity Classes for my Database (and i’m not allowed to use a ORM Mapper)
For example: I have the Entity
public class Account{
private String username;
private String password;
private String name;
private String mail;
public void setUserName(String username){
this.username = username;
}
public String getUserName(){
return username;
}
[all other getter/Setter...]
}
Now i want to tell a Method that it need to validate a Field, for example the username field.
The Method that does should look like this:
public void validateField(XXX Field, Entity entity) throws ValidationFailedException, EmptyFieldException;
where XXX is somehow the FieldIdentifier.
Is that in any way possible in Java?
My only guess it that i Use public static final ìnt stuff in there to give every field a Method or so…
What do you use? I don’t see any annotations from which I can guess your framework. If you use Hibernate, you can use something like @NotNull or something else, or even do your custom validation:
This is how you would go with your example:
http://silentwalker.wordpress.com/2009/04/07/custom-validation-in-hibernate/
You can also create your own annotations without using any framework and use them @prepersist or whatever. Basically the sky is the limit.
P.S Since you don’t want to use any non internal code, here is how you can approach:
First, you can define an annotation
Then, before persisting the class, you would inspect its fields:
So, you have something like this: