This is through the reflection mechanism of Java to avoid repeated writing similar call Methods
- // Set data through the Reflection Method
- For (INT I = 0; I <grade_num; I ++ ){
- Method1 = getvaluemethod. setmethod (New fr0313dto (),
- "Minoritystunum" + (I + 1 ),
- New Class [] {string. Class });
- Method2 = getvaluemethod. setmethod (New fr0313dto (),
- "Girlstunum" + (I + 1 ),
- New Class [] {string. Class });
- Method3 = getvaluemethod. setmethod (New fr0313dto (),
- "Stunum" + (I + 1), new class [] {string. Class });
- Try {
- Method1.invoke (DTO, new object [] {string
- . Valueof (minoritystunum )});
- Method2.invoke (DTO, new object [] {string
- . Valueof (getintvalue (acadgirlmap
- . Get (gradeacadyear [I])});
- Method3.invoke (DTO, new object [] {string
- . Valueof (getintvalue (acadstumap
- . Get (gradeacadyear [I])});
- } Catch (illegalargumentexception e ){
- Log. Error (E );
- } Catch (illegalaccessexception e ){
- Log. Error (E );
- } Catch (invocationtargetexception e ){
- Log. Error (E );
- }
- }
The following describes how to assemble the set method. setmethod in getvaluemethod
- Public static method setmethod (Object object, string propertyname,
- Class [] partypes ){
- If (propertyname = NULL | propertyname. Length () = 0 ){
- Return NULL;
- }
- String propertynameupper = propertyname. substring (0, 1). touppercase ()
- + Propertyname. substring (1 );
- String methodname = "set" + propertynameupper;
- Method method = NULL;
- Try {
- Method = object. getclass (). getmethod (methodname, partypes );
- } Catch (securityexception e ){
- E. printstacktrace ();
- } Catch (nosuchmethodexception e ){
- E. printstacktrace ();
- }
- Return method;
- }