Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Invocation with interface type parameters

Posted on 2001-08-20
13
Medium Priority
?
314 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

598 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