PageRenderTime 16ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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