1 20 21 package spoon.aval.support.validator; 22 23 import java.lang.annotation.Annotation ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import spoon.aval.Validator; 29 import spoon.aval.annotation.value.RefersTo; 30 import spoon.aval.processing.AValProcessor; 31 import spoon.aval.processing.ValidationPoint; 32 import spoon.processing.Severity; 33 import spoon.reflect.Factory; 34 import spoon.reflect.declaration.CtAnnotation; 35 import spoon.reflect.declaration.CtElement; 36 import spoon.reflect.reference.CtFieldReference; 37 import spoon.reflect.visitor.Query; 38 import spoon.support.query.AnnotationFilter; 39 53 public class RefersToValidator implements Validator<RefersTo> { 54 55 static private Map <Class <? extends Annotation >,List <CtElement>> cache = new HashMap <Class <? extends Annotation >,List <CtElement>>(); 56 59 public void check(ValidationPoint<RefersTo> vp) { 60 Class <? extends Annotation > referedType = vp.getValAnnotation().value(); 61 String referedAttributeName = vp.getValAnnotation().attribute(); 62 63 String dslAttributeName = ((CtFieldReference)vp.getDslElement()).getSimpleName(); 64 Object dslAttribute = vp.getDslAnnotation().getElementValue(dslAttributeName); 65 66 Factory f = vp.getProgramElement().getFactory(); 67 List <CtElement> posibles = cache.get(referedType); 68 69 if(posibles == null){ 70 posibles = Query.getElements(f,new AnnotationFilter<CtElement>(referedType)); 71 cache.put(referedType,posibles); 72 } 73 CtAnnotation dslAnnotation = vp.getDslAnnotation(); 74 75 for(CtElement posible : posibles){ 76 CtAnnotation refered = posible.getAnnotation(f.Type().createReference(referedType)); 77 Object referedVal = refered.getElementValue(referedAttributeName); 78 79 if(referedVal == null){ 80 CtElement dslElement = vp.getDslElement().getDeclaration(); 81 String message = referedAttributeName+" is not an attribute of the annotation "+referedType; 82 if(dslElement != null) 83 ValidationPoint.report(Severity.ERROR, dslElement, message); 84 return; 85 } 86 87 if(referedVal.equals(dslAttribute)){ 88 return; 89 } 90 } 91 92 String message = vp.getValAnnotation().message().replace("?val",dslAttribute.toString()) ; 93 ValidationPoint.report(vp.getValAnnotation().severity(), dslAnnotation, message,vp.fixerFactory(vp.getValAnnotation().fixers())); 94 } 95 96 } 97 | Popular Tags |