001: /**
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use,
007: * modify and/or redistribute the software under the terms of the
008: * CeCILL-C
009: * license as circulated by CEA, CNRS and INRIA at the following URL:
010: * http://www.cecill.info.
011: *
012: * This program is distributed in the hope that it will be useful, but
013: * WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C
015: * License for more details.
016: *
017: * The fact that you are presently reading this means that you have had
018: * knowledge of the CeCILL-C license and that you accept its terms.
019: */package spoon.aval.processing;
020:
021: import java.lang.annotation.Annotation;
022: import java.lang.annotation.ElementType;
023:
024: import spoon.aval.support.validator.problemFixer.AValFixer;
025: import spoon.processing.ProblemFixer;
026: import spoon.processing.Severity;
027: import spoon.reflect.Factory;
028: import spoon.reflect.declaration.CtAnnotation;
029: import spoon.reflect.declaration.CtElement;
030: import spoon.reflect.reference.CtReference;
031:
032: /**
033: * Class representing a point in the program in which a validation should take
034: * place. it contains:
035: *
036: * <ul>
037: * <li> The annotated CtElement in the base program
038: * <li> The annotation instance placed on the base program
039: * <li> The annotation CtElement that is @Validated
040: * <li> The @Validator annotation instance.
041: * </ul>
042: *
043: * With this information, implementors of the {@link spoon.aval.Validator}
044: * interface are to decide if the use of a given annotation is valid or not.
045: *
046: * @see spoon.aval.Validator
047: *
048: * @param <V>
049: * The type of Validation Annotation.
050: */
051: public class ValidationPoint<V extends Annotation> {
052:
053: static private boolean shouldStop = false;
054:
055: /**
056: * Helper method called by a Validator to report an error, warning or
057: * message as dictated by the severity parameter.
058: *
059: * @param severity
060: * The severity of the report
061: * @param element
062: * The CtElement to which the report is associated
063: * @param message
064: * The message to report
065: */
066: public static void report(Severity severity, CtElement element,
067: String message, ProblemFixer... fix) {
068: element.getFactory().getEnvironment().report(null, severity,
069: element, message,
070: fix == null ? new ProblemFixer[0] : fix);
071: shouldStop = shouldStop | severity == Severity.ERROR;
072: }
073:
074: @SuppressWarnings("unchecked")
075: public ProblemFixer[] fixerFactory(
076: Class<? extends ProblemFixer>[] fixerType) {
077: ProblemFixer[] pf = new ProblemFixer[fixerType.length];
078: try {
079: for (int i = 0; i < fixerType.length; i++) {
080: pf[i] = fixerType[i].newInstance();
081: if (pf[i] instanceof AValFixer) {
082: AValFixer aF = (AValFixer) pf[i];
083: aF.setValidationPoint(this );
084: }
085: }
086: } catch (InstantiationException e) {
087: e.printStackTrace();
088: } catch (IllegalAccessException e) {
089: e.printStackTrace();
090: }
091: return pf;
092: }
093:
094: static boolean shouldStopProcessing() {
095: return shouldStop;
096: }
097:
098: static void resetShouldStopFlag() {
099: shouldStop = false;
100: }
101:
102: private CtElement programElement;
103:
104: private CtAnnotation dslAnnotation;
105:
106: private CtReference dslElement;
107:
108: private V valAnnotation;
109:
110: ValidationPoint(CtElement programElement,
111: CtAnnotation dslAnnotation, CtReference reference,
112: V valAnnotation) {
113: this .programElement = programElement;
114: this .dslAnnotation = dslAnnotation;
115: this .dslElement = reference;
116: this .valAnnotation = valAnnotation;
117: }
118:
119: /**
120: *
121: * The actual annotation placed on the base program element ({@link ValidationPoint#getProgramElement()})
122: *
123: * @return The annotation instance placed on the base program
124: */
125: public CtAnnotation getDslAnnotation() {
126: return dslAnnotation;
127: }
128:
129: /**
130: * A reference to the Dsl element that is annotated with the
131: *
132: * @Validator.
133: *
134: * @return If the validator is a structural one, the CtTypeReference type.
135: * if it is a value validator, a CtFieldReference representing the
136: * attribute.
137: */
138: public CtReference getDslElement() {
139: return dslElement;
140: }
141:
142: /**
143: * The annotated program element
144: *
145: * @see ElementType
146: *
147: * @return either: a CtClass, CtInterface, CtPackage, CtExecutable, CtField,
148: * CtParameter, or CtLocalVariable
149: */
150: public CtElement getProgramElement() {
151: return programElement;
152: }
153:
154: /**
155: * The actual
156: *
157: * @Validator instance that annotates the
158: * {@link ValidationPoint#getDslElement()}
159: *
160: * @return The
161: * @Validator annotation instance.
162: */
163: public V getValAnnotation() {
164: return valAnnotation;
165: }
166:
167: public Factory getFactory() {
168: return dslElement.getFactory();
169: }
170:
171: }
|