Solved

Reflection from a Hashtable

Posted on 2006-11-28
4
216 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

828 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