Public class reflectutil {
// Case insensitive
Public static object setfieldvaluedefault (object target, string fname,
Class FTYPE, object fvalue ){
If (target = NULL
| Fname = NULL
| "". Equals (fname)
| (Fvalue! = NULL &&! FTYPE
. Isassignablefrom (fvalue. getclass ()))){
Return target;
}
Class clazz = target. getclass ();
Field [] FS = clazz. getdeclaredfields ();
Try {
For (INT I = 0; I <fs. length; I ++ ){
If (fname. tolowercase (). Equals (FS [I]. getname (). tolowercase ())){
Method method = clazz. getdeclaredmethod ("set"
+ Character. touppercase (FS [I]. getname (). charat (0 ))
+ FS [I]. getname (). substring (1), String. Class );
If (! Modifier. ispublic (method. getmodifiers ())){
Method. setaccessible (true );
}
Method. Invoke (target, fvalue );
}
}
} Catch (exception me ){
Try {
Field field = clazz. getdeclaredfield (fname );
If (! Modifier. ispublic (field. getmodifiers ())){
Field. setaccessible (true );
}
Field. Set (target, fvalue );
} Catch (exception Fe ){
Fe. getstacktrace ();
}
}
Return target;
}
// Case sensitive
Public static object setfieldvalue (object target, string fname, class FTYPE,
Object fvalue ){
If (target = NULL
| Fname = NULL
| "". Equals (fname)
| (Fvalue! = NULL &&! FTYPE
. Isassignablefrom (fvalue. getclass ()))){
Return target;
}
Class clazz = target. getclass ();
Field [] FS = clazz. getdeclaredfields ();
Try {
Method method = clazz. getdeclaredmethod ("set"
+ Character. touppercase (fname. charat (0 ))
+ Fname. substring (1), String. Class );
If (! Modifier. ispublic (method. getmodifiers ())){
Method. setaccessible (true );
}
Method. Invoke (target, fvalue );
} Catch (exception me ){
Try {
Field field = clazz. getdeclaredfield (fname );
If (! Modifier. ispublic (field. getmodifiers ())){
Field. setaccessible (true );
}
Field. Set (target, fvalue );
} Catch (exception Fe ){
Fe. getstacktrace ();
}
}
Return target;
}
Public static object getfieldvalue (object target, string fname, class FTYPE ){
If (target = NULL | fname = NULL | "". Equals (fname )){
Return NULL;
}
Class clazz = target. getclass ();
Try {
Method method = clazz. getdeclaredmethod ("get"
+ Character. touppercase (fname. charat (0 ))
+ Fname. substring (1), FTYPE );
If (! Modifier. ispublic (method. getmodifiers ())){
Method. setaccessible (true );
}
Return method. Invoke (target );
} Catch (exception me ){
Try {
Field field = clazz. getdeclaredfield (fname );
If (! Modifier. ispublic (field. getmodifiers ())){
Field. setaccessible (true );
}
Return field. Get (target );
} Catch (exception Fe ){
Fe. getstacktrace ();
}
}
Return NULL;
}
}