• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting values from a method Using Reflection  RSS feed

 
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class object and there are 2 getters in that with values. For some reason am getting classcastexception(weblogic class loader issue(not able to cast)). Is there any way I can get the values using reflection API or some other API. Please check.

I tried all the possibilities. Now only way is I have to get the values without type casting the class object.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I totally follow your post but to just answer the question: Yes, you can call a method which returns a value from Reflection, and you can gain access to those returned values. See this part of the tutorial for details: http://docs.oracle.com/javase/tutorial/reflect/, and perhaps more specifically http://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html.

But I think there might be more to your question: When do you get a ClassCastException? Can you show us some code that causes the ClassCastException and be more specific about precisely what you want to do?
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return type of the method which am calling is String. field.get(obj) is giving illegalAccess Exception. Is there any limitation ? Please help.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like an example of the XY problem: you have some problem X (you're getting a ClassCastException), and you think that solution Y (reflection) will solve it. So you ask how to apply solution Y, without explaining what your problem X really is.

So, can you show us your code and explain exactly where you're getting a ClassCastException and what exactly the exception message is? Maybe we can then help you solve problem X, instead of giving you some tips for the wrong solution Y. Reflection is not magically going to help you bypass a ClassCastException.

In your last post you're mentioning IllegalAccessException. So is it ClassCastException or IllegalAccessException? Or are you having two problems at the same time?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!