KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > aval > processing > AValProcessor


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.processing;
22
23 import java.lang.annotation.Annotation JavaDoc;
24 import java.util.Collection JavaDoc;
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 /**
38  * Spoon processor that analyses a program and executes the
39  *
40  * @Validators it finds. By default, if any validator reports an error,
41  * all further processing (like code generation) will be skiped.
42  *
43  * The processor passes though all CtElements in the model. For each element,
44  * it:
45  *
46  * <ol>
47  * <li> processes all the annotations of the CtElement.
48  * <li> processes all the meta-annotations (@Validators).
49  * <li> for each
50  * @Validator, it creates an instance of its implementation, and constructs the
51  * corresponding {@link spoon.aval.processing.ValidationPoint}
52  * <li> it invokes the check method on the
53  * @Validator's implementation.
54  * </ol>
55  *
56  * @see spoon.aval.Validator
57  * @see spoon.aval.processing.ValidationPoint
58  */

59 public class AValProcessor extends AbstractProcessor<CtElement> {
60
61     @Property
62     static public boolean stopOnErrors = true;
63
64     /**
65      * Main Validation processing method. Invoked by Spoon.
66      */

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 JavaDoc("unchecked")
79     private void processValidators(CtElement element, CtAnnotation dslAnn) {
80
81         CtTypeReference annotationType = dslAnn.getAnnotationType();
82         Annotation JavaDoc[] annotations = annotationType.getAnnotations();
83
84         for (Annotation JavaDoc 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 JavaDoc<CtFieldReference> dslB = annotationType.getDeclaredFields();
95         for (CtFieldReference reference : dslB) {
96             Annotation JavaDoc[] anns = reference.getAnnotations();
97             if (anns == null)
98                 continue;
99             for (Annotation JavaDoc 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 JavaDoc("unchecked")
112     private void invokeValidator(Class JavaDoc<? extends Validator> validator,
113             CtElement element, Annotation JavaDoc 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 JavaDoc e) {
123             e.printStackTrace();
124         } catch (IllegalAccessException JavaDoc e) {
125             e.printStackTrace();
126         }
127
128     }
129
130     @Override JavaDoc
131     public void processingDone() {
132         super.processingDone();
133         if (stopOnErrors) {
134             getFactory().getEnvironment().setProcessingStopped(
135                     ValidationPoint.shouldStopProcessing());
136         }
137     }
138
139     @Override JavaDoc
140     public void init() {
141         super.init();
142         ValidationPoint.resetShouldStopFlag();
143     }
144
145 }
146
Popular Tags