Guest User

Untitled

a guest
Jun 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public Date getDate();
  2.  
  3. Object o = getFromSomeWhere.....;
  4.  
  5. Method m = o.getMethod("getDate");
  6. Date date = (Date) m.getValue();
  7.  
  8. Object o = getFromSomeWhere.....;
  9. Method m = o.getClass().getMethod("getDate");
  10. Date date = (Date) m.invoke(o);
  11.  
  12. import java.lang.reflect.*;
  13. import java.util.*;
  14.  
  15. public class Test
  16. {
  17. public static void main(String[] args) throws Exception
  18. {
  19. Object o = new Test();
  20. Method m = o.getClass().getMethod("getDate");
  21. Date date = (Date) m.invoke(o);
  22. System.out.println(date);
  23. }
  24.  
  25. public Date getDate()
  26. {
  27. return new Date();
  28. }
  29. }
  30.  
  31. Object aDate = new Date();
  32. Method aMethod = aDate.getClass().getMethod("getDate", (Class<Void>[]) null);
  33. Object aResult = aMethod.invoke(aDate, (Void) null);
  34.  
  35. if (o instance of GetDateInterface){
  36. GetDateInterface foo = (GetDateInterface) o;
  37. Date d = foo.getDate();
  38. }
  39.  
  40. import java.util.Date;
  41.  
  42. interface Dated {
  43. public Date getDate();
  44. public void setDate(Date date);
  45. }
  46.  
  47. class FooObject implements Dated {
  48. private Date date;
  49. public void setDate(Date date) { this.date = date; }
  50. public Date getDate() { return date; }
  51. // other code...
  52. }
  53.  
  54. public static void main (String[] args) {
  55. Object o = getRandomObject(); // implemented elsewhere
  56. if (o instanceof Dated) {
  57. Date date = ((Dated)o).getDate();
  58. doStuffWith(date);
  59. }
  60. }
Add Comment
Please, Sign In to add comment