1 20 21 package spoon.aval.processing; 22 23 import java.lang.annotation.Annotation ; 24 import java.util.Collection ; 25 26 import spoon.aval.Validator; 27 import spoon.aval.annotation.Implementation; 28 import spoon.processing.AbstractProcessor; 29 import spoon.processing.Property; 30 import spoon.reflect.declaration.CtAnnotation; 31 import spoon.reflect.declaration.CtElement; 32 import spoon.reflect.declaration.CtPackage; 33 import spoon.reflect.reference.CtFieldReference; 34 import spoon.reflect.reference.CtReference; 35 import spoon.reflect.reference.CtTypeReference; 36 37 59 public class AValProcessor extends AbstractProcessor<CtElement> { 60 61 @Property 62 static public boolean stopOnErrors = true; 63 64 67 public void process(CtElement element) { 68 69 if (element.getParent(CtPackage.class) == null) { 70 return; 71 } 72 73 for (CtAnnotation dslAnn : element.getAnnotations()) { 74 processValidators(element, dslAnn); 75 } 76 } 77 78 @SuppressWarnings ("unchecked") 79 private void processValidators(CtElement element, CtAnnotation dslAnn) { 80 81 CtTypeReference annotationType = dslAnn.getAnnotationType(); 82 Annotation [] annotations = annotationType.getAnnotations(); 83 84 for (Annotation annotation : annotations) { 85 CtTypeReference<?> ref = getFactory().Type().createReference( 86 annotation.annotationType()); 87 Implementation sv = ref.getAnnotation(Implementation.class); 88 if (sv == null || ref == null) 89 continue; 90 invokeValidator(sv.value(), element, annotation, dslAnn, ref); 91 92 } 93 94 Collection <CtFieldReference> dslB = annotationType.getDeclaredFields(); 95 for (CtFieldReference reference : dslB) { 96 Annotation [] anns = reference.getAnnotations(); 97 if (anns == null) 98 continue; 99 for (Annotation annotation : anns) { 100 CtTypeReference<?> ref = getFactory().Type().createReference( 101 annotation.annotationType()); 102 Implementation sv = ref.getAnnotation(Implementation.class); 103 if (sv == null || ref == null) 104 continue; 105 invokeValidator(sv.value(), element, annotation, dslAnn, 106 reference); 107 } 108 } 109 } 110 111 @SuppressWarnings ("unchecked") 112 private void invokeValidator(Class <? extends Validator> validator, 113 CtElement element, Annotation annotation, CtAnnotation dslAnn, 114 CtReference reference) { 115 116 try { 117 Validator validatorInstance = validator.newInstance(); 118 ValidationPoint vp = new ValidationPoint(element, dslAnn, 119 reference, annotation); 120 validatorInstance.check(vp); 121 122 } catch (InstantiationException e) { 123 e.printStackTrace(); 124 } catch (IllegalAccessException e) { 125 e.printStackTrace(); 126 } 127 128 } 129 130 @Override 131 public void processingDone() { 132 super.processingDone(); 133 if (stopOnErrors) { 134 getFactory().getEnvironment().setProcessingStopped( 135 ValidationPoint.shouldStopProcessing()); 136 } 137 } 138 139 @Override 140 public void init() { 141 super.init(); 142 ValidationPoint.resetShouldStopFlag(); 143 } 144 145 } 146 | Popular Tags |