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  
46  
47  package org.codehaus.modello.plugin.java.javasource;
48  
49  /*
50   * Copyright (c) 2004, Codehaus.org
51   *
52   * Permission is hereby granted, free of charge, to any person obtaining a copy of
53   * this software and associated documentation files (the "Software"), to deal in
54   * the Software without restriction, including without limitation the rights to
55   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
56   * of the Software, and to permit persons to whom the Software is furnished to do
57   * so, subject to the following conditions:
58   *
59   * The above copyright notice and this permission notice shall be included in all
60   * copies or substantial portions of the Software.
61   *
62   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
65   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
66   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
67   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
68   * SOFTWARE.
69   */
70  
71  /**
72   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
73   * @version $Revision$ $Date$
74   **/
75  public class JType
76  {
77  
78      public static final JType BOOLEAN = new JType( "boolean" );
79      public static final JType BYTE = new JType( "byte" );
80      public static final JType CHAR = new JType( "char" );
81      public static final JType DOUBLE = new JType( "double" );
82      public static final JType FLOAT = new JType( "float" );
83      public static final JType INT = new JType( "int" );
84      public static final JType LONG = new JType( "long" );
85      public static final JType SHORT = new JType( "short" );
86  
87      private String name = null;
88  
89      private boolean _isArray = false;
90  
91      /**
92       * used for array types
93       **/
94      private JType _componentType = null;
95  
96      /**
97       * Creates a new JType with the given name
98       * @param name the name of the type
99       **/
100     public JType( String name )
101     {
102         super();
103         this.name = name;
104     } //-- JType
105 
106     /**
107      * Creates a JType Object representing an array of the current
108      * JType.
109      * @return the new JType which is represents an array.
110      * @deprecated removed in javasource 1.3rc1, replaced by JArrayType
111      **/
112     public final JType createArray()
113     {
114         JType jType = new JType( getName() );
115         jType._isArray = true;
116         jType._componentType = this;
117         return jType;
118     } //-- createArray
119 
120     /**
121      * If this JType is an array this method will returns the component type
122      * of the array, otherwise null will be returned.
123      * @return the component JType if this JType is an array, otherwise null.
124      **/
125     public JType getComponentType()
126     {
127         return _componentType;
128     } //-- getComponentType
129 
130     public String getLocalName()
131     {
132 
133         //-- use getName method in case it's been overloaded
134         String name = getName();
135 
136         if ( name == null ) return null;
137         int idx = name.lastIndexOf( '.' );
138         if ( idx >= 0 )
139         {
140             name = name.substring( idx + 1 );
141         }
142         return name;
143     } //-- getLocalName
144 
145     public String getName()
146     {
147         return this.name;
148     } //-- getName
149 
150     /**
151      * Checks to see if this JType represents an array.
152      * @return true if this JType represents an array, otherwise false
153      **/
154     public final boolean isArray()
155     {
156         return _isArray;
157     }
158 
159     /**
160      * Checks to see if this JType represents a primitive
161      * @return true if this JType represents a primitive, otherwise false
162      **/
163     public boolean isPrimitive()
164     {
165         return ( ( this == BOOLEAN ) ||
166             ( this == BYTE ) ||
167             ( this == CHAR ) ||
168             ( this == DOUBLE ) ||
169             ( this == FLOAT ) ||
170             ( this == INT ) ||
171             ( this == LONG ) ||
172             ( this == SHORT ) );
173     } //-- isPrimitive
174 
175     /**
176      * Returns the String representation of this JType, which is
177      * simply the name of this type.
178      * @return the String representation of this JType
179      **/
180     public String toString()
181     {
182 
183         if ( _isArray )
184             return _componentType.toString() + "[]";
185         else
186             return this.name;
187 
188     } //-- toString
189 
190     //---------------------/
191     //- Protected methods -/
192     //---------------------/
193 
194     /**
195      * Allows subtypes, such as JClass to alter the package to which
196      * this JType belongs
197      * @param newPackage the new package to which this JType belongs
198      * <BR>
199      * <B>Note:</B> The package name cannot be changed on a primitive type.
200      **/
201     protected void changePackage( String newPackage )
202     {
203 
204         if ( this.name == null ) return;
205         if ( this.isPrimitive() ) return;
206 
207         String localName = null;
208         int idx = name.lastIndexOf( '.' );
209         if ( idx >= 0 )
210             localName = this.name.substring( idx + 1 );
211         else
212             localName = this.name;
213 
214         if ( ( newPackage == null ) || ( newPackage.length() == 0 ) )
215             this.name = localName;
216         else
217             this.name = newPackage + "." + localName;
218 
219     } //-- changePackage
220 
221 } //-- JType