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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:35:28+00:00 2026-05-14T03:35:28+00:00

Given: class A { public void m(List l) { … } } Let’s say

  • 0

Given:

class A
{
    public void m(List l) { ... }
}

Let’s say I want to invoke method m with reflection, passing an ArrayList as the parameter to m:

List myList = new ArrayList();
A a = new A();
Method method = A.class.getMethod("m", new Class[] { myList.getClass() });
method.invoke(a, Object[] { myList });

The getMethod on line 3 will throw NoSuchMethodException because the runtime type of myList is ArrayList, not List.

Is there a good generic way around this that doesn’t require knowledge of class A’s parameter types?

  • 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-14T03:35:29+00:00Added an answer on May 14, 2026 at 3:35 am

    If you know the type is List, then use List.class as argument.

    If you don’t know the type in advance, imagine you have:

    public void m(List l) {
     // all lists
    }
    
    public void m(ArrayList l) {
      // only array lists
    }
    

    Which method should the reflection invoke, if there is any automatic way?

    If you want, you can use Class.getInterfaces() or Class.getSuperclass() but this is case-specific.

    What you can do here is:

    public void invoke(Object targetObject, Object[] parameters,
            String methodName) {
        for (Method method : targetObject.getClass().getMethods()) {
            if (!method.getName().equals(methodName)) {
                continue;
            }
            Class<?>[] parameterTypes = method.getParameterTypes();
            boolean matches = true;
            for (int i = 0; i < parameterTypes.length; i++) {
                if (!parameterTypes[i].isAssignableFrom(parameters[i]
                        .getClass())) {
                    matches = false;
                    break;
                }
            }
            if (matches) {
                // obtain a Class[] based on the passed arguments as Object[]
                method.invoke(targetObject, parametersClasses);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This following is from generics tutorials: Say class R extends S, public void addR(List<?
I was given the following as an interview question: class A { public: void
Given two methods on the same class in Java : public void doSomething( Person
Given: public interface IBatchProcess { void Run(); } and multiple implementation of: public class
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Given a basic repository interface: public interface IPersonRepository { void AddPerson(Person person); List<Person> GetAllPeople();
Given, public class SomeClass { public string SomeName{get;} public List<string> RelatedNames{get;} } public class
Source Code of LoginAction.java package com.test; import java.util.ArrayList; import java.util.List; public class LoginAction {
Given the following controller class: public class ProjectController : Controller { public ActionResult List()
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;

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.