import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import com.sun.corba.se.impl.orbutil.GetPropertyAction;
import com.sun.jmx.mbeanserver.Introspector;
import reflect.MethodDemo;
public class IntroSpectorTest {
/**
* @param args
* @throws IntrospectionException
* @throws Exception
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws Exception {
ReflectPoint pt1=new ReflectPoint(3,5);
String propertyName="x";
//"x"--->"X"-->"getX"-->MethodGetX-->
Object retVal=getProperty(pt1,propertyName);
System.out.println(retVal);
Object value=7;
setProperties(pt1, propertyName, value);
System.out.println( pt1.getX());
}
private static void setProperties(Object pt1, String propertyName, Object value)
throws Exception {
PropertyDescriptor pd2=new PropertyDescriptor(propertyName,pt1.getClass());
Method methodSetX=pd2.getWriteMethod();
methodSetX.invoke(pt1,value);
}
private static Object getProperty(Object pt1, String propertyName) throws Exception {
// PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
/*Method methodGetX=pd.getReadMethod();
Object retVal=methodGetX.invoke(pt1);
return retVal;*/
BeanInfo beanInfo=Introspector.getBeanInfo(pt1.getClass());
PropertyDescriptor []pds=beanInfo.getPropertyDescriptors();
Object retVal=null;
for (PropertyDescriptor pd : pds) {
if(pd.getName().equals(propertyName)){
Method methodGetX = pd.getReadMethod();
retVal=methodGetX.invoke(pt1);
break;
}
}
return retVal;
}
}
getBeanInfo报错,这是为什么?
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getBeanInfo(Class<capture#2-of ?>) is undefined for the type Introspector
at IntroSpectorTest.getProperty(IntroSpectorTest.java:48)
at IntroSpectorTest.main(IntroSpectorTest.java:28)
怎么解决这个问题。。。 |
|