KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > TypeHelper


1 package alt.jiapi.reflect;
2
3 import java.util.ArrayList JavaDoc;
4 /**
5  * Class TypeHelper.
6  *
7  * @author Mika Riekkinen
8  */

9 class TypeHelper {
10     static String JavaDoc typeToDescriptor(String JavaDoc typeName) {
11         int idx = typeName.indexOf('[');
12         String JavaDoc type = null;
13         if (idx == -1) {
14             type = typeName;
15         }
16         else {
17             type = typeName.substring(0, idx).trim();
18         }
19
20         StringBuffer JavaDoc dBuf = new StringBuffer JavaDoc();
21         if (idx > 0) {
22             for (int i = idx; i < typeName.length(); i++) {
23                 if (typeName.charAt(i) == '[') {
24                     dBuf.append('[');
25                 }
26             }
27         }
28
29         String JavaDoc desc = null;
30         // Primitive types
31
if ("byte".equals(type)) {
32             dBuf.append("B");
33         }
34         else if ("char".equals(type)) {
35             dBuf.append("C");
36         }
37         else if ("double".equals(type)) {
38             dBuf.append("D");
39         }
40         else if ("float".equals(type)) {
41             dBuf.append("F");
42         }
43         else if ("int".equals(type)) {
44             dBuf.append("I");
45         }
46         else if ("long".equals(type)) {
47             dBuf.append("J");
48         }
49         else if ("short".equals(type)) {
50             dBuf.append("S");
51         }
52         else if ("boolean".equals(type)) {
53             dBuf.append("Z");
54         }
55         else if ("void".equals(type)) {
56             dBuf.append("V");
57         }
58         else {
59             // Object type
60
dBuf.append('L');
61             dBuf.append(type.replace('.', '/'));
62             dBuf.append(';');
63             //dBuf = "L" + type.replace('.', '/') + ";";
64
}
65
66         return dBuf.toString();
67     }
68
69
70     /**
71      * Converts internal representation of descriptor to
72      * simple java form like 'double' or 'java.lang.Object'
73
74      B byte signed byte
75      C char Unicode character
76      D double double-precision floating-point value
77      F float single-precision floating-point value
78      I int integer
79      J long long integer
80      L<classname>; reference an instance of class <classname>
81      S short signed short
82      Z boolean true or false
83      [ reference one array dimension
84
85      */

86     static String JavaDoc descriptorToType(String JavaDoc descriptor) {
87         StringBuffer JavaDoc typeBuf = new StringBuffer JavaDoc();
88         int i = 0;
89         for(;descriptor.charAt(i) == '['; i++) {
90             typeBuf.append("[]");
91         }
92
93         // Object type
94
if (descriptor.charAt(i) == 'L') {
95             String JavaDoc type = descriptor.substring(i + 1, descriptor.length() - 1);
96             typeBuf.insert(0, type.replace('/', '.'));
97         }
98         else if ("B".equals(descriptor)) {
99             typeBuf.insert(0, "byte");
100         }
101         else if ("C".equals(descriptor)) {
102             typeBuf.insert(0, "char");
103         }
104         else if ("D".equals(descriptor)) {
105             typeBuf.insert(0, "double");
106         }
107         else if ("F".equals(descriptor)) {
108             typeBuf.insert(0, "float");
109         }
110         else if ("I".equals(descriptor)) {
111             typeBuf.insert(0, "int");
112         }
113         else if ("J".equals(descriptor)) {
114             typeBuf.insert(0, "long");
115         }
116         else if ("S".equals(descriptor)) {
117             typeBuf.insert(0, "short");
118         }
119         else if ("Z".equals(descriptor)) {
120             typeBuf.insert(0, "boolean");
121         }
122         else if ("V".equals(descriptor)) {
123             return "void";
124         }
125
126         return typeBuf.toString();
127         //throw new IllegalArgumentException("Unknown descriptor type: " + descriptor);
128
}
129
130
131     static String JavaDoc[] descriptorsToTypes(String JavaDoc pDesc) {
132         // BUG: Missing support for arrays
133
ArrayList JavaDoc al = new ArrayList JavaDoc();
134         try {
135             for (int i = 0; i < pDesc.length(); i++) {
136                 int dim = 0;
137                 StringBuffer JavaDoc typeBuf = new StringBuffer JavaDoc();
138                 for(;pDesc.charAt(i) == '['; i++) {
139                     typeBuf.append("[]");
140                 }
141                 
142                 if (pDesc.charAt(i) != 'L') {
143                     String JavaDoc type = descriptorToType("" + pDesc.charAt(i));
144                     typeBuf.insert(0, type);
145                     al.add(typeBuf.toString());
146                 }
147                 else {
148                     int start = i;
149                     for(;pDesc.charAt(i) != ';'; i++) {
150                         ;
151                     }
152                     
153                     String JavaDoc desc = pDesc.substring(start, i + 1);
154                     String JavaDoc typeName = descriptorToType(desc);
155                     typeBuf.insert(0, typeName);
156                     al.add(typeBuf.toString());
157                 }
158             }
159         }
160         catch(IllegalArgumentException JavaDoc iae) {
161             throw new IllegalArgumentException JavaDoc("Illegal descriptor: " + pDesc);
162         }
163
164         return (String JavaDoc[])al.toArray(new String JavaDoc[0]);
165     }
166
167
168     static String JavaDoc signatureToDescriptor(Signature signature) {
169         StringBuffer JavaDoc desc = new StringBuffer JavaDoc("(");
170         String JavaDoc typeNames[] = signature.getParameters();
171
172         for (int i = 0; i < typeNames.length; i++) {
173             desc.append(typeToDescriptor(typeNames[i]));
174         }
175         desc.append(")");
176         desc.append(typeToDescriptor(signature.getReturnType()));
177
178         return desc.toString();
179     }
180 }
181
Popular Tags