I’d like to be able to call ‘getProgram’ on objects which have that method, without knowing which class they belong to. I know I should use an interface here, but I’m working with someone else’s code and can’t redesign the classes I’m working with. I thought BeanUtils.getProperty might help me, but it seems it only returns strings. Is there something like Beanutils.getProperty that will return a cast-able object? Or another, smarter way to work with two similar classes that don’t share an interface? thanks, -Morgan
Share
Presumably you have a finite number of classes implementing this method, and you can link to them directly. So you don’t need reflection. Reflection is evil.
Say you have a set of classes with the method:
Then you just need instanceof/cast pairs. You can put this in a method so that you only need to do it once.
Alternatively you might want to use an adapter: