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 9074403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:37:40+00:00 2026-06-16T18:37:40+00:00

I want to use getDeclaredMethod() to find a method with this signature: public void

  • 0

I want to use getDeclaredMethod() to find a method with this signature:

public void foo(String inArg1, Object... inArgs);

Using this call:

Class<?>[] argClasses = { String.class, Integer.class };
Method m = clazz.getDeclaredMethod("foo", argClasses);

But it generates a NoSuchMethodException exception. However, it is possible to invoke the method (assuming you find it some other way):

Object[] args = { "arg1", new Integer(2) };
m.invoke(instance, args);

I could list them all, with getDeclaredMethods(), and then try to do the signature matching myself, but that seems like a lot of work.

Should what I want to do be possible? Am I just missing something dumb?

  • 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-06-16T18:37:42+00:00Added an answer on June 16, 2026 at 6:37 pm

    The method is callable with any object(s) because it is declared to accept object(s). – But, because it is declared with Object type you won’t find via getDeclaredMethod() if you don’t search for the exact declared type.

    You should be able, though, to iterate over all declared methods and check on their parameter types to see if they are assignable from your given type to find all methods which are callable with your parameters.

    Example:

    Class[] signature = {String.class, Integer.class};
    Method[] methods = someClass.getDeclaredMethods();
    
    for ( Method m : methods ) {
      Class[] params = m.getParameterTypes();
      if ( params.length == signature.length ) {
        int i;
        for ( i = 0; i < signature.length && params[i].isAssignableFrom( signature[i] ); i++ ) {
    
        }
        if ( i == signature.length ) {
          System.out.println("Found matching method: " + m.getName());
        }
      }
    }
    

    However, I have not checked which type(s) Object... actually is assignable from. I assume that it is not Integer but rather Integer[] since the varargs are compiled to arrays.

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

Sidebar

Related Questions

I want use this 1 for using Bar code or QR code scanner. I
I want use page that has this form: <form method=post action=modules.php?name=search> <input onkeypress=FKeyPress(this); onkeydown=FKeyDown(this);
I want use AES Algorithm. I use this function on it void SubBytes(char *SArr[4][4]){
I want use regex to find something in string (or QString) that is between
We have a powerbuilder application and we want use a scanner through this application
I want use writeToFile to store a pic image to the documents, but find
I want to use hover function with live method but it is not working
i want to use title on text. is this possible to make title on
I do not want use each and every time parseInt('08', 10) for each string
my problem is that, i want to save class object java.lang.reflect.Field into database using

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.