In PHP, which is, of course, an interpreted language, I can dereference field names at runtime, e.g. 'source' and 'value' below, and use the values of those two fields pointed to by source and value.
{$key['source']}[LANGUAGE_NONE][0]['value'];
Is there a similar facility in Java to do so? I have some properties of a bean that I need to validate and perform various checks against. Rather than hardcoding the logic, I’d like to loop through a SQL table of fieldnames and perform the indicated validation on the field. E.g. if I have the value "email" in the database, I would pick the property below and validate it based on the rules in the database column:
String email;
I will have many beans and many fields, which is why I was thinking of this approach. I don’t have a say in the design 🙁
Another option I was thinking was using Spring validation, but there does not appear to be a way to trigger it until a Spring Form Bean is submitted. Maybe there’s an alternate way to trigger it?
I think you are saying that you wish to store the names of properties in a data store, and access that data store at runtime to query the properties. The construct you have described is closest to a Map in Java – a key/value pair, and there is nothing to stop you accessing data stored that way, e.g.
myMap.get("myproperty");. The problem is modelling all your Objects as Maps isn’t very OO.Another option is to use reflection and convention. So a property in your database named ‘source’ would be accessed by a method called ‘getSource()’. This is similar to JavaBeans
Is that what you meant?