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@exolab.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.exolab.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-2002 (C) Intalio, Inc. All Rights Reserved.
42   */
43  package org.exolab.javasource;
44  
45  /**
46   * Holds information about a given annotation type element.
47   *
48   * @author <a href="mailto:andrew DOT fawcett AT coda DOTcom">Andrew Fawcett</a>
49   * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
50   */
51  public final class JAnnotationTypeElement implements JMember {
52      //--------------------------------------------------------------------------
53  
54      private String      _name;
55      private JDocComment _comment;
56      private JType       _type;
57      private JModifiers  _modifiers;
58      private String      _default;
59  
60      //--------------------------------------------------------------------------
61  
62      /**
63       * Constructs a JAnnotationTypeElement with a given name and type.
64       *
65       * @param name Name of this new JAnnotatedTypeElement.
66       * @param type Type of this new JAnnotatedTypeElement.
67       */
68      public JAnnotationTypeElement(final String name, final JType type) {
69          setName(name);
70          
71          _type = type;
72          _modifiers = new JModifiers();
73          _comment = new JDocComment();
74          _comment.appendComment("Element " + name);
75      }
76  
77      //--------------------------------------------------------------------------
78  
79      /**
80       * Returns the modifiers for this JAnnotationTypeElement.
81       *
82       * @return The modifiers for this JAnnotationTypeElement.
83       */
84      public JModifiers getModifiers() {
85          return _modifiers;
86      }
87  
88      /**
89       * Sets the name of this JAnnotationTypeElement.
90       *
91       * @param name The name of this JAnnotationTypeElement.
92       */
93      public void setName(final String name) {
94          if (!JNaming.isValidJavaIdentifier(name)) {
95              String err = "'" + name + "' is ";
96              if (JNaming.isKeyword(name)) {
97                  err += "a reserved word and may not be used as "
98                      + " a field name.";
99              } else {
100                 err += "not a valid Java identifier.";
101             }
102             throw new IllegalArgumentException(err);
103         }
104         _name = name;
105     }
106 
107     /**
108      * Returns the name of this JAnnotationTypeElement.
109      *
110      * @return The name of this JAnnotationTypeElement.
111      */
112     public String getName() {
113         return _name;
114     }
115 
116     /**
117      * Returns the JType representing the type of this JAnnotationTypeElement.
118      *
119      * @return The JType representing the type of this JAnnotationTypeElement.
120      */
121     public JType getType() {
122         return _type;
123     }
124 
125     /**
126      * Returns the initialization string for this JAnnotationTypeElement.
127      *
128      * @return The initialization string for this JAnnotationTypeElement.
129      */
130     public String getDefaultString() {
131         return _default;
132     }
133 
134     /**
135      * Sets the initialization string for this JAnnotationTypeElement. This
136      * method allows some flexibility in declaring default values.
137      *
138      * @param defaultString The default string for this member.
139      */
140     public void setDefaultString(final String defaultString) {
141         _default = defaultString;
142     }
143 
144     /**
145      * Sets the JavaDoc comment describing this member.
146      *
147      * @param comment The JDocComment for this member.
148      */
149     public void setComment(final JDocComment comment) {
150         _comment = comment;
151     }
152 
153     /**
154      * Sets the JavaDoc comment describing this member.
155      *
156      * @param comment The JDocComment for this member.
157      */
158     public void setComment(final String comment) {
159         if (_comment == null) {
160             _comment = new JDocComment();
161         }
162         _comment.setComment(comment);
163     }
164 
165     /**
166      * Returns the JavaDoc comment describing this member.
167      *
168      * @return The comment describing this member, or null if no comment has
169      *         been set.
170      */
171     public JDocComment getComment() {
172         return _comment;
173     }
174 
175     //--------------------------------------------------------------------------
176 
177     /**
178      * Outputs the annotation type element to the provided JSourceWriter.
179      *
180      * @param jsw the JSourceWriter to print this element to
181      */
182     public void print(final JSourceWriter jsw) {
183         if (_comment != null) { _comment.print(jsw); }
184         jsw.write(_type.toString());
185         jsw.write(" ");
186         jsw.write(_name);
187         jsw.write("()");
188         if (_default != null) {
189             jsw.write(" default ");
190             jsw.write(_default);
191         }
192         jsw.write(";");
193     }
194 
195     //--------------------------------------------------------------------------
196 }