• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method class from Reflect , class cast exception for cast method  RSS feed

 
Indrayani Godbole
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am using Method class of reflect package.
There is a method in Method class, which returns parametertypes.
I want to cast a String value as per the returned parameter type.
for Ex, please check below code




I am getting class cast exception.

can anybody tell, where i am going wrong
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you didn't you post the entire error message (please do that in the future) -which has more information about why the cast was unsuccessful- I'd assume that "ptype" is something to which a String can't be cast.

But there's something a bit odd about this question; can you tell us why you are trying this?

As an aside, casting "columnValue" to Object accomplishes nothing - every object in Java is already an instance of Object.

Also note that assigning the result of the cast to an "Object" reference makes no sense - it basically throws away the cast you wanted to accomplish.
 
Indrayani Godbole
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually what i wanted to do is ,

1. one csv column is read and its value is stored in String variable
2. now there is a setter method in pojo class
3. the setter method to be used is identified by matching its name with the column header in CSV file. this is done using reflection in java
4. now i am ready with a setter method and a value to be set in the method.
5. since the method is identified on run time, i am not aware of the arguments that it is taking, i just know that, since it is a setter method, it is taking only one argument. but datatype of argument is not know.
6.so what i am doing is, i am fetching datatype of method arguments using following code :



7. here now, ptype variable tells me the datatype of argument.
8. now i want to invoke the setter method by passing the columnvalue as an argument to it.
9. but before i invoke the setter method, i need to cast the column value to a datatype that the method accepts, that is why i am doing the following :

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't make sense. A cast is not a type conversion. Since the object you have is a String, the only classes you could cast it to are Object and String - neither of which makes sense (everything is an Object anyway, and you already have a String).

It sounds like you need a much more elaborate type conversion routine that can convert the string into whatever data type the setter takes. Or, much better, refactor those POJO classes so that they can accept String objects. Presumably those objects would know how to convert a string into whatever data type they actually represent.

And on a more fundamental level, you need to be clear about the difference between a cast and a conversion.
 
Indrayani Godbole
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhhh okk.
that was really a valuable reply.
i believe, i really need to go through the concepts of Cast and Conversion, will definitely check the same.

Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!