So from reading some of the articles,themessageigotoutofitwasbeingabletomodifyfieldsandsetvaluestoclassesinrealtimewithoutrecompoutrecompilecorecomporecompompilecleretrecled
soisitpossibletodothisto3rdpartyjavalibrarycreatedclasseswhichnosourcecodeisavailable/isitpossibletousereflectiontontomodifyclayclablable
inwhatotherscenariosisreflectioncommonlyused?
iamtryingtounderstandhowreflectioncanbeapplicable。
解决方案
anytime you ' redealingwithastringatruntimeandwanttotreatpartofthatstringasanidentifierinthelanguage。
remoteprocedurecalling---treatpartofamessagereceivedoverthenetworkasamethodname。
序列化和序列化--convertfieldnamestostringsoyoucanwritetheobject ' sfieldstoastreamandlaterconvertitbackinter
对象关系映射----maintainarelationshipbetweenfieldsinanobjectandcolumnsinadatabase。
interfaceswithdynamicallytypedscriptinglanguages-- turnastringvalueproducedbyascriptinglanguageintoareferencetoafieldormethorm
itcanalsobeusedtoallowlanguagefeaturestobeemulatedinthelanguage。
considerthecommandlinejavacom.example.myclasswhichturnsastringintoaclassname.this doesn ' trequirereflection, becausethejavaexecutablecanturna.classfileintocode,butwithouldnotbeabletowritejavacom.example.wrapper code
class Wrapper {
publicstaticvoidmain (string . argv ) throws Exception { )。
//dosomeinitializationorotherwork
class delegate=class.forname (argv [0];
method main=delegate.get method (' main ',string ) ).class );
main.apply(null,Arrays.aslist ) argv ).subList(1) 1,argv.length ).toarray ) argv );
}
}