?
Solved

Reflection from a Hashtable

Posted on 2006-11-28
4
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 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