sudhakar_koundinya
asked on
Regarding Java Reflection API
Hello All,
I am newbie to java reflection API. could u please give me some valuable pointers to understand them??
for example
class Test
{
public static void main(String s[])
{
test(new String("Hi there"));
}
static void test(Object o)
{
//at this pointer I don't know what are the data and method members of that object
// I can go for instanceof but the problem is at runtime if i have multiple classes then it would be problem for
// writing such codes as it deals with multiple if and else conditions
//If I go with object.getClass() that will be faster for me. But my problem is
// how to work with data and method members of object
//I know I have method called class.getMethods() in Class
//But when it comes to runtime I am not understaindg How to execute that runtime methods.
// in otherwords I wanted to have following type of example for an example
// String str=(String)(class.getMeth ods()[0]). substring( 0,10);
}
}
I hope you understand my query
thanks
Sudhakar
I am newbie to java reflection API. could u please give me some valuable pointers to understand them??
for example
class Test
{
public static void main(String s[])
{
test(new String("Hi there"));
}
static void test(Object o)
{
//at this pointer I don't know what are the data and method members of that object
// I can go for instanceof but the problem is at runtime if i have multiple classes then it would be problem for
// writing such codes as it deals with multiple if and else conditions
//If I go with object.getClass() that will be faster for me. But my problem is
// how to work with data and method members of object
//I know I have method called class.getMethods() in Class
//But when it comes to runtime I am not understaindg How to execute that runtime methods.
// in otherwords I wanted to have following type of example for an example
// String str=(String)(class.getMeth
}
}
I hope you understand my query
thanks
Sudhakar
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
> I can go for instanceof but the problem is at runtime if i have multiple classes then it would be > problem for writing such codes as it deals with multiple if and else conditions
Reflection is not really going to help you there ( or perhaps you need to explain the big picture).
Sounds like you'd be better off having your objects implement an interface that defines the methods you need to call so you don't need to worry about if/else.
Reflection is not really going to help you there ( or perhaps you need to explain the big picture).
Sounds like you'd be better off having your objects implement an interface that defines the methods you need to call so you don't need to worry about if/else.
have a look at reflection api. good place to start.
run time you need to exact names.
run time you need to exact names.
you could use the getMethods() to get all the methods for the class but then you'll still need to loop thru them all to see if what you are after is availble.
to call it you'd then use:
Method m = ...
Object result = m.invoke(o, args);
to call it you'd then use:
Method m = ...
Object result = m.invoke(o, args);
>>String str=(String)(class.getMeth ods()[0]). substring( 0,10);
What would you be attempting to do there?
What would you be attempting to do there?
ASKER
I am just looking at this
String result = (String) theMethods[0].invoke
(requestParam, null);
How does this help me
String result = (String) theMethods[0].invoke
(requestParam, null);
How does this help me
typo,
run time you need not to know names or methods.
run time you need not to know names or methods.
You invoke the Method, not a String
ASKER
>>What would you be attempting to do there?
That is just Pseudo . I am trying to execute runtime methods
That is just Pseudo . I am trying to execute runtime methods
> String result = (String) theMethods[0].invoke(reque stParam, null);
> How does this help me
allows you to call method (at runtime).
from what you've said so far, I'm not sure if it does help you.
I guess if you wanted to call specific method on an object if it exists (regardless of class) then thats the way to go.
> How does this help me
allows you to call method (at runtime).
from what you've said so far, I'm not sure if it does help you.
I guess if you wanted to call specific method on an object if it exists (regardless of class) then thats the way to go.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks all of you guys
I understand what I need to do now :-)
I understand what I need to do now :-)
:)
8-)