Solved

Invocation with interface type parameters

Posted on 2001-08-20
13
309 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 73
Running JavaFX on JDeveloper 12C 1 76
ejb wildfly example 2 72
program arguments vs VM arguments 4 28
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

749 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