KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > generator > JVariable


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.generator;
20
21 import gcc.generator.JEntity;
22 import gcc.generator.JExpression;
23
24 import java.util.HashMap;
25
26 public class JVariable extends JEntity
27 {
28     protected static HashMap _typeMap = new HashMap(60);
29
30     protected Class _type;
31     protected String _typeDecl;
32     protected JExpression _initExpr;
33     protected boolean _isArray;
34
35     public JVariable( Class type, String name )
36     {
37         super( name );
38         setType(type);
39     }
40
41     public void setType( Class type )
42     {
43         _type = type;
44         calculateTypeDecl();
45     }
46
47     public Class getType()
48     {
49         return _type;
50     }
51
52     public String getTypeDecl()
53     {
54         return _typeDecl;
55     }
56
57     public void setInitExpression( JExpression initExpr )
58     {
59         _initExpr = initExpr;
60     }
61
62     public JExpression getInitExpression( )
63     {
64         return _initExpr;
65     }
66
67     public int hashCode()
68     {
69         return _type.hashCode() + _name.hashCode();
70     }
71
72     public boolean equals( Object other )
73     {
74         boolean rc = false;
75
76         if (other == this)
77         {
78             rc = true;
79         }
80         else if (other instanceof JVariable)
81         {
82             JVariable v = (JVariable)other;
83
84             rc = v._type.equals(_type);
85         }
86
87         return rc;
88     }
89
90     protected void calculateTypeDecl()
91     {
92         if (_type == null)
93         {
94             return;
95         }
96
97         _typeDecl = (String) _typeMap.get(_type);
98
99         if (_typeDecl == null)
100         {
101             synchronized( _typeMap )
102             {
103                 _typeDecl = _type.getName();
104
105                 if (_type.isArray())
106                 {
107                     _typeDecl = convertToTypeDecl( _typeDecl );
108                 }
109
110                 _typeMap.put( _type, _typeDecl );
111             }
112         }
113     }
114
115     protected String convertToTypeDecl( String typeName )
116     {
117         String rc = "";
118         char charAt = 0;
119         int i;
120
121         if (typeName != null && typeName.length() > 0)
122         {
123             for( i=0; i<typeName.length(); i++ )
124             {
125                 charAt = typeName.charAt(i);
126
127                 if (charAt == '[')
128                 {
129                     rc = rc + "[]";
130                 }
131                 else if (charAt == 'Z')
132                 {
133                     rc = "boolean" + rc;
134                 }
135                 else if (charAt == 'B')
136                 {
137                     rc = "byte" + rc;
138                 }
139                 else if (charAt == 'C')
140                 {
141                     rc = "char" + rc;
142                 }
143                 else if (charAt == 'L')
144                 {
145                     int semiIndex = typeName.indexOf( ";" );
146                     rc = typeName.substring( i + 1, semiIndex ) + rc;
147                     i = semiIndex;
148                 }
149                 else if (charAt == 'D')
150                 {
151                     rc = "double" + rc;
152                 }
153                 else if (charAt == 'F')
154                 {
155                     rc = "float" + rc;
156                 }
157                 else if (charAt == 'I')
158                 {
159                     rc = "int" + rc;
160                 }
161                 else if (charAt == 'J')
162                 {
163                     rc = "long" + rc;
164                 }
165                 else if (charAt == 'S')
166                 {
167                     rc = "short" + rc;
168                 }
169                 else
170                 {
171                     System.out.println( "Error: Invalid signature. typeName = " + typeName + ", charAt = " + charAt + ", i = " + i );
172                 }
173             }
174         }
175
176         return rc;
177     }
178
179     protected void showTypeInfo()
180     {
181         System.out.println( "getName() = " + _type.getName() );
182         System.out.println( "\tisArray() = " + _type.isArray() );
183         System.out.println( "\tisPrimitive() = " + _type.isPrimitive() );
184         System.out.println( "\ttoString() = " + _type.toString() );
185         System.out.println( "\ttypeDecl = " + getTypeDecl() );
186         System.out.println( "" );
187     }
188
189     protected void validateDeclType( String t )
190     {
191         String ct = getTypeDecl();
192         if (!t.equals(ct))
193         {
194             System.out.println( "Class Decl Type: '" + ct + "' does not match expected type: '" + t + "'" );
195         }
196     }
197
198     public static void main( String args[] )
199         throws Exception
200     {
201         (new JVariable( java.lang.String.class, "v" )).showTypeInfo();
202         (new JVariable( java.lang.String[].class, "v" )).showTypeInfo();
203         (new JVariable( java.lang.String[][].class, "v" )).showTypeInfo();
204
205         (new JVariable( int.class, "v" )).showTypeInfo();
206         (new JVariable( int[].class, "v" )).showTypeInfo();
207         (new JVariable( int[][].class, "v" )).showTypeInfo();
208
209         (new JVariable( java.lang.String.class, "v" )).validateDeclType( "java.lang.String");
210         (new JVariable( java.lang.String[].class, "v" )).validateDeclType( "java.lang.String[]");
211         (new JVariable( java.lang.String[][].class, "v" )).validateDeclType( "java.lang.String[][]");
212
213         (new JVariable( int.class, "v" )).validateDeclType( "int");
214         (new JVariable( int[].class, "v" )).validateDeclType( "int[]");
215         (new JVariable( int[][].class, "v" )).validateDeclType( "int[][]");
216     }
217 }
218
Popular Tags