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 (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   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
71   * @version $Revision$ $Date$
72   **/
73  public class JType {
74  
75      public static final JType BOOLEAN = new JType("boolean");
76      public static final JType BYTE = new JType("byte");
77      public static final JType CHAR = new JType("char");
78      public static final JType DOUBLE = new JType("double");
79      public static final JType FLOAT = new JType("float");
80      public static final JType INT = new JType("int");
81      public static final JType LONG = new JType("long");
82      public static final JType SHORT = new JType("short");
83  
84      private String name = null;
85  
86      private boolean _isArray = false;
87  
88      /**
89       * used for array types
90       **/
91      private JType _componentType = null;
92  
93      /**
94       * Creates a new JType with the given name
95       * @param name the name of the type
96       **/
97      public JType(String name) {
98          super();
99          this.name = name;
100     } // -- JType
101 
102     /**
103      * Creates a JType Object representing an array of the current
104      * JType.
105      * @return the new JType which is represents an array.
106      * @deprecated removed in javasource 1.3rc1, replaced by JArrayType
107      **/
108     public final JType createArray() {
109         JType jType = new JType(getName());
110         jType._isArray = true;
111         jType._componentType = this;
112         return jType;
113     } // -- createArray
114 
115     /**
116      * If this JType is an array this method will returns the component type
117      * of the array, otherwise null will be returned.
118      * @return the component JType if this JType is an array, otherwise null.
119      **/
120     public JType getComponentType() {
121         return _componentType;
122     } // -- getComponentType
123 
124     public String getLocalName() {
125 
126         // -- use getName method in case it's been overloaded
127         String name = getName();
128 
129         if (name == null) return null;
130         int idx = name.lastIndexOf('.');
131         if (idx >= 0) {
132             name = name.substring(idx + 1);
133         }
134         return name;
135     } // -- getLocalName
136 
137     public String getName() {
138         return this.name;
139     } // -- getName
140 
141     /**
142      * Checks to see if this JType represents an array.
143      * @return true if this JType represents an array, otherwise false
144      **/
145     public final boolean isArray() {
146         return _isArray;
147     }
148 
149     /**
150      * Checks to see if this JType represents a primitive
151      * @return true if this JType represents a primitive, otherwise false
152      **/
153     public boolean isPrimitive() {
154         return ((this == BOOLEAN)
155                 || (this == BYTE)
156                 || (this == CHAR)
157                 || (this == DOUBLE)
158                 || (this == FLOAT)
159                 || (this == INT)
160                 || (this == LONG)
161                 || (this == SHORT));
162     } // -- isPrimitive
163 
164     /**
165      * Returns the String representation of this JType, which is
166      * simply the name of this type.
167      * @return the String representation of this JType
168      **/
169     public String toString() {
170 
171         if (_isArray) return _componentType.toString() + "[]";
172         else return this.name;
173     } // -- toString
174 
175     // ---------------------/
176     // - Protected methods -/
177     // ---------------------/
178 
179     /**
180      * Allows subtypes, such as JClass to alter the package to which
181      * this JType belongs
182      * @param newPackage the new package to which this JType belongs
183      * <BR>
184      * <B>Note:</B> The package name cannot be changed on a primitive type.
185      **/
186     protected void changePackage(String newPackage) {
187 
188         if (this.name == null) return;
189         if (this.isPrimitive()) return;
190 
191         String localName = null;
192         int idx = name.lastIndexOf('.');
193         if (idx >= 0) localName = this.name.substring(idx + 1);
194         else localName = this.name;
195 
196         if ((newPackage == null) || (newPackage.length() == 0)) this.name = localName;
197         else this.name = newPackage + "." + localName;
198     } // -- changePackage
199 } // -- JType