KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > runtime > Desc


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.runtime;
17
18 /**
19  * A support class for implementing <code>$sig</code> and
20  * <code>$type</code>.
21  * This support class is required at runtime
22  * only if <code>$sig</code> or <code>$type</code> is used.
23  */

24 public class Desc {
25
26     /**
27      * Specifies how a <code>java.lang.Class</code> object is loaded.
28      *
29      * <p>If true, it is loaded by:
30      * <ul><pre>Thread.currentThread().getContextClassLoader().loadClass()</pre></ul>
31      * <p>If false, it is loaded by <code>Class.forName()</code>.
32      * The default value is false.
33      */

34     public static boolean useContextClassLoader = false;
35
36     private static Class JavaDoc getClassObject(String JavaDoc name)
37         throws ClassNotFoundException JavaDoc
38     {
39         if (useContextClassLoader)
40             return Thread.currentThread().getContextClassLoader()
41                    .loadClass(name);
42         else
43             return Class.forName(name);
44     }
45
46     /**
47      * Interprets the given class name.
48      * It is used for implementing <code>$class</code>.
49      */

50     public static Class JavaDoc getClazz(String JavaDoc name) {
51         try {
52             return getClassObject(name);
53         }
54         catch (ClassNotFoundException JavaDoc e) {
55             throw new RuntimeException JavaDoc("$class: internal error");
56         }
57     }
58
59     /**
60      * Interprets the given type descriptor representing a method
61      * signature. It is used for implementing <code>$sig</code>.
62      */

63     public static Class JavaDoc[] getParams(String JavaDoc desc) {
64         if (desc.charAt(0) != '(')
65             throw new RuntimeException JavaDoc("$sig: internal error");
66
67         return getType(desc, desc.length(), 1, 0);
68     }
69
70     /**
71      * Interprets the given type descriptor.
72      * It is used for implementing <code>$type</code>.
73      */

74     public static Class JavaDoc getType(String JavaDoc desc) {
75         Class JavaDoc[] result = getType(desc, desc.length(), 0, 0);
76         if (result == null || result.length != 1)
77             throw new RuntimeException JavaDoc("$type: internal error");
78
79         return result[0];
80     }
81
82     private static Class JavaDoc[] getType(String JavaDoc desc, int descLen,
83                                    int start, int num) {
84         Class JavaDoc clazz;
85         if (start >= descLen)
86             return new Class JavaDoc[num];
87
88         char c = desc.charAt(start);
89         switch (c) {
90         case 'Z' :
91             clazz = Boolean.TYPE;
92             break;
93         case 'C' :
94             clazz = Character.TYPE;
95             break;
96         case 'B' :
97             clazz = Byte.TYPE;
98             break;
99         case 'S' :
100             clazz = Short.TYPE;
101             break;
102         case 'I' :
103             clazz = Integer.TYPE;
104             break;
105         case 'J' :
106             clazz = Long.TYPE;
107             break;
108         case 'F' :
109             clazz = Float.TYPE;
110             break;
111         case 'D' :
112             clazz = Double.TYPE;
113             break;
114         case 'V' :
115             clazz = Void.TYPE;
116             break;
117         case 'L' :
118         case '[' :
119             return getClassType(desc, descLen, start, num);
120         default :
121             return new Class JavaDoc[num];
122         }
123
124         Class JavaDoc[] result = getType(desc, descLen, start + 1, num + 1);
125         result[num] = clazz;
126         return result;
127     }
128
129     private static Class JavaDoc[] getClassType(String JavaDoc desc, int descLen,
130                                         int start, int num) {
131         int end = start;
132         while (desc.charAt(end) == '[')
133             ++end;
134
135         if (desc.charAt(end) == 'L') {
136             end = desc.indexOf(';', end);
137             if (end < 0)
138                 throw new IndexOutOfBoundsException JavaDoc("bad descriptor");
139         }
140
141         String JavaDoc cname;
142         if (desc.charAt(start) == 'L')
143             cname = desc.substring(start + 1, end);
144         else
145             cname = desc.substring(start, end + 1);
146
147         Class JavaDoc[] result = getType(desc, descLen, end + 1, num + 1);
148         try {
149             result[num] = getClassObject(cname.replace('/', '.'));
150         }
151         catch (ClassNotFoundException JavaDoc e) {
152             // "new RuntimeException(e)" is not available in JDK 1.3.
153
throw new RuntimeException JavaDoc(e.getMessage());
154         }
155
156         return result;
157     }
158 }
159
Popular Tags