View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@codehaus.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.codehaus.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999-2000 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  package org.codehaus.modello.plugin.java.javasource;
46  
47  /*
48   * Copyright (c) 2004, Codehaus.org
49   *
50   * Permission is hereby granted, free of charge, to any person obtaining a copy of
51   * this software and associated documentation files (the "Software"), to deal in
52   * the Software without restriction, including without limitation the rights to
53   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
54   * of the Software, and to permit persons to whom the Software is furnished to do
55   * so, subject to the following conditions:
56   *
57   * The above copyright notice and this permission notice shall be included in all
58   * copies or substantial portions of the Software.
59   *
60   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
63   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
64   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
65   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
66   * SOFTWARE.
67   */
68  
69  /**
70   * A class which holds information about a field.
71   * Modelled closely after the Java Reflection API.
72   * This class is part of package which is used to
73   * create source code in memory.
74   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
75   * @version $Revision$ $Date$
76   **/
77  public class JField implements JMember {
78  
79      /**
80       * The set of modifiers for this JField
81       **/
82      private JModifiers modifiers = null;
83  
84      private JType type = null;
85  
86      private String name = null;
87  
88      private JDocComment comment = null;
89  
90      private String initString = null;
91  
92      private JAnnotations annotations = null;
93  
94      /**
95       * The Class in this JField has been declared
96       **/
97      private JClass declaringClass = null;
98  
99      public JField(JType type, String name) {
100 
101         setName(name);
102         this.type = type;
103         this.modifiers = new JModifiers();
104         this.modifiers.makePrivate();
105         comment = new JDocComment();
106         comment.appendComment("Field " + name + ".");
107         annotations = new JAnnotations();
108     } // -- JField
109 
110     /**
111      * Returns the comment describing this member.
112      * @return the comment describing this member, or
113      * null if no comment has been set.
114      **/
115     public JDocComment getComment() {
116         return this.comment;
117     } // -- getComment
118 
119     /**
120      * Returns the class in which this JField has been declared
121      * @return the class in which this JField has been declared
122      **/
123     public JClass getDeclaringClass() {
124         return this.declaringClass;
125     } // -- getDeclaringClass
126 
127     /**
128      * Returns the initialization String for this JField
129      * @return the initialization String for this JField,
130      * or null if no initialization String was specified.
131      **/
132     public String getInitString() {
133         return initString;
134     } // -- getInitString
135 
136     /**
137      * Returns the modifiers for this JField
138      * @return the modifiers for this JField
139      **/
140     public JModifiers getModifiers() {
141         return this.modifiers;
142     } // -- getModifiers
143 
144     /**
145      * Returns the name of this JField
146      * @return the name of this JField
147      **/
148     public String getName() {
149         return this.name;
150     } // -- getName
151 
152     /**
153      * Returns the JType represting the type of this JField
154      * @return the JClass represting the type of this JField
155      **/
156     public JType getType() {
157         return this.type;
158     } // -- getType
159 
160     /**
161      * Sets the comment describing this member.
162      * @param comment the JDocComment for this member
163      **/
164     public void setComment(JDocComment comment) {
165         this.comment = comment;
166     } // -- setComment
167 
168     /**
169      * Sets the comment describing this member.
170      * @param comment the JDocComment for this member
171      **/
172     public void setComment(String comment) {
173         if (this.comment == null) {
174             this.comment = new JDocComment();
175         }
176         this.comment.setComment(comment);
177     } // -- setComment
178 
179     /**
180      * Sets the initialization string for this JField;
181      * Allows some flexibility in declaring default values.
182      * @param init the initialization string for this member.
183      **/
184     public void setInitString(String init) {
185         this.initString = init;
186     } // -- setInitString
187 
188     /**
189      * Sets the name of this JField
190      * @param name the name of this JField
191      * @exception java.lang.IllegalArgumentException when the
192      * name is not a valid Java member name, or if a member
193      * with the given name already exists in the declaring class
194      **/
195     public void setName(String name) throws IllegalArgumentException {
196         if (!JNaming.isValidJavaIdentifier(name)) {
197             String err = "'" + name + "' is ";
198             if (JNaming.isKeyword(name)) err += "a reserved word and may not be used as " + " a field name.";
199             else err += "not a valid Java identifier.";
200             throw new IllegalArgumentException(err);
201         }
202         this.name = name;
203     } // -- setName
204 
205     public void setModifiers(JModifiers modifiers) {
206         this.modifiers = modifiers;
207     } // -- setModifiers
208 
209     protected void setDeclaringClass(JClass declaringClass) {
210         this.declaringClass = declaringClass;
211     } // -- setDeclaringClass
212 
213     public String toString() {
214         StringBuilder sb = new StringBuilder();
215         sb.append(modifiers.toString());
216         sb.append(' ');
217         sb.append(type);
218         sb.append(' ');
219         sb.append(name);
220         return sb.toString();
221     } // -- toString
222 
223     /**
224      * @return the annotations
225      */
226     public JAnnotations getAnnotations() {
227         return annotations;
228     }
229 
230     /**
231      * @param annotation the annotation to append
232      */
233     public void appendAnnotation(String annotation) {
234         if (annotations == null) {
235             annotations = new JAnnotations();
236         }
237         annotations.appendAnnotation(annotation);
238     }
239 
240     /**
241      * @param annotations the annotations to set
242      */
243     public void setAnnotations(JAnnotations annotations) {
244         this.annotations = annotations;
245     }
246 } // -- JField