Results 1 to 4 of 4
  1. #1
    viper2843 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Reflection - Dynamic parameter types

    I have a class that invokes a method via reflection based on a few parameters. Here is some pseudocode representing my current class.
    Java Code:
    public runMethod(String className, String methodName) {
        Class<?>[] parameterTypes = new Class<?>{Map.class}; // Predefined now but I want to be dynamic
        Object[] parameters = new Object[] {}; // Defined elsewhere.
        Class clazz = Class.forName(className);
        Method method = clazz.getMethod(methodName, parameterTypes);
        method.invoke(obj, parameters); // obj defined elsewhere
    }
    In order for this class to work the method it invokes must have the same parameter types in every case. This is working just fine but I want to make it a bit more useful by making the method parameters dynamic, sort of like how Spring MVC request methods work. The problem is the Java reflection API requires me to know the parameter types when asking for the method. I am wondering if there is a way of getting a Method object without having to provide a fixed set of parameter types? Is there a better way of achieving my stated objective that I am not thinking of? I am open to suggestions. Thanks in advance.

  2. #2
    viper2843 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Reflection - Dynamic parameter types

    Any help would be greatly appreciated.

  3. #3
    StormyWaters is online now Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: Reflection - Dynamic parameter types

    Looking at the API (Class (Java Platform SE 7 ))

    You should be able to get an array of all the methods for the class.

  4. #4
    viper2843 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Reflection - Dynamic parameter types

    I am aware of the getMethods() call in the Class object. I was hoping to avoid the potentially slow looping to find the methods with a given name. It seems like a oversight to not have a way of getting all methods of a given name. Overloading a method is such a simple thing in Java that I would figure someone would have created a better way to do this.

Similar Threads

  1. JAR dynamic loading and reflection
    By MichelPrevost in forum Advanced Java
    Replies: 4
    Last Post: 07-09-2012, 10:04 PM
  2. how to use java.reflection with parameter(s)
    By newbiejava in forum New To Java
    Replies: 8
    Last Post: 08-25-2010, 09:21 AM
  3. Replies: 4
    Last Post: 06-18-2010, 10:08 AM
  4. Dynamic types for a variable.
    By Somelauw in forum New To Java
    Replies: 5
    Last Post: 11-27-2009, 10:38 AM
  5. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •