jaroslav@1646
|
1 |
/*
|
jaroslav@1646
|
2 |
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@1646
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1646
|
4 |
*
|
jaroslav@1646
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1646
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1646
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1646
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1646
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1646
|
10 |
*
|
jaroslav@1646
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1646
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1646
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1646
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1646
|
15 |
* accompanied this code).
|
jaroslav@1646
|
16 |
*
|
jaroslav@1646
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1646
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1646
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1646
|
20 |
*
|
jaroslav@1646
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1646
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1646
|
23 |
* questions.
|
jaroslav@1646
|
24 |
*/
|
jaroslav@1646
|
25 |
|
jaroslav@1646
|
26 |
package sun.invoke.util;
|
jaroslav@1646
|
27 |
|
jaroslav@1646
|
28 |
import java.lang.invoke.MethodType;
|
jaroslav@1646
|
29 |
import java.util.ArrayList;
|
jaroslav@1646
|
30 |
import java.util.List;
|
jaroslav@1646
|
31 |
|
jaroslav@1646
|
32 |
/**
|
jaroslav@1646
|
33 |
* Utility routines for dealing with bytecode-level signatures.
|
jaroslav@1646
|
34 |
* @author jrose
|
jaroslav@1646
|
35 |
*/
|
jaroslav@1646
|
36 |
public class BytecodeDescriptor {
|
jaroslav@1646
|
37 |
|
jaroslav@1646
|
38 |
private BytecodeDescriptor() { } // cannot instantiate
|
jaroslav@1646
|
39 |
|
jaroslav@1646
|
40 |
public static List<Class<?>> parseMethod(String bytecodeSignature, ClassLoader loader) {
|
jaroslav@1646
|
41 |
return parseMethod(bytecodeSignature, 0, bytecodeSignature.length(), loader);
|
jaroslav@1646
|
42 |
}
|
jaroslav@1646
|
43 |
|
jaroslav@1646
|
44 |
static List<Class<?>> parseMethod(String bytecodeSignature,
|
jaroslav@1646
|
45 |
int start, int end, ClassLoader loader) {
|
jaroslav@1646
|
46 |
if (loader == null)
|
jaroslav@1646
|
47 |
loader = ClassLoader.getSystemClassLoader();
|
jaroslav@1646
|
48 |
String str = bytecodeSignature;
|
jaroslav@1646
|
49 |
int[] i = {start};
|
jaroslav@1646
|
50 |
ArrayList<Class<?>> ptypes = new ArrayList<Class<?>>();
|
jaroslav@1646
|
51 |
if (i[0] < end && str.charAt(i[0]) == '(') {
|
jaroslav@1646
|
52 |
++i[0]; // skip '('
|
jaroslav@1646
|
53 |
while (i[0] < end && str.charAt(i[0]) != ')') {
|
jaroslav@1646
|
54 |
Class<?> pt = parseSig(str, i, end, loader);
|
jaroslav@1646
|
55 |
if (pt == null || pt == void.class)
|
jaroslav@1646
|
56 |
parseError(str, "bad argument type");
|
jaroslav@1646
|
57 |
ptypes.add(pt);
|
jaroslav@1646
|
58 |
}
|
jaroslav@1646
|
59 |
++i[0]; // skip ')'
|
jaroslav@1646
|
60 |
} else {
|
jaroslav@1646
|
61 |
parseError(str, "not a method type");
|
jaroslav@1646
|
62 |
}
|
jaroslav@1646
|
63 |
Class<?> rtype = parseSig(str, i, end, loader);
|
jaroslav@1646
|
64 |
if (rtype == null || i[0] != end)
|
jaroslav@1646
|
65 |
parseError(str, "bad return type");
|
jaroslav@1646
|
66 |
ptypes.add(rtype);
|
jaroslav@1646
|
67 |
return ptypes;
|
jaroslav@1646
|
68 |
}
|
jaroslav@1646
|
69 |
|
jaroslav@1646
|
70 |
static private void parseError(String str, String msg) {
|
jaroslav@1646
|
71 |
throw new IllegalArgumentException("bad signature: "+str+": "+msg);
|
jaroslav@1646
|
72 |
}
|
jaroslav@1646
|
73 |
|
jaroslav@1646
|
74 |
static private Class<?> parseSig(String str, int[] i, int end, ClassLoader loader) {
|
jaroslav@1646
|
75 |
if (i[0] == end) return null;
|
jaroslav@1646
|
76 |
char c = str.charAt(i[0]++);
|
jaroslav@1646
|
77 |
if (c == 'L') {
|
jaroslav@1646
|
78 |
int begc = i[0], endc = str.indexOf(';', begc);
|
jaroslav@1646
|
79 |
if (endc < 0) return null;
|
jaroslav@1646
|
80 |
i[0] = endc+1;
|
jaroslav@1646
|
81 |
String name = str.substring(begc, endc).replace('/', '.');
|
jaroslav@1646
|
82 |
try {
|
jaroslav@1646
|
83 |
return loader.loadClass(name);
|
jaroslav@1646
|
84 |
} catch (ClassNotFoundException ex) {
|
jaroslav@1646
|
85 |
throw new TypeNotPresentException(name, ex);
|
jaroslav@1646
|
86 |
}
|
jaroslav@1646
|
87 |
} else if (c == '[') {
|
jaroslav@1646
|
88 |
Class<?> t = parseSig(str, i, end, loader);
|
jaroslav@1646
|
89 |
if (t != null)
|
jaroslav@1646
|
90 |
t = java.lang.reflect.Array.newInstance(t, 0).getClass();
|
jaroslav@1646
|
91 |
return t;
|
jaroslav@1646
|
92 |
} else {
|
jaroslav@1646
|
93 |
return Wrapper.forBasicType(c).primitiveType();
|
jaroslav@1646
|
94 |
}
|
jaroslav@1646
|
95 |
}
|
jaroslav@1646
|
96 |
|
jaroslav@1646
|
97 |
public static String unparse(Class<?> type) {
|
jaroslav@1646
|
98 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1646
|
99 |
unparseSig(type, sb);
|
jaroslav@1646
|
100 |
return sb.toString();
|
jaroslav@1646
|
101 |
}
|
jaroslav@1646
|
102 |
|
jaroslav@1646
|
103 |
public static String unparse(MethodType type) {
|
jaroslav@1646
|
104 |
return unparseMethod(type.returnType(), type.parameterList());
|
jaroslav@1646
|
105 |
}
|
jaroslav@1646
|
106 |
|
jaroslav@1646
|
107 |
public static String unparse(Object type) {
|
jaroslav@1646
|
108 |
if (type instanceof Class<?>)
|
jaroslav@1646
|
109 |
return unparse((Class<?>) type);
|
jaroslav@1646
|
110 |
if (type instanceof MethodType)
|
jaroslav@1646
|
111 |
return unparse((MethodType) type);
|
jaroslav@1646
|
112 |
return (String) type;
|
jaroslav@1646
|
113 |
}
|
jaroslav@1646
|
114 |
|
jaroslav@1646
|
115 |
public static String unparseMethod(Class<?> rtype, List<Class<?>> ptypes) {
|
jaroslav@1646
|
116 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1646
|
117 |
sb.append('(');
|
jaroslav@1646
|
118 |
for (Class<?> pt : ptypes)
|
jaroslav@1646
|
119 |
unparseSig(pt, sb);
|
jaroslav@1646
|
120 |
sb.append(')');
|
jaroslav@1646
|
121 |
unparseSig(rtype, sb);
|
jaroslav@1646
|
122 |
return sb.toString();
|
jaroslav@1646
|
123 |
}
|
jaroslav@1646
|
124 |
|
jaroslav@1646
|
125 |
static private void unparseSig(Class<?> t, StringBuilder sb) {
|
jaroslav@1646
|
126 |
char c = Wrapper.forBasicType(t).basicTypeChar();
|
jaroslav@1646
|
127 |
if (c != 'L') {
|
jaroslav@1646
|
128 |
sb.append(c);
|
jaroslav@1646
|
129 |
} else {
|
jaroslav@1646
|
130 |
boolean lsemi = (!t.isArray());
|
jaroslav@1646
|
131 |
if (lsemi) sb.append('L');
|
jaroslav@1646
|
132 |
sb.append(t.getName().replace('.', '/'));
|
jaroslav@1646
|
133 |
if (lsemi) sb.append(';');
|
jaroslav@1646
|
134 |
}
|
jaroslav@1646
|
135 |
}
|
jaroslav@1646
|
136 |
|
jaroslav@1646
|
137 |
}
|