/transmutant-core/src/main/groovy/com/googlecode/transmutant/StepValidator.groovy

http://transmutant.googlecode.com/ · Groovy · 68 lines · 57 code · 11 blank · 0 comment · 9 complexity · 3165e44603438b455b950a853f35e427 MD5 · raw file

  1. package com.googlecode.transmutant
  2. import java.util.HashSet
  3. import org.apache.log4j.BasicConfigurator
  4. import org.apache.log4j.Level
  5. import org.apache.log4j.LogManager
  6. public class StepValidator implements Steppable { {
  7. BasicConfigurator.configure()
  8. LogManager.rootLogger.level = Level.INFO
  9. }
  10. def log = LogManager.getLogger(StepValidator.class);
  11. void step(def steps) {
  12. steps.each {step->
  13. def orderSet = [] as Set
  14. def orderList = []
  15. def outSet = [] as Set
  16. def outList = []
  17. step.class.methods.each {m->
  18. if (m.isAnnotationPresent(Step.class)){
  19. def order = m.getAnnotation(Step.class).order()
  20. orderSet.add(order)
  21. orderList.add(order)
  22. }
  23. if (m.isAnnotationPresent(Out.class)){
  24. def name = m.getAnnotation(Out.class).name()
  25. outSet.add(name)
  26. outList.add(name)
  27. }
  28. }
  29. checkForDuplicateAnnotations(step, outSet, outList)
  30. if (orderList.size() != 1 && orderSet.size() < orderList.size()){
  31. throw new IllegalStateException("@Step methods should have unique order values")
  32. }
  33. }
  34. }
  35. private checkForDuplicateAnnotations(step, outSet, outList) {
  36. def inSet = [] as Set
  37. def inList = []
  38. step.class.declaredFields.each {f->
  39. if (f.isAnnotationPresent(In.class)){
  40. def name = f.getAnnotation(In.class).name()
  41. inSet.add(name)
  42. inList.add(name)
  43. }
  44. if (f.isAnnotationPresent(Out.class)){
  45. def name = f.getAnnotation(Out.class).name()
  46. outSet.add(name)
  47. outList.add(name)
  48. }
  49. }
  50. if(inSet.size() < inList.size()) {
  51. log.warn("duplicate @In names found")
  52. }
  53. if(outSet.size() < outList.size()) {
  54. log.warn("duplicate @Out names found")
  55. }
  56. }
  57. }