黑马程序员技术交流社区
标题:
帮忙看一下??哪里出了问题
[打印本页]
作者:
回首、后_街
时间:
2013-5-7 13:08
标题:
帮忙看一下??哪里出了问题
本帖最后由 回首、后_街 于 2013-5-9 20:25 编辑
IntrospectorDemo demo = new IntrospectorDemo();
demo.setName("张三");
BeanInfo bi = Introspector.getBeanInfo(demo.getClass(),Object.class);
PropertyDescriptor[] props = bi.getPropertyDescriptors();
for(int i=0;i<props.length;i++){
System.out.println(props
.getName()+"="+props
.getReadMethod().invoke(demo,null));
}
运行该程序出现该异常Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.oop
不知道哪里出了问题???
作者:
蔡增辉
时间:
2013-5-7 13:44
本帖最后由 蔡增辉 于 2013-5-7 14:53 编辑
"Prohibited package name",是不是包名出现了问题
作者:
Sword
时间:
2013-5-7 14:18
你可以把详细的点贴出来,只有一段代码的话,这样很难判断哪里出错了
作者:
花开花落总相似
时间:
2013-5-7 14:35
看不出来 代码不全没法运行
作者:
回首、后_街
时间:
2013-5-7 14:41
花开花落总相似 发表于 2013-5-7 14:35
看不出来 代码不全没法运行
package java.oop;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
public class Test {
/**
* @param args
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
IntrospectorDemo demo = new IntrospectorDemo();
demo.setName("张三");
BeanInfo bi = Introspector.getBeanInfo(demo.getClass(),Object.class);
PropertyDescriptor[] props = bi.getPropertyDescriptors();
for(int i=0;i<props.length;i++){
System.out.println(props
.getName()+"="+props
.getReadMethod().invoke(demo,null));
}
}
IntrospectorDemo demo = new IntrospectorDemo(); //是一个类 里面的属性有
public class IntrospectorDemo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
作者:
DMY2008
时间:
2013-5-8 15:23
你这个代码、、怎么看不出来你想做啥子呢!
作者:
Super_Class
时间:
2013-5-8 22:50
属性描述符不是这样用的。
|--
5、IntroSpector 内省
IntrosPector 是用来操作JavaBean类的;
所谓的javabean类其实是一个特殊的java类含有get和set方法的类。
三种方法:
1、使用PropertyDescriptor 属性描述符
PropertyDescriptor pd = new PropertyDescriptor(String propertyName,Class BeanClass);
getX()
Method getMethod = pd.getReadMethod();
set(类型 对象);
Method setMethod = pd.getWriteMethod();
2、使用Introspector 内省
BeanInfo beanInfo = Introspector.getBeanInfo(字节码比如:ReflectPoint.class);
再得到所有的属性描述符
PropertyDescriptor [] pds = beanInfo.getPropertyDescriptors();
然后可以进行迭代获得每个字段的值(使用get方法或使用set方法修改)
3、直接使用BeanUtils类(静态方法)
BeanUtils.setProperty(,,);
BeanUtils.getProperty();
作者:
刘胜寒
时间:
2013-5-9 10:14
问题如果以解决,请及时修改分类,否则继续提问问,谢谢合作!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2