KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > apt > AnnotationConstraintAptValidator


1 package org.apache.beehive.controls.runtime.generator.apt;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$
19  */

20
21 import java.io.File JavaDoc;
22 import java.lang.annotation.Annotation JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.text.ParseException JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Map.Entry;
35
36 import org.apache.beehive.controls.api.bean.AnnotationMemberTypes;
37 import org.apache.beehive.controls.api.bean.AnnotationConstraints.MembershipRule;
38 import org.apache.beehive.controls.api.bean.AnnotationConstraints.MembershipRuleValues;
39 import org.apache.beehive.controls.api.properties.PropertyKey;
40 import org.apache.beehive.controls.api.properties.PropertySet;
41 import org.apache.beehive.controls.runtime.bean.AnnotationConstraintValidator;
42
43 import com.sun.mirror.declaration.AnnotationMirror;
44 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
45 import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
46 import com.sun.mirror.declaration.AnnotationValue;
47 import com.sun.mirror.declaration.Declaration;
48 import com.sun.mirror.declaration.MethodDeclaration;
49 import com.sun.mirror.declaration.TypeDeclaration;
50 import com.sun.mirror.type.AnnotationType;
51
52 /**
53  * This class is for validating control property values at build time
54  * It calls {@link org.apache.beehive.controls.runtime.bean.AnnotationConstraintValidator
55  * AnnotationConstraintValidator} to do the validation.
56  */

57 public class AnnotationConstraintAptValidator extends AnnotationConstraintValidator
58 {
59
60     public AnnotationConstraintAptValidator()
61     {
62         super();
63     }
64
65     /**
66      * This method ensures that any control property value assignment satisfies
67      * all property constraints. This method should be called from an annotation
68      * processor to ensure declarative control property assignment using
69      * annotations are validated at build time. This method is currently called
70      * from ControlAnnotationProcessor and ControlClientAnnotationProcessor.
71      *
72      * @param d
73      * a declaration which may contain a control property value
74      * assignment
75      * @throws IllegalArgumentException
76      * when the declaration contains a control property value
77      * assignment that does not satisfy a property constraint.
78      */

79     public static void validate(Declaration d) throws IllegalArgumentException JavaDoc
80     {
81         Collection JavaDoc<AnnotationMirror> mirrors = d.getAnnotationMirrors();
82
83         // for each annotations defined on the declaration, if the annotation
84
// is a PropertySet, ensure the values assigned to the properties
85
// satisfy all PropertySet and PropertyType constraints.
86
for (AnnotationMirror m : mirrors)
87         {
88             AnnotationType type = m.getAnnotationType();
89             AnnotationTypeDeclaration decl = m.getAnnotationType().getDeclaration();
90             if (decl.getAnnotation(PropertySet.class) != null)
91             {
92                 Iterator JavaDoc<Map.Entry JavaDoc<AnnotationTypeElementDeclaration, AnnotationValue>> i = m
93                         .getElementValues().entrySet().iterator();
94                 while (i.hasNext())
95                 {
96                     Map.Entry JavaDoc<AnnotationTypeElementDeclaration, AnnotationValue> entry = i
97                             .next();
98                     Collection JavaDoc<Annotation JavaDoc> annotations = getMemberTypeAnnotations(entry.getKey());
99                     if (annotations.size() > 0)
100                     {
101                         Annotation JavaDoc[] anArray = new Annotation JavaDoc[annotations.size()];
102                         annotations.toArray(anArray);
103                         validate(anArray, entry.getValue().getValue());
104                     }
105                 }
106
107                 //If a membership rule is defined on the property set, ensure the rule is satisfied.
108
if (decl.getAnnotation(MembershipRule.class) != null)
109                 {
110                     try
111                     {
112                         String JavaDoc declClassName = decl.getQualifiedName();
113
114                         TypeDeclaration owningClass = decl.getDeclaringType();
115                         if (owningClass != null)
116                             declClassName = owningClass.getQualifiedName() + "$" + decl.getSimpleName();
117                             
118                         Class JavaDoc clazz = Class.forName(declClassName);
119                         Annotation JavaDoc a = d.getAnnotation(clazz);
120                         validateMembership(a);
121                     }
122                     catch (ClassNotFoundException JavaDoc cnfe)
123                     {
124                         //should not happen
125
}
126                 }
127             }
128         }
129
130         // If the declaration is a class or interface, validate its methods
131
// and fields.
132
if (d instanceof TypeDeclaration)
133         {
134             TypeDeclaration td = ((TypeDeclaration) d);
135             for (Declaration md : td.getMethods())
136                 validate(md);
137             for (Declaration fd : td.getFields())
138                 validate(fd);
139         }
140         // If the delcaration is a method, validate its parameters.
141
else if (d instanceof MethodDeclaration)
142         {
143             for (Declaration pd : ((MethodDeclaration) d).getParameters())
144                 validate(pd);
145         }
146
147     }
148
149     private static Collection JavaDoc<Annotation JavaDoc> getMemberTypeAnnotations(
150             Declaration decl)
151     {
152         Collection JavaDoc<Annotation JavaDoc> annotations = new ArrayList JavaDoc<Annotation JavaDoc>();
153         for (AnnotationMirror am : decl.getAnnotationMirrors())
154         {
155             AnnotationType at = am.getAnnotationType();
156             try
157             {
158                 if (at.getContainingType() == null)
159                     continue;
160                 String JavaDoc containingClassName = at.getContainingType()
161                         .getDeclaration().getQualifiedName();
162                 if (containingClassName.equals(AnnotationMemberTypes.class
163                         .getName()))
164                 {
165                     String JavaDoc memberTypeName = at.getDeclaration().getSimpleName();
166                     Class JavaDoc clazz = Class.forName(containingClassName + "$"
167                             + memberTypeName);
168                     Annotation JavaDoc a = decl.getAnnotation(clazz);
169                     if (null != a)
170                     {
171                         annotations.add(a);
172                     }
173                 }
174             }
175             catch (ClassNotFoundException JavaDoc e)
176             {
177             }
178         }
179         return annotations;
180     }
181 }
182
Popular Tags