How! ! Kind of "introspection" way to change a property value javabean

Category: Java SE
 
teng0721
2010-06-06 04:49:39

Sponsored Links
I now want to modify the current bean, and his father the value of all the properties and make changes, if introspective approach should be how to achieve ah ~!
My current practice is to use the following code!
BeanInfo info = Introspector.getBeanInfo (beanClass, stopClass);
PropertyDescriptor [] propertys = info.getPropertyDescriptors ();
but got property After that how to modify the value of ah if he is getReadMethod () method to get the bean's get method, but how to use invoke to call this method ah? ?

Sponsored Links

AFAFAFAFAF
2010-06-06 04:54:50
Class cls = Class.forName (MyClsName);
; Method mtd = cls.getMethod (MyMethodName, new Class [] {String.class});
Object obj = (Object) cls.newInstance ();
mtd.invoke (obj, new Object [] {MyValue});

geluin
2010-06-06 05:00:08
MyClsName, MyMethodName , MyValue are your own class, method name, parameter values ​​
renxukuan
2010-06-06 05:09:10
Upstairs This method can only get their own class methods and properties that I have to do now is the base class, and his father The methods and properties, but do not include all of the parent class.
can be used
BeanInfo info = Introspector.getBeanInfo (beanClass, stopClass);
to specify a range, but so that no one contains all the properties and methods of these classes , you invoke when the call can not get that object ah! ! And your the newInstance () method which class is how I did not find ah ah
ccmslcccc
2010-06-06 05:24:46
Oh, newInstance () to find, but he is to call the original class or a new class re New ah, this is not cause significant overhead ah, but if I was a constructor parameter can also be used so what?
wretchlong
2010-06-06 05:26:50
continue to focus on! ~ ~ ~! !
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.