KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
25
26 import spoon.aval.annotation.ReplacesAnnotationInPackage;
27 import spoon.processing.AbstractManualProcessor;
28 import spoon.processing.Property;
29 import spoon.processing.Severity;
30 import spoon.reflect.Factory;
31 import spoon.reflect.declaration.CtAnnotation;
32 import spoon.reflect.declaration.CtAnnotationType;
33 import spoon.reflect.declaration.CtPackage;
34 import spoon.reflect.visitor.Filter;
35 import spoon.reflect.visitor.Query;
36 import spoon.support.query.AnnotationFilter;
37
38 /**
39  * Processes dummy implementations of external validated annotations.
40  * <p>
41  * Processes Annotation types that are annotated with
42  * {@link ReplacesAnnotationInPackage}. These annotations are used to
43  * &#64;Validate annotations for which the source code is not available.
44  *
45  */

46 public class DummyPreProcessor extends AbstractManualProcessor {
47
48     /**
49      * Property that contains the names of the packages in which the dummies are
50      * located.
51      */

52     @Property
53     String JavaDoc[] packs;
54
55     @Override JavaDoc
56     public void init() {
57         super.init();
58     }
59
60     /**
61      * Processes the dummy packages as defined by
62      * {@link DummyPreProcessor#packs}.
63      * <p>
64      * This method is upcalled by spoon.
65      */

66     public void process() {
67
68         Factory fac = getFactory();
69         if (packs == null) {
70             List JavaDoc<CtAnnotationType<? extends Annotation JavaDoc>> types = Query
71                     .getElements(
72                             fac,
73                             new Filter<CtAnnotationType<? extends Annotation JavaDoc>>() {
74
75                                 @SuppressWarnings JavaDoc("unchecked")
76                                 public Class JavaDoc<CtAnnotationType<? extends Annotation JavaDoc>> getType() {
77                                     // TODO Auto-generated method stub
78
return (Class JavaDoc) CtAnnotationType.class;
79                                 }
80
81                                 public boolean matches(
82                                         CtAnnotationType<? extends Annotation JavaDoc> element) {
83                                     return element
84                                             .getAnnotation(ReplacesAnnotationInPackage.class) != null;
85                                 }
86
87                             });
88
89             for (CtAnnotationType<? extends Annotation JavaDoc> type : types) {
90                 replaceAnnotationImpl(fac, type);
91             }
92         } else {
93             for (String JavaDoc packag : packs) {
94                 processDummyPackage(fac, packag);
95             }
96         }
97     }
98
99     private void processDummyPackage(Factory fac, String JavaDoc packag) {
100         List JavaDoc<CtAnnotationType<? extends Annotation JavaDoc>> dummys = getPackages(fac,
101                 packag);
102
103         for (CtAnnotationType<? extends Annotation JavaDoc> type : dummys) {
104             replaceAnnotationImpl(fac, type);
105         }
106     }
107
108     private void replaceAnnotationImpl(Factory fac,
109             CtAnnotationType<? extends Annotation JavaDoc> type) {
110         CtAnnotation<ReplacesAnnotationInPackage> rep = type.getAnnotation(fac
111                 .Type().createReference(ReplacesAnnotationInPackage.class));
112         String JavaDoc repPackName = (String JavaDoc) rep.getElementValue("value");
113         CtPackage repPack = fac.Package().get(repPackName);
114         if (repPack == null) {
115             fac.getEnvironment().report(
116                     this,
117                     Severity.MESSAGE,
118                     rep,
119                     "Replaces annotation refers to unknown package "
120                             + repPackName);
121             repPack = fac.Package().getOrCreate(repPackName);
122         }
123         type.getParent(CtPackage.class).getTypes().remove(type);
124         repPack.getTypes().add(type);
125         type.setParent(repPack);
126         // fac.Package().registerPackage(pck)
127
}
128
129     private List JavaDoc<CtAnnotationType<? extends Annotation JavaDoc>> getPackages(
130             Factory fac, String JavaDoc packag) {
131         CtPackage dummyP = fac.Package().get(packag);
132         List JavaDoc<CtAnnotationType<? extends Annotation JavaDoc>> dummys = Query
133                 .getElements(
134                         dummyP,
135                         new AnnotationFilter<CtAnnotationType<? extends Annotation JavaDoc>>(
136                                 ReplacesAnnotationInPackage.class));
137         return dummys;
138     }
139 }
140
Popular Tags