Solved

Invocation with interface type parameters

Posted on 2001-08-20
13
308 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
ID: 6407640
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
ID: 6408026
> But I need the second one to work.

Can you explain in more detail why?
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6408737
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:dnoelpp
ID: 6409214
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
ID: 6409236
If there are more than one possibility then it won't compile unless you cast the null.
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6409279
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
 
LVL 92

Expert Comment

by:objects
ID: 6409326
> Do I need to post a question at Community Support?

I believe so.
0
 
LVL 3

Author Comment

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

Expert Comment

by:modder
ID: 6409410
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
ID: 6409547
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6417161
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
ID: 6417215
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
ID: 6417245
I found a good solution in the bsh.Reflect class of the BeanShell source. Hooray to Open Source! :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
How to convert String matching to regex in java 4 49
hibernate example for saving data 19 52
maven module vs maven project 3 46
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

856 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