Java reflection technology application-reflectutil

Source: Internet
Author: User

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.