Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Java Refelection instead of instanceof  RSS feed

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am passing in an object into a method(object can an instance of three different classes A,B,C and these have no common interface or abstract class). I do not want to repeat the code for each instance, how can I use reflection to make it one setter for all the objects?

 
Sheriff
Posts: 21747
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now I caught Exception, but in real life situations you will want to catch all exceptions separately: NoSuchMethodException, IllegalAccessException and InvocationTargetException.

Please note that I'd prefer your old solution; sure, it may be more tedious to write, but it is less error prone and more clear. Instead of trying to call a method on an object without knowing it actually has the method, you are actually making sure it has the method first. Also, you show that you know what object types you are expecting.
[ May 01, 2008: Message edited by: Rob Prime ]
 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, Thanks for your response. After thinking about your suggestion of keeping the old code I plan to stick to it so that its more clear/easy to understand and no danger of not finding a method on a class.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, this is not what you are asking but the most elegant solution would be to define an interface if it is possible.
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find yourself writing code like that, then it's a good time to learn what object oriented programming is all about. Specifically, polymorphism.

Create an interface or class that contains the method setXXXXX(...), and then make classes A, B and C implement the interface or extend the class:
 
Rob Spoor
Sheriff
Posts: 21747
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. I just assumed that these classes could not be modified, since Santosh said there was no common interface.
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!