3 Replies Latest reply: Oct 21, 2010 3:21 PM by 796440 RSS

    Reflection question on getMethod

    442757
      Hi,
      I'm trying to solve the following issue in reflection. I've a POJO which kind of acts as a metadata for the method signature in TestResponse class. TestResponse has a setDate() methid which takes a Date parameter. I'm trying to make this is a generic code which can accept any method and its signature to set in the response. What I'm not able to figure out is how to set the parameter Class while calling getMethod() based on the input. The input tells me to set the parameter as Date, but not sure how to achiever that.

      Here's my sample code. Ofcourse, the mdi.modifier.getClass() is wrong since it'll get String.class instead of Date.class.

      TestResponse response = new TestResponse();
      Object val = "test";
      MDIBase mdi = new MDIBase("setDate", "Date");
      Method m = response.getClass().getMethod(mdi.method, mdi.modifier.getClass());
      m.invoke(response, new Object[] { val });

      Here's MDIBase

      public class MDIBase {
      public String method;
      public String modifier;
      public MDIBase(String method, String modifier){
      this.method = method;
      this.modifier = modifier;
      }

      Any pointer will be highly appreciated.

      Thanks
        • 1. Re: Reflection question on getMethod
          796440
          It's not clear what your question is. It sounds like you're asking how to specify the parameter list to distinguish a signature maybe? Or how to pass those parameters when calling invoke()?
          public class Foo {
            public Baz bar(int i, String s, Date d) {
              return ...;
            }
          }
          
          // To invoke the above reflectively...
          
          Foo foo = someFoo;
          
          Method m = Foo.class.getMethod("bar", int.class, String.class, Date.class);
          // OR, if you dislike varargs...
          Class[] argClasses = {intclass, String.class, Date.class};
          Method m = Foo.class.getMethod("bar", argClasses);
          
          // there's also getDeclaredMethod(). Study the docs to understand the difference and determine which is appropriate
          
          Baz baz = (Baz)m.invoke(foo, 123, "abc", new Date());
          
          // or invoke it with an Object[] holding Integer 123, "abc", and  a Date, if you prefer
          • 2. Re: Reflection question on getMethod
            442757
            Thanks jverd for looking into this. I'm sorry for not being clear on my question. But I've found what I was looking for.

            I'm setting the modifier value in MDIBase as java.util.Date

            response.getClass().getMethod(mdi.method, Class.forName(mdi.modifier));
            • 3. Re: Reflection question on getMethod
              796440
              shamikus wrote:
              Thanks jverd for looking into this. I'm sorry for not being clear on my question. But I've found what I was looking for.

              I'm setting the modifier value in MDIBase as java.util.Date

              response.getClass().getMethod(mdi.method, Class.forName(mdi.modifier));
              So, mdi is a refrence or a class name, and modifier is a public (or if not public, at least accessible from the above code) String field in that class. method is another public or otherwise accessible string field. You're finding a method with the name given in the method variable, and the single arg whose class is named in the modifier variable. Yes?

              Anyway, glad you got it working.