KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > aval > support > validator > RefersToValidator


1 /**
2  * Spoon - http://spoon.gforge.inria.fr/
3  * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
4  *
5  * This software is governed by the CeCILL-C License under French law and
6  * abiding by the rules of distribution of free software. You can use,
7  * modify and/or redistribute the software under the terms of the
8  * CeCILL-C
9  * license as circulated by CEA, CNRS and INRIA at the following URL:
10  * http://www.cecill.info.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C
15  * License for more details.
16  *
17  * The fact that you are presently reading this means that you have had
18  * knowledge of the CeCILL-C license and that you accept its terms.
19  */

20
21 package spoon.aval.support.validator;
22
23 import java.lang.annotation.Annotation JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
40  * Implementation of the {@link RefersTo} validator
41  *
42  * <p>
43  * This class implements the {@link RefersTo} validator.
44  * It checks the complete metamodel to see if there are CtElements annotated with
45  * the refered annotation and value. If this is not the case, an ERROR is reported.
46  * An ERROR is also reported if the refered annotation does not contain
47  * the refered attribute
48  * <p>
49  * To avoid unnecesary (and maybe costly) traversals of the model, a cache is kept with
50  * the annotations and values found during the first traversal of the model.
51  *
52  */

53 public class RefersToValidator implements Validator<RefersTo> {
54
55     static private Map JavaDoc<Class JavaDoc<? extends Annotation JavaDoc>,List JavaDoc<CtElement>> cache = new HashMap JavaDoc<Class JavaDoc<? extends Annotation JavaDoc>,List JavaDoc<CtElement>>();
56     /**
57      * Implementation of the Validator interface. Called by {@link AValProcessor}
58      */

59     public void check(ValidationPoint<RefersTo> vp) {
60         Class JavaDoc<? extends Annotation JavaDoc> referedType = vp.getValAnnotation().value();
61         String JavaDoc referedAttributeName = vp.getValAnnotation().attribute();
62         
63         String JavaDoc dslAttributeName = ((CtFieldReference)vp.getDslElement()).getSimpleName();
64         Object JavaDoc dslAttribute = vp.getDslAnnotation().getElementValue(dslAttributeName);
65     
66         Factory f = vp.getProgramElement().getFactory();
67         List JavaDoc<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 JavaDoc referedVal = refered.getElementValue(referedAttributeName);
78             
79             if(referedVal == null){
80                 CtElement dslElement = vp.getDslElement().getDeclaration();
81                 String JavaDoc 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 JavaDoc 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