views:

356

answers:

5

I have lots of object defined in the system, perhaps 1000 objects, and some of them have this method:

public Date getDate();

Is there anyway I can do something like this:

Object o = getFromSomeWhere.....;

Method m = o.getMethod("getDate");
Date date = (Date) m.getValue();
+2  A: 

If there is an interface requiring a getDate() method you can check using the following code:

if (o instance of GetDateInterface){
    GetDateInterface foo = (GetDateInterface) o;
    Date d = foo.getDate();
}
jjnguy
+7  A: 

If you can make them all implement an interface, that would certainly be the best option. However, reflection will also work, and your code was nearly there:

Object o = getFromSomeWhere.....;
Method m = o.getClass().getMethod("getDate");
Date date = (Date) m.invoke(o);

(There's a bunch of exceptions you'll need to handle, admittedly...)

For a full example:

import java.lang.reflect.*;
import java.util.*;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        Object o = new Test();
        Method m = o.getClass().getMethod("getDate");
        Date date = (Date) m.invoke(o);
        System.out.println(date);
    }

    public Date getDate()
    {
        return new Date();
    }
}
Jon Skeet
Tsk tsk, "throws Exception" is bad form.
Henrik Paul
wow..this is exactly what i want..thanks...
shrimpy
@Henrik: For production code, I'd quite agree. But when writing a sample app to demonstrate a particular piece of code (really only three lines are relevant) I think it's fine. I mention in the post that there are a bunch of exceptions to handle. Would it really do any good to spell those out explicitly in the test app? Would cluttering up the program actually make anything clearer? Where's the benefit?
Jon Skeet
You are correct, indeed. I must've missed the mention of exceptions in my morning-haste, my apologies.
Henrik Paul
I kind of agree, unfortunately people do copy the style of the [example] code they have seen.
Tom Hawtin - tackline
+1  A: 

To complete the other answer(s), I'd also identify the classes with an interface. This is how I'd do it

import java.util.Date;

interface Dated {
  public Date getDate();
  public void setDate(Date date);
}

class FooObject implements Dated {
  private Date date;
  public void setDate(Date date) { this.date = date; }
  public Date getDate() { return date; }
  // other code...
}

public static void main (String[] args) {
  Object o = getRandomObject(); // implemented elsewhere
  if (o instanceof Dated) {
    Date date = ((Dated)o).getDate();
    doStuffWith(date);
  }
}
Henrik Paul
i cannot modify the other code...hmm..otherwise..i will do in this way then..thanks anyway
shrimpy
+3  A: 

Try this:

Object aDate = new Date();
Method aMethod = aDate.getClass().getMethod("getDate", (Class<Void>[]) null);
Object aResult = aMethod.invoke(aDate, (Void) null);

You should add try-catch to determine if there really is a method getDate before invoking.

Azder
A: 

If you don't mind the extra dependency, BeanUtils can do this for you.

Dan Howard