• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to tell if an instance has a method?  RSS feed

 
Johann Dobbins
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is sorta a silly question, because the correct solution is obvious, but isn't an option, because I can not add or change any interfaces.

Say I have 20 objects, all of which implement interface X. And the majority of these objects, expose a method setName(String name), which is not defined in interface X. And I'm writing some code, where I know I have an instance of X, is there a way to check if setName(name) is an option, and then call it?

1. obvious solution. extend interface X with interface XHasSetName. and have those classes that have this method implement XHasSetName. THIS SOLUTION IS NOT AN OPTION.
2. have a bunch of conditionals with instanceof. THIS IS HIDEOUS.

Anything obvious that I am missing? That doesn't require refactoring!

Thank you
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use reflection to find and execute methods on an object.
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since compiler will stop us from invoking a method which is not available, nothing to worry about it.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Johann: instanceOf does you absolutely no good, because the type has nothing to do with what you're trying to accomplish.

@anbarasu: A regular call, yes; not a reflective call.
 
Johann Dobbins
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:@Johann: instanceOf does you absolutely no good, because the type has nothing to do with what you're trying to accomplish.

@anbarasu: A regular call, yes; not a reflective call.


of course instanceof is useful, just ugly. If classes A, B, C.....Q all implement X, but only A, B, C.....M expose setName(String name), i could do:

 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do that. Or you could use reflection, as I suggested earlier.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point was that if you have an arbitrary collection of objects, some of which implement the method and some don't, then I don't see the point of using instanceOf, when there's another option available. If you're okay with just throwing an if statement, feel comfortable you'll cover all the bases, and don't mind maintaining it, knock yourself out.
 
Johann Dobbins
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:My point was that if you have an arbitrary collection of objects, some of which implement the method and some don't, then I don't see the point of using instanceOf, when there's another option available. If you're okay with just throwing an if statement, feel comfortable you'll cover all the bases, and don't mind maintaining it, knock yourself out.


sorry, i was just clarifying why i had initially stated that using instanceof was an option, just a very poor one. i do plan to use reflection, and appreciate the post recommending it.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!