javabeans - getting a property of a java bean of unknown class -


I would like to be able to call the "getprogram" call on the object without which the class of which they belong. I know I should use an interface here, but I am working with someone else's code and can not redefine the sections I am working with. I thought BeanUtils.getProperty could help me, but it seems that it only gives the wire what are some things like beetilils that can return a cast-object? Alternatively, a smart way to work with two equal classes, which do not share the interface? Thank you, -Morgan

You probably have a limited number of classes that implement this method, and You can link it directly to them so you do not need reflection, reflection is bad.

Say a set of classes with the method:

  public class LibA {public program getProgram () {return program; } ...}; Public class LibB {public program getProgram () {return program; } ...}; ...  

Then you just need an easiest / cast pair. You can put it in a method so that you only need to do this once.

  Get public stable program (object object) {if (Uzas instance of LibA) {return (LibA) obj) .getProgram (); } And if (obj instanceof LibB) {return (obb) .getProgram (); } Other {adopting new invalid records (obj + "Getting known is not a program"); // or a suitable application exception}}  

Alternatively you may want to use an adapter:

  public interface program container {program getprogram (); ...} Implementation of the public class LibAContainer ProgramContainer {Private Final LibA libA; Public LibertyEner (Liba Liba) {this.libA = libA; } Public program getProgram () {back libA.getProgram (); } ...}  

Comments