Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 515367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:37:15+00:00 2026-05-13T07:37:15+00:00

I want to use Class.getMethod(String name, Class… parameterTypes) to find the method I need

  • 0

I want to use

Class.getMethod(String name, Class... parameterTypes)

to find the method I need to invoke with the given parameters, but apparently as described in Bug 6176992 Java doesn’t include autoboxing there. So if my reflected class has a method with a (String, int) signature you still get a NoSuchMethodException with a {String.class, Integer.class} array as a paremeter.

Is there any sollution for this? The only way I could think of to call getMethod() with every permutation of primitive and non primitive types which I don’t really want to do.

Edit: To make it more clear: I am well aware of the primitive type classes, but I don’t see how they could help to solve my problem. My parameterTypes array comes from somewhere and I know that it will only return non primitive types. I can not assume that the interface will only be declared with primitive types and that’s exactly my problem:

public interface TestInterface()
{
    public void doTest(Integer i1, int i2, double d3, Double d);
}

Class<?>[] classes = { Integer.class, Integer.class, Double.class, Double.class }
// Due to autoboxing I should become the doTest method here, but it doesn't work
TestInterface.class.getMethod("doTest", classes);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:37:16+00:00Added an answer on May 13, 2026 at 7:37 am

    As @Stephen C mentions, your only hope is to do the search yourself. All of his caveats hold but I’d argue a little flexibility would go a long way to covering most of the gotchas as long as the callers were aware of the caveats… versus making your callers always be painfully specific.

    For code that actually does something like this you can look here:
    http://meta-jb.svn.sourceforge.net/viewvc/meta-jb/trunk/dev/src/main/java/org/progeeks/util/MethodIndex.java?revision=3811&view=markup

    The findMethod() call is the entry point but it delegates (after some caching, etc.) to this method:

    private Method searchForMethod( String name, Class[] parms ) {
        Method[] methods = type.getMethods();
        for( int i = 0; i < methods.length; i++ ) {
            // Has to be named the same of course.
            if( !methods[i].getName().equals( name ) )
                continue;
    
            Class[] types = methods[i].getParameterTypes();
    
            // Does it have the same number of arguments that we're looking for.
            if( types.length != parms.length )
                continue;
    
            // Check for type compatibility
            if( InspectionUtils.areTypesCompatible( types, parms ) )
                return methods[i];
            }
        return null;
    }
    

    InspectionUtils.areTypesCompatible() takes two lists of types, normalizes their primitives, and then verifies that one is “assignable” to the other. So it will handle the case where you have an Integer and are trying to call a method that takes int as well as the case where you have a String and are trying to call a method that takes Object. It does not handle the case of having an int and calling a method that takes float. There has to be some specificity.

    The one caveat is that the above method just searches in method order so if there are ambiguities then the selection is arbitrary. I’ve never encountered a real-world issue, so far in practice.

    Here is the compatibility check for reference:
    public static boolean areTypesCompatible( Class[] targets, Class[] sources ) {
    if( targets.length != sources.length )
    return false;

        for( int i = 0; i < targets.length; i++ ) {
            if( sources[i] == null )
                continue;
    
            if( !translateFromPrimitive( targets[i] ).isAssignableFrom( sources[i] ) )
                return false;
            }
        return( true );
    }
    

    The code is BSD and mine so the snippets are legal to use. If you decide you’d rather use this util package directly the most recent public release is here:
    https://meta-jb.svn.sourceforge.net/svnroot/meta-jb/trunk/dev/m2-repo/org/meta-jb/meta-jb-util/0.17.1/

    And I only mention that because there hasn’t been a bundled download in a long time since most of my active users are maven users. I seem to be more fond of writing code than cutting full releases. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you'll have to edit your question a little… May 14, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer For a roundup of mobile sims see here. https://stackoverflow.com/questions/464089/simulators-emulators-for-mobile-browser-testing Specifically… May 14, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer The target property of the event object (the parameter to… May 14, 2026 at 9:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.