KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > jvm > InternalNameUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.jvm;
25
26 public final class InternalNameUtils
27 {
28     public static String JavaDoc dottifySlashesAndDollarSigns(String JavaDoc str)
29     { return _dottifySlashesAndDollarSigns( str ).toString(); }
30
31     public static String JavaDoc decodeType(String JavaDoc internalrep) throws TypeFormatException
32     { return _decodeType(internalrep).toString(); }
33
34     public static String JavaDoc decodeTypeList(String JavaDoc internalrep) throws TypeFormatException
35     {
36     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(64);
37     _decodeTypeList(internalrep, 0, sb);
38     return sb.toString();
39     }
40
41     public static boolean isPrimitive(char rep)
42     {
43     return (rep == 'Z' ||
44         rep == 'B' ||
45         rep == 'C' ||
46         rep == 'S' ||
47         rep == 'I' ||
48         rep == 'J' ||
49         rep == 'F' ||
50         rep == 'D' ||
51         rep == 'V');
52     }
53
54     private static void _decodeTypeList(String JavaDoc typeList, int start_pos, StringBuffer JavaDoc appendTo) throws TypeFormatException
55     {
56     if (appendTo.length() != 0)
57         appendTo.append(' ');
58
59     char c = typeList.charAt(start_pos);
60     if (isPrimitive(c))
61         {
62         appendTo.append( _decodeType( typeList.substring(start_pos, start_pos + 1) ) );
63         ++start_pos;
64         }
65     else
66         {
67         int stop_index;
68
69         if (c == '[')
70             {
71             int finger = start_pos + 1;
72             while (typeList.charAt(finger) == '[')
73                 ++finger;
74             if (typeList.charAt(finger) == 'L')
75                 {
76                 ++finger;
77                 while (typeList.charAt( finger ) != ';')
78                     ++finger;
79                 }
80             stop_index = finger;
81             }
82         else
83             {
84             stop_index = typeList.indexOf(';', start_pos);
85             if (stop_index < 0)
86                 throw new TypeFormatException(typeList.substring(start_pos) + " is neither a primitive nor semicolon terminated!");
87             }
88
89         appendTo.append( _decodeType( typeList.substring( start_pos, (start_pos = stop_index + 1) ) ) );
90         }
91     if (start_pos < typeList.length())
92         {
93         appendTo.append(',');
94         _decodeTypeList(typeList, start_pos, appendTo);
95         }
96     }
97
98     private static StringBuffer JavaDoc _decodeType(String JavaDoc type) throws TypeFormatException
99     {
100 // System.err.println("_decodeType: " + type);
101

102     int array_level = 0;
103     StringBuffer JavaDoc out;
104
105     char c = type.charAt(0);
106     
107     switch (c)
108         {
109         case 'Z':
110         out = new StringBuffer JavaDoc("boolean");
111         break;
112         case 'B':
113         out = new StringBuffer JavaDoc("byte");
114         break;
115         case 'C':
116         out = new StringBuffer JavaDoc("char");
117         break;
118         case 'S':
119         out = new StringBuffer JavaDoc("short");
120         break;
121         case 'I':
122         out = new StringBuffer JavaDoc("int");
123         break;
124         case 'J':
125         out = new StringBuffer JavaDoc("long");
126         break;
127         case 'F':
128         out = new StringBuffer JavaDoc("float");
129         break;
130         case 'D':
131         out = new StringBuffer JavaDoc("double");
132         break;
133         case 'V':
134         out = new StringBuffer JavaDoc("void");
135         break;
136         case '[':
137         ++array_level;
138         out = _decodeType(type.substring(1));
139         break;
140         case 'L':
141         out = _decodeSimpleClassType(type);
142         break;
143         default:
144         throw new TypeFormatException(type + " is not a valid inernal type name.");
145         }
146     for (int i = 0; i < array_level; ++i)
147         out.append("[]");
148     return out;
149     }
150
151     private static StringBuffer JavaDoc _decodeSimpleClassType(String JavaDoc type) throws TypeFormatException
152     {
153     int len = type.length();
154     if (type.charAt(0) != 'L' || type.charAt( len - 1 ) != ';')
155         throw new TypeFormatException(type + " is not a valid representation of a simple class type.");
156
157     return _dottifySlashesAndDollarSigns( type.substring(1, len - 1) );
158     }
159
160     private static StringBuffer JavaDoc _dottifySlashesAndDollarSigns(String JavaDoc s)
161     {
162     StringBuffer JavaDoc sb = new StringBuffer JavaDoc( s );
163     for (int i = 0, len = sb.length(); i < len; ++i)
164         {
165         char c = sb.charAt(i);
166         if ( c == '/' || c == '$')
167             sb.setCharAt(i, '.');
168         }
169     return sb;
170     }
171
172     private InternalNameUtils()
173     {}
174
175     public static void main(String JavaDoc[] argv)
176     {
177     try
178         {
179         //System.out.println( decodeType( (new String[0]).getClass().getName() ) );
180
System.out.println(decodeTypeList(argv[0]));
181         }
182     catch (Exception JavaDoc e)
183         { e.printStackTrace(); }
184     }
185
186 // public static String dottifySlashes(String name)
187
// {
188
// StringBuffer sb = new StringBuffer(name);
189

190 // int pos = name.indexOf('/', 0);
191
// while (pos > 0)
192
// {
193
// sb.setCharAt(pos, '.');
194
// ps = name.indexOf('/', ++pos);
195
// }
196
// return sb.toString();
197
// }
198

199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
Popular Tags