KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > AptProperty


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

19 import java.util.Collection JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
23 import com.sun.mirror.declaration.AnnotationMirror;
24 import com.sun.mirror.declaration.AnnotationValue;
25 import com.sun.mirror.type.AnnotationType;
26 import com.sun.mirror.type.PrimitiveType;
27
28 import org.apache.beehive.controls.api.packaging.FeatureInfo;
29 import org.apache.beehive.controls.api.packaging.PropertyInfo;
30 import org.apache.beehive.controls.runtime.generator.apt.TwoPhaseAnnotationProcessor;
31
32 /**
33  * The AptProperty class represents a control Property where the property attributes
34  * are derived using APT metadata
35  */

36 public class AptProperty
37 {
38     /**
39      * Constructs a new AptProperty instance
40      * from APT metadata
41      * @param propertySet the declaring PropertySet
42      * @param propDecl the declration of the property annotation type element
43      */

44     public AptProperty(AptPropertySet propertySet, AnnotationTypeElementDeclaration propDecl,
45                        TwoPhaseAnnotationProcessor ap)
46     {
47         _propertySet = propertySet;
48         _propDecl = propDecl;
49         _ap = ap;
50
51         //
52
// Primitive properties must specify a default value, to provide consistent semantics
53
// in cases where no value has been set by annotation, client, or configuration. Object
54
// typed properties have an optional default, and 'null' in this context means that the
55
// property value has not been set.
56
//
57
if (propDecl.getReturnType() instanceof PrimitiveType &&
58             propDecl.getDefaultValue() == null)
59         {
60             _ap.printError( propDecl, "property.primitive.without.default", propDecl.getSimpleName() );
61         }
62     }
63
64     /**
65      * Returns the PropertySet associated with the Property
66      */

67     public AptPropertySet getPropertySet() { return _propertySet; }
68
69     /**
70      * Returns the base property name. The associated accessor methods will have the
71      * form set{name} and get{name}
72      */

73     public String JavaDoc getAccessorName()
74     {
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         sb.append(_propertySet.getPrefix());
77     
78         String JavaDoc name = getName();
79         sb.append(Character.toUpperCase(name.charAt(0)));
80         if (name.length() > 0)
81             sb.append(name.substring(1));
82         return sb.toString();
83     }
84
85     /**
86      * Returns the name of the property reading accessor method
87      */

88     public String JavaDoc getReadMethod()
89     {
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91         if (getType().equals("boolean"))
92             sb.append("is");
93         else
94             sb.append("get");
95         sb.append(getAccessorName());
96         return sb.toString();
97     }
98
99     /**
100      * Returns the name of the property writing accessor method
101      */

102     public String JavaDoc getWriteMethod()
103     {
104         return "set" + getAccessorName();
105     }
106
107     /**
108      * Returns the name associated with this Property in the PropertySet
109      */

110     public String JavaDoc getName()
111     {
112         if ( _propDecl == null )
113             return "";
114
115         //
116
// Use the member name of the property method in the property set
117
//
118
return _propDecl.getSimpleName();
119     }
120
121     /**
122      * Returns the static final field name containing the key for this Property
123      */

124     public String JavaDoc getKeyName()
125     {
126         return getAccessorName() + "Key";
127     }
128
129     /**
130      * Returns the type of the Property
131      */

132     public String JavaDoc getType()
133     {
134         if ( _propDecl == null || _propDecl.getReturnType() == null )
135             return "";
136         
137         return _propDecl.getReturnType().toString();
138     }
139
140     /**
141      * Returns true if the property is an annotation type, false otherwise
142      */

143     public boolean isAnnotation()
144     {
145         if ( _propDecl == null )
146             return false;
147
148         return _propDecl.getReturnType() instanceof AnnotationType;
149     }
150
151     /**
152      * Returns any PropertyInfo associated with the property (or null if none)
153      */

154     public PropertyInfo getPropertyInfo()
155     {
156         if ( _propDecl == null )
157             return null;
158         
159         return _propDecl.getAnnotation(PropertyInfo.class);
160     }
161
162     /**
163      * Returns any FeatureInfo associated with the property (or null if none)
164      */

165     public FeatureInfo getFeatureInfo()
166     {
167         if ( _propDecl == null )
168             return null;
169         
170         return _propDecl.getAnnotation(FeatureInfo.class);
171     }
172
173     /**
174      * Returns 'true' is the property is a bound property that will support registration of
175      * a PropertyChangeListener for change notifications.
176      */

177     public boolean isBound()
178     {
179         //
180
// Constrained properties are implicitly bound. Refer to section 7.4.3 of the JavaBeans
181
// spec for the rationale.
182
//
183
PropertyInfo propInfo = getPropertyInfo();
184         return propInfo != null && (propInfo.bound() || propInfo.constrained());
185     }
186
187     /**
188      * Returns 'true' is the property is a constrained property that will support registration of
189      * a VetoableChangeListener for vetoable change notifications.
190      */

191     public boolean isConstrained()
192     {
193         PropertyInfo propInfo = getPropertyInfo();
194         return propInfo != null && propInfo.constrained();
195     }
196
197     /**
198      * Returns the class name of the property editor class, or null
199      */

200     public String JavaDoc getEditorClass()
201     {
202         PropertyInfo pi = getPropertyInfo();
203         if (pi == null)
204             return null;
205
206         //
207
// This is trickier, because APT doesn't allow access to Class-valued annotations,
208
// because the type may not yet have been compiled.
209
//
210
Collection JavaDoc<AnnotationMirror> annotMirrors = _propDecl.getAnnotationMirrors();
211         for (AnnotationMirror am: annotMirrors)
212         {
213             if (am.getAnnotationType().toString().equals(
214                     "org.apache.beehive.controls.api.packaging.PropertyInfo"))
215             {
216                 Map JavaDoc<AnnotationTypeElementDeclaration,AnnotationValue> avs =
217                     am.getElementValues();
218                 for (AnnotationTypeElementDeclaration ated: avs.keySet())
219                 {
220                     if (ated.toString().equals("editorClass()"))
221                     {
222                         //
223
// Get the annotation value, and ignore the default value which implies
224
// no editor class (because 'null' cannot be a default value)
225
//
226
String JavaDoc editorClass = avs.get(ated).getValue().toString();
227                         if (editorClass.equals("org.apache.beehive.controls.api.packaging.PropertyInfo.NoEditor.class"))
228                             return null;
229
230                         return editorClass;
231                     }
232                 }
233                 break;
234             }
235         }
236         return null;
237     }
238
239     AnnotationTypeElementDeclaration _propDecl;
240     private AptPropertySet _propertySet;
241     TwoPhaseAnnotationProcessor _ap;
242 }
243
Popular Tags