Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Date getDate();
- Object o = getFromSomeWhere.....;
- Method m = o.getMethod("getDate");
- Date date = (Date) m.getValue();
- Object o = getFromSomeWhere.....;
- Method m = o.getClass().getMethod("getDate");
- Date date = (Date) m.invoke(o);
- 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();
- }
- }
- Object aDate = new Date();
- Method aMethod = aDate.getClass().getMethod("getDate", (Class<Void>[]) null);
- Object aResult = aMethod.invoke(aDate, (Void) null);
- if (o instance of GetDateInterface){
- GetDateInterface foo = (GetDateInterface) o;
- Date d = foo.getDate();
- }
- 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);
- }
- }
Add Comment
Please, Sign In to add comment