• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Simple Question

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , i have a simple question on reflection.
Let's say i have a class which has a method aMethod(int) which takes an int as a parameter.
WHen using reflection i'd like to invoke that method. The thing is the invoke method takes Object as an argument to pass to the method. I don't know how do i cast it so that i will send a correct type to my method. I can pass to it an Integer but that is incorrect type i must convert it somehow to an int anyone knows how ?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer has a method intValue
Integer myInt = new Integer(5);
int i = intValue(myInt);
 
Ruslan Ivanov
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This definatly won't work when invoking the method using this. The type of argument must be passed as an Object
Take a look at the Method class in reflection package method called invoke.(Object,Object[]);
Thanks for assistance
 
Ruslan Ivanov
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also i wouldn't want to use isInstance() or instanceof to check what type of object i am dealing with otherwise it be waist of time for me to use this. Maybe i am not just getting an idea how this works yet.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well now I think that I do not understand the question.
What's wrong with overloading the method with a version that takes an object and converts it to call the version using an int?
aMethod(Object in){
if (in instanceof Integer){
aMethod(intValue(in));
}
else whatever
}


[This message has been edited by Cindy Glass (edited February 25, 2001).]
 
Ruslan Ivanov
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a big problem when you don't know about waht type of object i am dealing with when using reflection. It might come over the networking and i must know what i am dealing with. I get the type of it and everything else. Well i just found out that i don't need to cast it , if i dont' pass a correct argument it will just trow an exception at me at runtime i might have to deal with it then, still it's very incovinient to use instanceof if i got 100's of objects , later on i might extends the program and then what would happen ? I must rewrite some stuff which is not good.
 
Ruslan Ivanov
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy , if you are interested what it looks like now and it works, here is the idea what i was after.
I have a textfile which has the methods names and the return type for getters. Now i just create my object. I have the Bill class which has methods like these setters and getters i just use it now though the reflection. It's pretty cool because i don't need to know now anything about the class.

 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic