Guest User

Untitled

a guest
Jul 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1.  
  2. class Magic {
  3.  
  4. def methodMissing(String methodName, Object arguments) {
  5. method(methodName, *arguments)
  6. }
  7.  
  8. private method(String methodName, Object... arguments) {
  9. if (!(arguments[-1] instanceof Closure))
  10. throw new MissingMethodException(methodName, this.class, arguments)
  11. method(methodName, arguments[-1], *arguments[0..-2])
  12. }
  13.  
  14. private method(String methodName, Map map, Object... arguments) {
  15. if (!(arguments[-1] instanceof Closure))
  16. throw new MissingMethodException(methodName, this.class, arguments)
  17. method(methodName, map, arguments[-1], *arguments[0..-2])
  18. }
  19.  
  20. private method(String methodName, String... strings) {
  21. println "$methodName(String... $strings)"
  22. }
  23.  
  24. private method(String methodName, Map map) {
  25. println "$methodName(Map $map)"
  26. }
  27.  
  28. private method(String methodName, Closure closure) {
  29. println "$methodName(Closure ${closure()})"
  30. }
  31.  
  32. private method(String methodName, Map map, String... strings) {
  33. println "$methodName(Map $map, String... $strings)"
  34. }
  35.  
  36. private method(String methodName, Closure closure, String... strings) {
  37. println "$methodName(Closure ${closure()}, String... $strings)"
  38. }
  39.  
  40. private method(String methodName, Map map, Closure closure) {
  41. println "$methodName(Map $map, Closure ${closure()})"
  42. }
  43.  
  44. private method(String methodName, Map map, Closure closure, String... strings) {
  45. println "$methodName(Map $map, Closure ${closure()}, String... $strings)"
  46. }
  47.  
  48. }
  49.  
  50. new Magic().with {
  51. s "string1", "string2"
  52. // s(String... [string1, string2])
  53. m key1: "value1", key2: "value2"
  54. // m(Map [key1:value1, key2:value2])
  55. c {"closure"}
  56. // c(Closure closure)
  57. sm "string1", key1: "value1", "string2", key2: "value2"
  58. // sm(Map [key1:value1, key2:value2], String... [string1, string2])
  59. sc "string1", "string2", {"closure"}
  60. // sc(Closure closure, String... [string1, string2])
  61. mc key1: "value1", key2: "value2", {"closure"}
  62. // mc(Map [key1:value1, key2:value2], Closure closure)
  63. smc "string1", key1: "value1", "string2", key2: "value2", {"closure"}
  64. // smc(Map [key1:value1, key2:value2], Closure closure, String... [string1, string2])
  65. }
Add Comment
Please, Sign In to add comment