[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reflection from a Hashtable

Posted on 2006-11-28
4
Medium Priority
?
221 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

649 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