-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Help me. Java Reflection problem
PostPosted: Thu Jan 01, 2009 4:07 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
"Here I want to invoke all methods of newUser object inside ReflectionUtil Class (Actually all 'get' methods)".

===========================================
RefletctionUtil util = new RefletctionUtil();

User newUser = (User)myDb.hibSelect(tblObj, tblObj.getUser_id());

String methodNames = (String) util.getAllMethods(newUser);
===========================================

* Above Code snippet is OK, Problem is inside 'ReflectionUtil Class'

===========================================
public class RefletctionUtil
{
public Object getAllMethods(User obj)
{
//to get all attributes and behaviors of this object
Method[] declaredMethods = obj.getClass().getDeclaredMethods();
Field[] declaredFields = obj.getClass().getDeclaredFields();

Object arr = "";
//iterate through the fields.
for(Field field:declaredFields)
{
try {
//while iterating call getMethodName method for get relavant get method for particular attribute.
arr = arr+field.getName()+"::=::"+getMethodName(field.getName(), obj)+";\n";
} catch (IllegalArgumentException ex) {
Logger.getLogger(RefletctionUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
return arr;


}

public Object getMethodName(String name, Object obj)
{
try {
String prefix = "get";
String methodName = "";
Class myClass = obj.getClass();

String tempName = name;

tempName = Character.toUpperCase(tempName.charAt(0)) + tempName.substring(1);
methodName = prefix + tempName;

Method classMethod = myClass.getMethod(methodName);
System.out.println(myClass.toString());
return classMethod.invoke(myClass);

} catch (IllegalAccessException ex) {
Logger.getLogger(RefletctionUtil.class.getName()).log(Level.SEVERE, null, ex);
return "IllegalAccessException";
} catch (IllegalArgumentException ex) {
Logger.getLogger(RefletctionUtil.class.getName()).log(Level.SEVERE, null, ex);
return "IllegalArgumentException";
} catch (InvocationTargetException ex) {
Logger.getLogger(RefletctionUtil.class.getName()).log(Level.SEVERE, null, ex);
return "InvocationTargetException";
} catch (NoSuchMethodException ex) {
Logger.getLogger(RefletctionUtil.class.getName()).log(Level.SEVERE, null, ex);
return "NoSuchMethodException";
} catch (SecurityException ex) {
Logger.getLogger(RefletctionUtil.class.getName()).log(Level.SEVERE, null, ex);
return "SecurityException";
}
}
}

==========================================

**'return classMethod.invoke(myClass); ' line returns an exception
a 'IllegalArgumentException'

**And my IDE shows 'java.lang.IllegalArgumentException: object is not an instance of declaring class'

**But methods all are members of 'User' class. So I dont know why this exception is?

Im stuck with this problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2009 3:09 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Shouldn't you call classMethod.invoke(obj);?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 12:10 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
I called it, you can see it in the above code like.

'return classMethod.invokde'


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 1:49 am 
Beginner
Beginner

Joined: Wed Nov 26, 2008 1:02 am
Posts: 22
hi,

just check it whether the invoke method is having one argument method or not? i think it is having 2 arguments..


hope it solves,dont forget to rate me,

regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 10:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I called it, you can see it in the above code like.

'return classMethod.invokde'


Yes.... but with 'myClass' as the argument and not 'obj'. I guess it is the 'obj' object that has a getter method not the 'myClass' object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.