Solved

Invocation with interface type parameters

Posted on 2001-08-20
13
305 Views
Last Modified: 2008-03-17
I discovered that for invocation with help of reflection I have to give the exact (interface) type for each parameter...

Example:

JFrame f = new JFrame("Example");
JPanel contentPane = f.getContentPane();
BorderLayout layout = new BorderLayout();

// here the invocation with the help of reflection
// does the same as: contentPane.setLayout(layout);
Method method = JPanel.class.getMethod("setLayout", new Class[] { LayoutManager.class });

// This doesn't work because of the exact parameter type.
Method method = JPanel.class.getMethod("setLayout", new Class[] { BorderLayout.class });

But I need the second one to work. I could check all interfaces for all parameters, but then for two parameters I have to check them crosswise, and for three parameters I hava a cube to check, etc. Do you understand?

Any idea to avoid this type of checking?
0
Comment
Question by:dnoelpp
13 Comments
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
Comment Utility
Hi,

Try to get all methods and filter them first by name than by parameter types:

Method[] methods = JPanel.class.getMethods();
Class expectedParams = new Class[] { BorderLayout.class };
for( int i=0; i<methods.length; ++i){
    if( !"setLayout".equals(methods[i].getName()) ){
         continue;
    }
    Class[] params = getParameterTypes();
    if( params.length != expectedParams.length ){
          continue;
    }
    boolean OK = true;
    for(int j=0; j<params.length; ++j){
          if( !params[j].isAssignableFrom(expectedParams[j])){
               OK = false;
                break;
          }
    }
    if(OK){
         return methods[i];
    }
}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> But I need the second one to work.

Can you explain in more detail why?
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
Yes, bazarny I thought about a solution like yours. But I am looking for a general solution (for a scriping language for constructing complicated beans*) and I am afraid about scalability... Imagine, you have a complicated method with many parameters, and all of them are interfaces, and you give it parameters which implement many interfaces...

Does someone know why reflection is implemented this way? I find it complicated that one has to find out the correct interface type and it's even complicated with several parameters.

* sort of a properties file with some extras added
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
Or, imagine, if some of the parameters are null... How are null parameters matched to actual parameter types in invoking methods or constructors?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
If there are more than one possibility then it won't compile unless you cast the null.
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
Yes, you're right... I program for 4 years with Java and I didn't know about this piece of a subtle problem... :-)

Okay, I would like to split this question between bazarny and objects. Do I need to post a question at Community Support?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
Comment Utility
> Do I need to post a question at Community Support?

I believe so.
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
I've put a request at Community Support (Q 20171567).
0
 
LVL 3

Expert Comment

by:modder
Comment Utility
dnoelpp,

OK. I've reduced the points on this question to 100
You can now award the question to one of the experts that you want to give points.

The points I refunded to you you can use to post dummy questions. Just title them
"Points for <expert ID>".

In the question body, just state:
"For your help with
http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=20171101 "

Once you've created the dummy question, you should find out what it's URL is. To do that, click on the "Other Questions" link at the top of this page, which brings you to the main page for this topic area. Your new dummy question will be listed at the top. Then, click on the dummy question to find out its URL, which will look a bit like this:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=NAME_OF_TOPIC_AREA_HERE&qid=XXXXXXXXXXX (a number)

Finally, come back to this question and post a comment saying "Posted dummy question for <Expert ID>", you can find it here: <url of dummy question>

Thanks for your cooperation

modder
Community Support
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
As an afterthought...

bazarny, your example still doesn't solve the problem of widening conversions of primitives.

Say, a method accepts an int parameter, you can pass it a short or a char parameter. This case is not handled by your example... :-)

I found a very thoughtful article at Java Report:

http://www.javareport.com/html/from_pages/article.asp?id=4276&mon=8&yr=2001

Please, if you are intrigued, tell me your comments about the problem.
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
Note that the proposed solution BetterMethodFinder still has a limitation: It looks only for defined methods in the class and not for inherited methods!

Example:

class Alpha {
    public void a() {}
}
class Beta {
}

The method finder will not find the inherited method a() on an instance of the class Beta.

Again, any comments?
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
I found a good solution in the bsh.Reflect class of the BeanShell source. Hooray to Open Source! :-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 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