KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import spoon.processing.AbstractProcessor;
27 import spoon.processing.Property;
28 import spoon.processing.Severity;
29 import spoon.reflect.Factory;
30 import spoon.reflect.declaration.CtAnnotationType;
31 import spoon.reflect.declaration.CtField;
32 import spoon.reflect.declaration.CtPackage;
33 import spoon.reflect.reference.CtExecutableReference;
34 import spoon.reflect.reference.CtPackageReference;
35 import spoon.reflect.reference.CtReference;
36 import spoon.reflect.reference.CtTypeReference;
37 import spoon.reflect.visitor.CtScanner;
38
39 /**
40  * This processor visits all the declarations of annotations in the program,
41  * checking that they are processed.
42  * <p>
43  * The completeness checker verifies that for every annotation type defined in
44  * the application there is a class that uses it. This is performed by taking
45  * each declaration, and checking that there is at least a reference to the
46  * annotation type, and a reference to each of the attributes defined in it.
47  *
48  * <p>
49  * The places where the reference is going to be checked are described by a list
50  * of packages, so it is possible to restrict the check to a subset of the
51  * system (for example only to unit tests)
52  *
53  *
54  */

55
56 public class CompletenessCheckerProcessor extends
57         AbstractProcessor<CtAnnotationType<?>> {
58
59     private List JavaDoc<CtPackage> framework;
60
61     private List JavaDoc<CtPackageReference> annotations;
62
63     @Property
64     String JavaDoc[] checkPackages;
65
66     @Property
67     String JavaDoc[] skipAnnotationsIn;
68
69     @Property
70     boolean checkAll = false;
71
72     /**
73      * Initializes the package list.
74      * <p>
75      * This method is called by spoon
76      */

77     @Override JavaDoc
78     public void init() {
79         super.init();
80         framework = new ArrayList JavaDoc<CtPackage>();
81         annotations = new ArrayList JavaDoc<CtPackageReference>();
82         configurePackages();
83     }
84
85     /**
86      * Overload this method to change the list of packages in which to chek the
87      * references.
88      */

89     protected void configurePackages() {
90         // framework.addAll(getFactory().Package().getCreatedPackages());
91
// annotations.add(getFactory().Package().createReference("spoon.aval"));
92
// annotations.add(getFactory().Package().createReference("java"));
93

94         Factory fac = getFactory();
95         if (checkAll) {
96             framework.addAll(getFactory().Package().getAll());
97         } else {
98             for (String JavaDoc check : checkPackages) {
99                 framework.add(fac.Package().getOrCreate(check));
100             }
101         }
102
103         for (String JavaDoc skip : skipAnnotationsIn) {
104             annotations.add(fac.Package().createReference(skip));
105         }
106
107     }
108
109     /**
110      * Processes each CtAnnotationType, checking that it is processed.
111      * <p>
112      * This method is called by spoon
113      */

114     public void process(CtAnnotationType<?> element) {
115
116         if (shouldSkip(element))
117             return;
118
119         checkTypeUse(element);
120         List JavaDoc<CtField<?>> fields = element.getFields();
121
122         for (CtField<?> field : fields) {
123             checkAccess(field);
124         }
125
126     }
127
128     private boolean shouldSkip(CtAnnotationType<?> element) {
129         CtPackageReference elPackRef = element.getPackage().getReference();
130         for (CtPackageReference ref : annotations) {
131             if (elPackRef.getSimpleName().startsWith(ref.getSimpleName())) {
132                 return true;
133             }
134         }
135         return false;
136     }
137
138     private void checkAccess(final CtField<?> field) {
139         final boolean[] found = new boolean[1];
140         found[0] = false;
141         for (CtPackage pack : framework) {
142             new CtScanner() {
143                 @Override JavaDoc
144                 public void scan(CtReference reference) {
145                     if (reference instanceof CtExecutableReference) {
146                         CtExecutableReference ref = (CtExecutableReference) reference;
147                         if (ref.getSimpleName().equals(field.getSimpleName())
148                                 && ref.getDeclaringType()
149                                         .equals(
150                                                 field.getDeclaringType()
151                                                         .getReference())) {
152                             found[0] = true;
153                         }
154                     }
155                     super.scan(reference);
156                 }
157             }.scan(pack);
158         }
159
160         if (!found[0])
161             getFactory().getEnvironment().report(this, Severity.WARNING, field,
162                     "This Field is never processed!");
163
164     }
165
166     private void checkTypeUse(final CtAnnotationType<?> element) {
167         final boolean[] found = new boolean[1];
168         found[0] = false;
169         for (CtPackage pack : framework) {
170             new CtScanner() {
171                 @Override JavaDoc
172                 public void scan(CtReference reference) {
173                     if (reference instanceof CtTypeReference<?>) {
174                         CtTypeReference<?> ref = (CtTypeReference<?>) reference;
175                         if (ref.equals(element.getReference())) {
176                             found[0] = true;
177                         }
178                     }
179                     super.scan(reference);
180                 }
181             }.scan(pack);
182         }
183
184         if (!found[0])
185             getFactory().getEnvironment().report(this, Severity.WARNING,
186                     element, "This annotation is never processed!");
187
188     }
189
190 }
191
Popular Tags