View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  
18  package org.apache.jdo.impl.enhancer.meta.prop;
19  
20  import java.lang.reflect.Modifier;
21  
22  
23  /***
24   * A class to hold the properties of a field.
25   */
26  final class JDOField
27  {
28      /***
29       * The name of the field.
30       */
31      final private String name;
32      
33      /***
34       * The type of the field.
35       */
36      private String type = null;
37      
38      /***
39       * The access modifier of the field.
40       */
41      private int modifiers = Modifier.PRIVATE;
42      
43      /***
44       * The JDO modifier of the field.
45       */
46      private String jdoModifier = null;
47      
48      /***
49       * The annotation type.
50       */
51      private String annotationType = null;
52      
53      /***
54       * Creates a new object with the given name.
55       *
56       * @param  name  The name of the field.
57       */
58      JDOField(String name)
59      {
60          this.name = name;
61      }
62      
63      /***
64       * Returns the name of the field.
65       *
66       * @return  The name of the field.
67       */
68      public String getName()
69      {
70          return name;
71      }
72      
73      /***
74       * Sets the type of the field. The given classname should have a
75       * natural form(with dots) and is converted to a VM-similar
76       * notation(with slashes).
77       *
78       * @param  type  The natural classname.
79       */
80      public void setType(String type)
81      {
82          this.type = NameHelper.fromCanonicalClassName(type);
83      }
84      
85      /***
86       * Returns the type of the field.
87       *
88       * @return  The type of the field.
89       */
90      public String getType()
91      {
92          return type;
93      }
94      
95      /***
96       * Returns the modifiers of the field.
97       *
98       * @param modifiers  The modifiers of the field.
99       */
100     public void setModifiers(int modifiers)
101     {
102         this.modifiers = modifiers;
103     }
104     
105     /***
106      * Returns the modifiers of the field.
107      *
108      * @return  The modifiers of the field.
109      */
110     public int getModifiers()
111     {
112         return modifiers;
113     }
114     
115     /***
116      * Sets the annotation type of the field.
117      *
118      * @param  annotationType annotation type
119      */
120     public void setAnnotationType(String annotationType)
121     {
122         this.annotationType = annotationType;
123     }
124     
125     /***
126      * Returns whether the field is annotated.
127      *
128      * @return  true if annotated field
129      */
130     public boolean isAnnotated()
131     {
132         return annotationType != null;
133     }
134     
135     /***
136      * Returns whether the field is a key primary.
137      *
138      * @return  true if primary key.
139      */
140     public boolean isKey()
141     {
142         return (annotationType != null
143                 && annotationType.equals(
144                     MetaDataProperties.ANNOTATION_TYPE_KEY));
145     }
146     
147     /***
148      * Is the field in the default fetch group?
149      *
150      * @return  Is the field in the default fetch group?
151      */
152     public boolean isInDefaultFetchGroup()
153     {
154         return (annotationType != null
155                 && annotationType.equals(
156                     MetaDataProperties.ANNOTATION_TYPE_DFG));
157     }
158     
159     /***
160      * Sets the modifiers of the field.
161      *
162      * @param jdoModifier  the persistence modifier of the field
163      */
164     public void setJdoModifier(String jdoModifier)
165     {
166         this.jdoModifier = jdoModifier;
167     }
168     
169     /***
170      * Returns whether the field is declared transient.
171      *
172      * @return  true if declared transient field.
173      * @see  #setJdoModifier
174      */
175     public boolean isKnownTransient()
176     {
177         return (jdoModifier != null
178                 && jdoModifier.equals(MetaDataProperties.JDO_TRANSIENT));
179     }
180     
181     /***
182      * Returns whether the field is persistent.
183      *
184      * @return  true if persistent field.
185      * @see  #setJdoModifier
186      */
187     public boolean isPersistent()
188     {
189         return (jdoModifier != null
190                 && jdoModifier.equals(MetaDataProperties.JDO_PERSISTENT));
191     }
192     
193     /***
194      * Returns whether the field is transactional.
195      *
196      * @return  true if transactional field
197      * @see  #setJdoModifier
198      */
199     public boolean isTransactional()
200     {
201         return (jdoModifier != null
202                 && jdoModifier.equals(MetaDataProperties.JDO_TRANSACTIONAL));
203     }
204     
205     /***
206      * Returns whether the field is managed.
207      *
208      * @return  true if managed field
209      */
210     public boolean isManaged()
211     {
212         return (isPersistent() || isTransactional());
213     }
214     
215     /***
216      * Creates a string-representation of the object.
217      *
218      * @return  The string-representation of the object.
219      */
220     public String toString()
221     {
222         return ('<' + "name:" + name
223                 + ',' + MetaDataProperties.PROPERTY_TYPE
224                 + ':' + type
225                 + ',' + MetaDataProperties.PROPERTY_ACCESS_MODIFIER
226                 + ':' + Modifier.toString(modifiers)
227                 + ',' + MetaDataProperties.PROPERTY_JDO_MODIFIER
228                 + ':' + jdoModifier
229                 + ',' + MetaDataProperties.PROPERTY_ANNOTATION_TYPE
230                 + ':' + annotationType + '>');
231     }
232 }