Love all, trust a few, do wrong to none.
chaitanya karthikk wrote:I want to know whether using reflection api is encouraged in business applications or not.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Richard Tookey wrote:It allowed one to build a map between a form field name and a (setter Method, validator, converter) triple. The user was unaware that behind the scenes reflection was being used. All the user had to do was to provide the name of the setter, the validator and the converter. Since the setter Method lookup was only done once for each field the whole was very very fast and very very effective.
Winston Gutkowski wrote:
1. Was this done in Java?
2. How would the user know the name of a setter?
Matthew Brown wrote:To be fair, I suspect many frameworks use reflection for this sort of thing. I'm thinking about those that use annotations to label model objects with certain validation criteria...
Winston Gutkowski wrote:
chaitanya karthikk wrote:I want to know whether using reflection api is encouraged in business applications or not.
Not by me. And here's just a few reasons:
1. It's unsafe - you can do virtually anything with reflection, and you lose all the normal compiler time checking provided by the language.
2. It's arcane - and somebody will have to deal with the code when you're gone. Glad it won't be me.
3. It's SLOW - up to 50 times slower than regular code.
4. (most of the time) It's unnecessary - very often it's done simply because the programmer can't be bothered (or doesn't understand how) to code a solution that doesn't involve reflection.
Your problem sounds like simple String validation to me; and reflection is the LAST thing I'd choose to do that - in fact it's the last thing I'd choose to do most things.
Winston
Love all, trust a few, do wrong to none.
Winston Gutkowski wrote:
Matthew Brown wrote:To be fair, I suspect many frameworks use reflection for this sort of thing. I'm thinking about those that use annotations to label model objects with certain validation criteria...
Now that makes a lot more sense, because at least then the rules are strictly defined. I imagine you could even use something like Guice; but chaitanya already said they weren't using dependency injection in his first post.
I also have to admit to an almost pathological loathing of reflection; mainly because I've seen it misused so much.![]()
Winston
chaitanya karthikk wrote:As Matthew said now a days all frameworks use reflection api extensively. I am working on spring framework and almost for all things spring used reflection api. When it comes to form binding, use of annotations etc.
Then you shouldn't I use reflection api with careful and cautious coding?
I know I am reinventing the wheel. I don't know why our architect does not want to use it. He says that why should I use an additional dependency. I already said that spring itself suggests to use JSR 330 implementation like hibernate-validator.Jayesh A Lalwani wrote:I have seen a homegrown DI injection framework that did all the reflection at runtime. Very slow and very messy. I have to ask:- why aren't you using Spring with Hibernate validator?. You are reinventing the wheel here
Love all, trust a few, do wrong to none.
chaitanya karthikk wrote:@Winston: I want to write just one class which will do basic validations like checking whether the fields are empty, contains white spaces, names are alphanumeric, age, mobile numbers etc strings are integers, email fields contain valid email addresses etc.
Is there any specific approach to do this without using reflection api. I don't find any alternative.