Solved

Reflection from a Hashtable

Posted on 2006-11-28
4
213 Views
Last Modified: 2010-03-31
So I'm pretty green to java still. What I have is a Hashtable that I have loaded with objects of my own classes.

I have put trace lines along with exception handling and I can see that the classes are successfully instantiated and loaded into the Hashtable and the Hashtable's size grows.

Once these objects are all created I need to then access them individually and  invoke their methods dynamically.  I have created all of my classes with the same method names and signatures so I can expect that each object in the hashtable has a method called set_filesource() for example. But these objects are not of the same class and do not implement any sort of interface.

The problem is I am not undrstanding Reflection quite yet nor do I grasp implementing Generics quite so well. So here's what I have right now. I can get this to compile fine because it's not checking the fact that the objects I load into the Hashtable are not of type Class. So when I run what's below I get a java.lang.ClassCastException: MyClass since the object in the Hashtable is of type MyClass not Class.

I'm wondering if I need to genericly cast my objects when I put them in the Hashtable or is there a way to generically cast the objects coming out of the Hashtable (which is what I'm attemptign here)? Otherwise is there a better way to dynamically invoke methods (with arguments) on objects of different classes in a Hashtable?


    private void executeMethod(String arg) {

        Class cl = (Class)MyHashtable.get(this.dataTarget);
        Object[] argObject = new Object[] {arg};
        String targetName = "set_"+methodTarget;

        try {  
            Method method = cl.getMethod(targetName,new Class[]{String.class});
            method.invoke(cl,argObject);
        }      
        catch (NoSuchMethodException ex) {
            // method does not exist
        }      
        catch (Exception ex) {
            ex.printStackTrace();
        }      

    }


Thanks for any help!
0
Comment
Question by:bmarshallbri
  • 2
  • 2
4 Comments
 

Author Comment

by:bmarshallbri
ID: 18034865
Never mind. I simplified and just created an interface so I didn't have to cast so generically.
0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 400 total points
ID: 18034935

>> Class cl = (Class)MyHashtable.get(this.dataTarget);

should be , Object cl = MyHashtable.get(this.dataTarget);

and,
 Method method = cl.getClass().getMethod(targetName,new Class[]{String.class});
method.invoke(cl,argObject);

BTW,, if you have a common interface then why you want reflection,

you can call,

MyInterface m = ( MyINterface) Mytable.get ( "jksad" ) ;
m.youyMethid();




0
 

Author Comment

by:bmarshallbri
ID: 18038556
I have not tried it bu that makes sense.

I still need to call methods by name so when I implemented the interface on the objects and cast them like

MyClass cl = (MyClass)MyHashtable.get(this.dataTarget);

The following then worked.

Method method = cl.getClass().getMethod(targetName,new Class[]{String.class});
method.invoke(cl,argObject);


So I just went back and recast it as a generic per your example and it worked like a charm. So my problem was I was casting as Class instead of Object.

Thanks so much!!
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 18043315
:)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now