KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.annotation.ElementType JavaDoc;
25
26 import spoon.aval.support.validator.problemFixer.AValFixer;
27 import spoon.processing.ProblemFixer;
28 import spoon.processing.Severity;
29 import spoon.reflect.declaration.CtAnnotation;
30 import spoon.reflect.declaration.CtElement;
31 import spoon.reflect.reference.CtReference;
32
33 /**
34  * Class representing a point in the program in which a validation should take
35  * place. it contains:
36  *
37  * <ul>
38  * <li> The annotated CtElement in the base program
39  * <li> The annotation instance placed on the base program
40  * <li> The annotation CtElement that is
41  *
42  * @Validated
43  * <li> The
44  * @Validator annotation instance.
45  * </ul>
46  *
47  * With this information, implementors of the {@link spoon.aval.Validator}
48  * interface are to decide if the use of a given annotation is valid or not.
49  *
50  * @see spoon.aval.Validator
51  *
52  * @param <V>
53  * The type of Validation Annotation.
54  */

55 public class ValidationPoint<V extends Annotation JavaDoc> {
56     
57     static private boolean shouldStop = false;
58     
59     
60     
61     /**
62      * Helper method called by a Validator to report an error, warning or
63      * message as dictated by the severity parameter.
64      *
65      * @param severity
66      * The severity of the report
67      * @param element
68      * The CtElement to which the report is associated
69      * @param message
70      * The message to report
71      */

72     public static void report(Severity severity, CtElement element,
73             String JavaDoc message,ProblemFixer... fix ) {
74         element.getFactory().getEnvironment().report(null, severity, element,
75                 message,fix==null?new ProblemFixer[0]:fix);
76         shouldStop = shouldStop | severity == Severity.ERROR;
77     }
78     
79     @SuppressWarnings JavaDoc("unchecked")
80     public ProblemFixer[] fixerFactory(Class JavaDoc<? extends ProblemFixer>[] fixerType){
81         ProblemFixer[] pf = new ProblemFixer[fixerType.length];
82         try {
83             for (int i = 0; i < fixerType.length; i++) {
84                 pf[i] = fixerType[i].newInstance();
85                 if (pf[i] instanceof AValFixer) {
86                     AValFixer aF = (AValFixer) pf[i];
87                     aF.setValidationPoint(this);
88                 }
89             }
90         } catch (InstantiationException JavaDoc e) {
91             e.printStackTrace();
92         } catch (IllegalAccessException JavaDoc e) {
93             e.printStackTrace();
94         }
95         return pf;
96     }
97     
98     static boolean shouldStopProcessing(){
99         return shouldStop;
100     }
101     
102     static void resetShouldStopFlag(){
103         shouldStop = false;
104     }
105
106     private CtElement programElement;
107
108     private CtAnnotation dslAnnotation;
109
110     private CtReference dslElement;
111
112     private V valAnnotation;
113
114     ValidationPoint(CtElement programElement, CtAnnotation dslAnnotation,
115             CtReference reference, V valAnnotation) {
116         this.programElement = programElement;
117         this.dslAnnotation = dslAnnotation;
118         this.dslElement = reference;
119         this.valAnnotation = valAnnotation;
120     }
121
122     /**
123      *
124      * The actual annotation placed on the base program element ({@link ValidationPoint#getProgramElement()})
125      *
126      * @return The annotation instance placed on the base program
127      */

128     public CtAnnotation getDslAnnotation() {
129         return dslAnnotation;
130     }
131
132     /**
133      * A reference to the Dsl element that is annotated with the
134      *
135      * @Validator.
136      *
137      * @return If the validator is a structural one, the CtTypeReference type.
138      * if it is a value validator, a CtFieldReference representing the
139      * attribute.
140      */

141     public CtReference getDslElement() {
142         return dslElement;
143     }
144
145     /**
146      * The annotated program element
147      *
148      * @see ElementType
149      *
150      * @return either: a CtClass, CtInterface, CtPackage, CtExecutable, CtField,
151      * CtParameter, or CtLocalVariable
152      */

153     public CtElement getProgramElement() {
154         return programElement;
155     }
156
157     /**
158      * The actual
159      *
160      * @Validator instance that annotates the
161      * {@link ValidationPoint#getDslElement()}
162      *
163      * @return The
164      * @Validator annotation instance.
165      */

166     public V getValAnnotation() {
167         return valAnnotation;
168     }
169
170 }
171
Popular Tags