java - Best way to hack the return type on an implemented method? -
i'm implementing service through maven dependency takes data , spits out @ virtual location, provided conforms strict type requirements. however, i've swapped out 1 of fields (let's call field buzz
, implementation of buzzer
) , wondering if can make work rest of service.
the service call factory requires type buzz
, diff between buzz
, buzzer
few added member fields. there hack can make factory accept buzzer
? looking through code, service not seem dependent on hashcode of buzz
being correct.
edit: examples
class buzz { public string id public string name public string title public iterable<profile> profiles public myconstructor(string id, string name, string title, iterable profile) { this.var = var; } }
and
class buzzer { public string id public string name public string title public profile profiles public myconstructor(string id, string name, string title, profile profiles) { this.var = var; } }
where profile
defined object
if can have buzzer extend buzz, ideal.
if can't (for example if 3rd party library , declared final class), hack solution "acquire" buzz instance (that depend on specific scenareo.. need more info more specific). , modify buzz property through reflection.
something this:
string buzz = null; // assuming buzz field string in example field fbuzz = null; try { class<?> cbuzz = thebuzzinstance.getclass(); fbuzz = cbuzz.getdeclaredfield( "buzz" ); fbuzz.setaccessible( true ); buzz = ( string ) fbuzz.get( thebuzzinstance ); string newbuzz = "hacked!"; fbuzz.set( thebuzzinstance, newbuzz ); } catch( exception e ) { // todo: error handling }
Comments
Post a Comment