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  
46  package org.codehaus.modello.plugin.java.javasource;
47  
48  /*
49   * Copyright (c) 2004, Codehaus.org
50   *
51   * Permission is hereby granted, free of charge, to any person obtaining a copy of
52   * this software and associated documentation files (the "Software"), to deal in
53   * the Software without restriction, including without limitation the rights to
54   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
55   * of the Software, and to permit persons to whom the Software is furnished to do
56   * so, subject to the following conditions:
57   *
58   * The above copyright notice and this permission notice shall be included in all
59   * copies or substantial portions of the Software.
60   *
61   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
67   * SOFTWARE.
68   */
69  
70  /**
71   * A class which holds information about a field.
72   * Modelled closely after the Java Reflection API.
73   * This class is part of package which is used to
74   * create source code in memory.
75   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
76   * @version $Revision$ $Date$
77   **/
78  public class JField implements JMember
79  {
80  
81      /**
82       * The set of modifiers for this JField
83       **/
84      private JModifiers modifiers = null;
85  
86      private JType type = null;
87  
88      private String name = null;
89  
90      private JDocComment comment = null;
91  
92      private String initString = null;
93  
94      private JAnnotations annotations = null;
95  
96      /**
97       * The Class in this JField has been declared
98       **/
99      private JClass declaringClass = null;
100 
101     public JField( JType type, String name )
102     {
103 
104         setName( name );
105         this.type = type;
106         this.modifiers = new JModifiers();
107         this.modifiers.makePrivate();
108         comment = new JDocComment();
109         comment.appendComment( "Field " + name + "." );
110         annotations = new JAnnotations();
111     } //-- JField
112 
113 
114     /**
115      * Returns the comment describing this member.
116      * @return the comment describing this member, or
117      * null if no comment has been set.
118      **/
119     public JDocComment getComment()
120     {
121         return this.comment;
122     } //-- getComment
123 
124 
125     /**
126      * Returns the class in which this JField has been declared
127      * @return the class in which this JField has been declared
128      **/
129     public JClass getDeclaringClass()
130     {
131         return this.declaringClass;
132     } //-- getDeclaringClass
133 
134     /**
135      * Returns the initialization String for this JField
136      * @return the initialization String for this JField,
137      * or null if no initialization String was specified.
138      **/
139     public String getInitString()
140     {
141         return initString;
142     } //-- getInitString
143 
144 
145     /**
146      * Returns the modifiers for this JField
147      * @return the modifiers for this JField
148      **/
149     public JModifiers getModifiers()
150     {
151         return this.modifiers;
152     } //-- getModifiers
153 
154     /**
155      * Returns the name of this JField
156      * @return the name of this JField
157      **/
158     public String getName()
159     {
160         return this.name;
161     } //-- getName
162 
163     /**
164      * Returns the JType represting the type of this JField
165      * @return the JClass represting the type of this JField
166      **/
167     public JType getType()
168     {
169         return this.type;
170     } //-- getType
171 
172     /**
173      * Sets the comment describing this member.
174      * @param comment the JDocComment for this member
175      **/
176     public void setComment( JDocComment comment )
177     {
178         this.comment = comment;
179     } //-- setComment
180 
181     /**
182      * Sets the comment describing this member.
183      * @param comment the JDocComment for this member
184      **/
185     public void setComment( String comment )
186     {
187         if ( this.comment == null )
188         {
189             this.comment = new JDocComment();
190         }
191         this.comment.setComment( comment );
192     } //-- setComment
193 
194     /**
195      * Sets the initialization string for this JField;
196      * Allows some flexibility in declaring default values.
197      * @param init the initialization string for this member.
198      **/
199     public void setInitString( String init )
200     {
201         this.initString = init;
202     } //-- setInitString
203 
204     /**
205      * Sets the name of this JField
206      * @param name the name of this JField
207      * @exception java.lang.IllegalArgumentException when the
208      * name is not a valid Java member name, or if a member
209      * with the given name already exists in the declaring class
210      **/
211     public void setName( String name ) throws
212         IllegalArgumentException
213     {
214         if ( !JNaming.isValidJavaIdentifier( name ) )
215         {
216             String err = "'" + name + "' is ";
217             if ( JNaming.isKeyword( name ) )
218                 err += "a reserved word and may not be used as "
219                     + " a field name.";
220             else
221                 err += "not a valid Java identifier.";
222             throw new IllegalArgumentException( err );
223         }
224         this.name = name;
225     } //-- setName
226 
227     public void setModifiers( JModifiers modifiers )
228     {
229         this.modifiers = modifiers;
230     } //-- setModifiers
231 
232     protected void setDeclaringClass( JClass declaringClass )
233     {
234         this.declaringClass = declaringClass;
235     } //-- setDeclaringClass
236 
237     public String toString()
238     {
239         StringBuffer sb = new StringBuffer();
240         sb.append( modifiers.toString() );
241         sb.append( ' ' );
242         sb.append( type );
243         sb.append( ' ' );
244         sb.append( name );
245         return sb.toString();
246     } //-- toString
247 
248     /**
249      * @return the annotations
250      */
251     public JAnnotations getAnnotations()
252     {
253         return annotations;
254     }
255 
256     /**
257      * @param annotation the annotation to append
258      */
259     public void appendAnnotation( String annotation )
260     {
261         if ( annotations == null )
262         {
263             annotations = new JAnnotations();
264         }
265         annotations.appendAnnotation( annotation );
266     }
267 
268     /**
269      * @param annotations the annotations to set
270      */
271     public void setAnnotations( JAnnotations annotations )
272     {
273         this.annotations = annotations;
274     }
275 
276 } //-- JField
277