反射的问题。。。。请大家帮忙

creazywind 2008-06-05 03:01:26
我有个问题想问下,我用反射得到了运行时类的某个属性值,如当admin添加一个user的时候我可以通过反射调用getAcctName()得到user的名字,
我想问怎么用这个用户名的值来构造一个这个类的新实例???我自己想的是调用它的setAcctName()方法把刚才得到的用户名给
赋值进去,但是又怎么确定这个方法名呢?通过反射得到的类能这样做吗???

public void save(Object obj) throws Exception, NoSuchMethodException {

if(obj==null){
return;
}
//cast to bussnessBean
FrameValidatorForm bean=(FrameValidatorForm)obj;
String property="";
//get unique property names
Set uniqueProperties = bean.getUniqueProperties();
if(uniqueProperties.isEmpty()){

}

else{
Iterator iter = uniqueProperties.iterator();
Class bussnessBean=bean.getClass();
while(iter.hasNext()){
property=(String) iter.next();


String methodName = "get" + property.substring(0,1).toUpperCase() + property.substring(1);
Method m = bussnessBean.getDeclaredMethod(methodName,null);
//得到用户名
String propValue = (String)m.invoke(obj,null);

}

Object queryObj=this.getObjectByProperty(instance);
if(queryObj==null){
System.out.println("$$$$$$$$$$$$$");
}
}

//save object to database.
this.sessionFactory.getCurrentSession().save(obj);
}
...全文
146 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
胡矣 2008-06-06
  • 打赏
  • 举报
回复
哦 恭喜 解决了就分享一下哈 :-)
chenzenan 2008-06-06
  • 打赏
  • 举报
回复
//获得对象的类型
Class classType=object.getClass();
//获得对象的所有属性
Field fields[]=classType.getDeclaredFields();
for(int i=0; i<fields.length;i++){
Field field=fields[i];
String fieldName=field.getName();
String firstLetter=fieldName.substring(0,1).toUpperCase();
//获得和属性对应的getXXX()方法的名字
String getMethodName="get"+firstLetter+fieldName.substring(1);
//获得和属性对应的setXXX()方法的名字
String setMethodName="set"+firstLetter+fieldName.substring(1);
//获得和属性对应的getXXX()方法
Method getMethod=classType.getMethod(getMethodName,new Class[]{});
//获得和属性对应的setXXX()方法
Method setMethod=classType.getMethod(setMethodName,new Class[]{field.getType()});
//调用原对象的getXXX()方法
Object value=getMethod.invoke(object,new Object[]{});
System.out.println(fieldName+":"+value);
}
fulianglove 2008-06-06
  • 打赏
  • 举报
回复
你怎么算出get方法的就怎么算出set方法被
还可以使用Apatch的BeanUtils公共类,里面有这种javabean的装配各种方法
creazywind 2008-06-06
  • 打赏
  • 举报
回复
呵呵,最后实在没办法了就一个一个的找到我要的set方法,然后把参数传进去 最后构造好对象然后调用其它的方法进行数据库的查询 这是我的那个方法
private boolean checkDuplicate(Object obj) throws Exception, NoSuchMethodException{

//cast to bussnessBean
FrameValidatorForm bean=(FrameValidatorForm)obj;
Class c=bean.getClass();
String property="";

//get unique property names
Set uniqueProperties = bean.getUniqueProperties();

if(uniqueProperties==null){
return false;
}

FrameValidatorForm newInstance = (FrameValidatorForm) c.newInstance();
Object [] objectList = new Object[1];

Iterator iter = uniqueProperties.iterator();
while(iter.hasNext()){

//get property name
property=(String)iter.next();
String getMethod = "get" + property.substring(0,1).toUpperCase() + property.substring(1);
Method m = c.getDeclaredMethod(getMethod,null);
Object value = m.invoke(bean,null);

objectList[0] = value;

Method [] methods = c.getDeclaredMethods();
for(int j=0;j<methods.length;j++){

//find the corresponding set method
if(methods[j].getName().equalsIgnoreCase("set"+property)){
//invoke the set method
methods[j].invoke(newInstance,objectList);
}
}
}

Object result=this.getObjectByProperty(newInstance);
if(result!=null){
return true;
}
return false;
}
jdlsfl 2008-06-05
  • 打赏
  • 举报
回复
String methodName = "set" + property.substring(0,1).toUpperCase() + property.substring(1);
Method m = bussnessBean.getDeclaredMethod(methodName,null);
m.invoke(obj,propValue);
creazywind 2008-06-05
  • 打赏
  • 举报
回复
呵呵 问题解决了 很感谢大家。。。
jdlsfl 2008-06-05
  • 打赏
  • 举报
回复
不需要构造新对象,只要用反射得到的对象进行操作就可以了
你知道方法的名字,直接调用就可以了,带参数的
creazywind 2008-06-05
  • 打赏
  • 举报
回复
setAcctName() 肯定有,因为这个操作的下一步是getObjectByProperty(Object obj)传进去一个对象然后看数据库里能不能找到记录。如果我直接传刚才取到的那个实例查询语句就会把所有的属性都给加上 select
fwgroup0_.GROUP_ID as GROUP1_6_0_,
fwgroup0_.GROUP_NAME as GROUP2_6_0_,
fwgroup0_.STATUS as STATUS6_0_,
fwgroup0_.GROUP_TYPE as GROUP4_6_0_,
fwgroup0_.DESCRIPTION as DESCRIPT5_6_0_,
fwgroup0_.PARENT_ID as PARENT6_6_0_
from
FW_GROUP fwgroup0_
where
fwgroup0_.GROUP_ID in (
?, ?, ?
)

所以我就需要一个新对象 只需要有一个值,因为我是要判断这一个值是否在数据库里已经有了,因为这个是一个开发框架所以有些东西也不能该 只能用别人写好的方法getObjectByProperty(Object obj)
sgdb 2008-06-05
  • 打赏
  • 举报
回复
既然你的类里能得到AcctName这个属性,你为什么又要重新用user name去构造一个新类呢?你要是实在想要,你可以通过getDeclaredMethods()把这个类的所有方法都列出来,看看有没有setAcctName()
creazywind 2008-06-05
  • 打赏
  • 举报
回复
是我没描述清吗?
interpb 2008-06-05
  • 打赏
  • 举报
回复
不是很懂 呵呵

67,537

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧