?
Solved

Invocation with interface type parameters

Posted on 2001-08-20
13
Medium Priority
?
313 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 400 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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
Course of the Month14 days, 2 hours left to enroll

800 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