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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:25:17+00:00 2026-05-27T08:25:17+00:00

Let say I have a class Foo like this public class Foo { public

  • 0

Let say I have a class Foo like this

public class Foo {
    public void setParameter(String name, Object value) {
       // ...
    }
}

I would like to get the setParameter method through reflection.

The problem is that in my code, I only know the ACTUAL parameters when I need to get this method. I have build a little method that can give me the Class array of actual parameters. Unfortunately the returned Class array from actual parameters ([String.class, String.class]) doesn’t match the Class array of formal parameters ([String.class, Object.class]) and getDeclaredMethod raises exception NoSuchMethodException.

Do you know of any library or code snippet that can help match the method ?

  • 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-27T08:25:18+00:00Added an answer on May 27, 2026 at 8:25 am

    You’ll probably need to be a little smarter about finding which method to call. One option is to iterate through each method from getDeclaredMethods(), first checking the name of the method to see if that’s the one you want to call, then checking if the number of actual parameters matches the number of formal parameters, and lastly, if both of those are true, iterating through its actual parameters and checking if the parameter class is assignable from your formal parameter class.

    One problem, however, is what happens if you have the methods:

    public void setParameter(String name, Object value);
    public void setParameter(String name, String value);
    

    as both will match that test.

    I don’t know of any library of the top of my head that can handle that in a lightweight way. Though most dependency-injection frameworks (Spring, Guice) would have some support for it for their needs, and it may be worth looking into how they do things.

    Jon Skeet’s answer here has an example of what I’m explaining.

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

Sidebar

Related Questions

let's say I have 2 classes like this: public class Foo { [Required] public
Let say I have a class like this: public sealed class Foo { public
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
Let's say I have an object who's class definition looks like: class Command: foo
Let's say I have a class: class Foo { public string Bar { get
Let's say I have a simple POJO class: public void Foo { public int
Let's say I have a fairly large IList<foo> where foo looks like this: public
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many
Let's say I have a CSS class named foo but would like it to

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.