View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
32   */
33  package org.exolab.javasource;
34  
35  /**
36   * Holds information about a given annotation type element.
37   *
38   * @author <a href="mailto:andrew DOT fawcett AT coda DOTcom">Andrew Fawcett</a>
39   * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
40   */
41  public final class JAnnotationTypeElement implements JMember {
42    // --------------------------------------------------------------------------
43  
44    private String _name;
45    private JDocComment _comment;
46    private JType _type;
47    private JModifiers _modifiers;
48    private String _default;
49  
50    // --------------------------------------------------------------------------
51  
52    /**
53     * Constructs a JAnnotationTypeElement with a given name and type.
54     *
55     * @param name Name of this new JAnnotatedTypeElement.
56     * @param type Type of this new JAnnotatedTypeElement.
57     */
58    public JAnnotationTypeElement(final String name, final JType type) {
59      setName(name);
60  
61      _type = type;
62      _modifiers = new JModifiers();
63      _comment = new JDocComment();
64      _comment.appendComment("Element " + name);
65    }
66  
67    // --------------------------------------------------------------------------
68  
69    /**
70     * Returns the modifiers for this JAnnotationTypeElement.
71     *
72     * @return The modifiers for this JAnnotationTypeElement.
73     */
74    public JModifiers getModifiers() {
75      return _modifiers;
76    }
77  
78    /**
79     * Sets the name of this JAnnotationTypeElement.
80     *
81     * @param name The name of this JAnnotationTypeElement.
82     */
83    public void setName(final String name) {
84      if (!JNaming.isValidJavaIdentifier(name)) {
85        String err = "'" + name + "' is ";
86        if (JNaming.isKeyword(name)) {
87          err += "a reserved word and may not be used as " + " a field name.";
88        } else {
89          err += "not a valid Java identifier.";
90        }
91        throw new IllegalArgumentException(err);
92      }
93      _name = name;
94    }
95  
96    /**
97     * Returns the name of this JAnnotationTypeElement.
98     *
99     * @return The name of this JAnnotationTypeElement.
100    */
101   public String getName() {
102     return _name;
103   }
104 
105   /**
106    * Returns the JType representing the type of this JAnnotationTypeElement.
107    *
108    * @return The JType representing the type of this JAnnotationTypeElement.
109    */
110   public JType getType() {
111     return _type;
112   }
113 
114   /**
115    * Returns the initialization string for this JAnnotationTypeElement.
116    *
117    * @return The initialization string for this JAnnotationTypeElement.
118    */
119   public String getDefaultString() {
120     return _default;
121   }
122 
123   /**
124    * Sets the initialization string for this JAnnotationTypeElement. This method allows some
125    * flexibility in declaring default values.
126    *
127    * @param defaultString The default string for this member.
128    */
129   public void setDefaultString(final String defaultString) {
130     _default = defaultString;
131   }
132 
133   /**
134    * Sets the JavaDoc comment describing this member.
135    *
136    * @param comment The JDocComment for this member.
137    */
138   public void setComment(final JDocComment comment) {
139     _comment = comment;
140   }
141 
142   /**
143    * Sets the JavaDoc comment describing this member.
144    *
145    * @param comment The JDocComment for this member.
146    */
147   public void setComment(final String comment) {
148     if (_comment == null) {
149       _comment = new JDocComment();
150     }
151     _comment.setComment(comment);
152   }
153 
154   /**
155    * Returns the JavaDoc comment describing this member.
156    *
157    * @return The comment describing this member, or null if no comment has been set.
158    */
159   public JDocComment getComment() {
160     return _comment;
161   }
162 
163   // --------------------------------------------------------------------------
164 
165   /**
166    * Outputs the annotation type element to the provided JSourceWriter.
167    *
168    * @param jsw the JSourceWriter to print this element to
169    */
170   public void print(final JSourceWriter jsw) {
171     if (_comment != null) {
172       _comment.print(jsw);
173     }
174     jsw.write(_type.toString());
175     jsw.write(" ");
176     jsw.write(_name);
177     jsw.write("()");
178     if (_default != null) {
179       jsw.write(" default ");
180       jsw.write(_default);
181     }
182     jsw.write(";");
183   }
184 
185   // --------------------------------------------------------------------------
186 }