View Javadoc
1   /*
2    * Copyright 2009 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.javasource;
15  
16  /**
17   * A (abstract) base class which holds information about fields. Modeled closely after the Java
18   * Reflection API. This class is part of package which is used to create source code in memory.
19   *
20   * @author <a href="mailto:wguttmn AT codehaus DOT org">Werner Guttmann</a>
21   * @since 1.3
22   */
23  public class AbstractJField extends JAnnotatedElementHelper implements JMember {
24  
25    /** The set of modifiers for this JField. */
26    private JModifiers _modifiers = null;
27  
28    /** Type of this field. */
29    private final JType _type;
30  
31    /** Name of the field. */
32    private String _name = null;
33  
34    /** JavaDoc for this field. */
35    private JDocComment _comment = new JDocComment();
36  
37    /** Initialization string for this field. */
38    private String _initString = null;
39  
40    /** Indicates whether this field is of type date/time. */
41    private boolean _isDateTime = false;
42  
43    /** The Class in this JField has been declared. */
44    private JClass _declaringClass = null;
45  
46    /**
47     * Creates a new JField.
48     * 
49     * @param type JType of this new field.
50     * @param name Name of this new field.
51     */
52    public AbstractJField(final JType type, final String name) {
53      setName(name);
54  
55      _type = type;
56  
57      setModifiers(new JModifiers());
58    }
59  
60    /**
61     * Returns the JavaDoc comment describing this member.
62     *
63     * @return The JavaDoc comment describing this member, or null if no comment has been set.
64     */
65    public JDocComment getComment() {
66      return _comment;
67    }
68  
69    /**
70     * Returns the class in which this JField has been declared.
71     *
72     * @return The class in which this JField has been declared.
73     */
74    public JClass getDeclaringClass() {
75      return _declaringClass;
76    }
77  
78    /**
79     * Returns the initialization String for this JField.
80     *
81     * @return The initialization String for this JField, or null if no initialization String was
82     *         specified.
83     */
84    public String getInitString() {
85      return _initString;
86    }
87  
88    /**
89     * Returns the modifiers for this JField.
90     *
91     * @return The modifiers for this JField.
92     */
93    public JModifiers getModifiers() {
94      return _modifiers;
95    }
96  
97    /**
98     * Returns the name of this JField.
99     *
100    * @return The name of this JField.
101    */
102   public String getName() {
103     return _name;
104   }
105 
106   /**
107    * Returns the JType representing the type of this JField.
108    *
109    * @return The JType representing the type of this JField.
110    */
111   public JType getType() {
112     return _type;
113   }
114 
115   /**
116    * Sets the JavaDoc comment describing this JField.
117    *
118    * @param comment The JavaDoc comment for this JField.
119    */
120   public void setComment(final JDocComment comment) {
121     _comment = comment;
122   }
123 
124   /**
125    * Sets the JavaDoc comment describing this JField.
126    *
127    * @param comment The JavaDoc comment for this JField.
128    */
129   public void setComment(final String comment) {
130     _comment.setComment(comment);
131   }
132 
133   /**
134    * Sets the initialization string for this JField. This allows some flexibility in declaring
135    * default values.
136    *
137    * @param init The initialization string for this member.
138    */
139   public void setInitString(final String init) {
140     _initString = init;
141   }
142 
143   /**
144    * Sets the name of this JField.
145    *
146    * @param name The name of this JField.
147    */
148   public void setName(final String name) {
149     if (!JNaming.isValidJavaIdentifier(name)) {
150       String err = "'" + name + "' is ";
151       if (JNaming.isKeyword(name)) {
152         err += "a reserved word and may not be used as " + " a field name.";
153       } else {
154         err += "not a valid Java identifier.";
155       }
156       throw new IllegalArgumentException(err);
157     }
158     _name = name;
159   }
160 
161   /**
162    * Sets the access modifiers on this JField.
163    *
164    * @param modifiers The access modifiers to be used for this JField.
165    */
166   public void setModifiers(final JModifiers modifiers) {
167     _modifiers = modifiers;
168   }
169 
170   /**
171    * Sets the class that declares this JField.
172    *
173    * @param declaringClass The class in which this Jfield is declared.
174    */
175   protected void setDeclaringClass(final JClass declaringClass) {
176     _declaringClass = declaringClass;
177   }
178 
179   /**
180    * Indicates whether this JField instance represents a field of type date/time.
181    * 
182    * @return True if this field is of type date/time.
183    */
184   public boolean isDateTime() {
185     return _isDateTime;
186   }
187 
188   /**
189    * To indicate whether this JField instance represents a field of type date/time.
190    * 
191    * @param isDateTime True if this field is of type date/time.
192    */
193   public void setDateTime(final boolean isDateTime) {
194     _isDateTime = isDateTime;
195   }
196 
197   /**
198    * {@inheritDoc}
199    */
200   public String toString() {
201     return new StringBuilder().append(_modifiers).append(' ').append(_type).append(' ')
202         .append(_name).toString();
203   }
204 }