48826
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package jdk.experimental.bytecode;
|
|
25 |
|
|
26 |
import java.util.Iterator;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Helper to create and manipulate type descriptors, where type descriptors
|
|
30 |
* are represented as JVM type descriptor strings and symbols are represented
|
|
31 |
* as name strings
|
|
32 |
*/
|
|
33 |
public class BasicTypeHelper implements TypeHelper<String, String> {
|
|
34 |
|
|
35 |
@Override
|
|
36 |
public String elemtype(String s) {
|
|
37 |
if (!s.startsWith("[")) {
|
|
38 |
throw new IllegalStateException();
|
|
39 |
}
|
|
40 |
return s.substring(1);
|
|
41 |
}
|
|
42 |
|
|
43 |
@Override
|
|
44 |
public String arrayOf(String s) {
|
|
45 |
return "[" + s;
|
|
46 |
}
|
|
47 |
|
|
48 |
@Override
|
|
49 |
public String type(String s) {
|
|
50 |
return "L" + s + ";";
|
|
51 |
}
|
|
52 |
|
|
53 |
@Override
|
|
54 |
public TypeTag tag(String s) {
|
|
55 |
switch (s.charAt(0)) {
|
|
56 |
case '[':
|
|
57 |
case 'L':
|
|
58 |
return TypeTag.A;
|
|
59 |
case 'B':
|
|
60 |
case 'C':
|
|
61 |
case 'Z':
|
|
62 |
case 'S':
|
|
63 |
case 'I':
|
|
64 |
return TypeTag.I;
|
|
65 |
case 'F':
|
|
66 |
return TypeTag.F;
|
|
67 |
case 'J':
|
|
68 |
return TypeTag.J;
|
|
69 |
case 'D':
|
|
70 |
return TypeTag.D;
|
|
71 |
case 'V':
|
|
72 |
return TypeTag.V;
|
|
73 |
case 'Q':
|
|
74 |
return TypeTag.Q;
|
|
75 |
default:
|
|
76 |
throw new IllegalStateException("Bad type: " + s);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
@Override
|
|
81 |
public String nullType() {
|
|
82 |
// Needed in TypedCodeBuilder; ACONST_NULL pushes a 'null' onto the stack,
|
|
83 |
// and stack maps handle null differently
|
|
84 |
return "<null>";
|
|
85 |
}
|
|
86 |
|
|
87 |
@Override
|
|
88 |
public String commonSupertype(String t1, String t2) {
|
|
89 |
if (t1.equals(t2)) {
|
|
90 |
return t1;
|
|
91 |
} else {
|
|
92 |
try {
|
|
93 |
Class<?> c1 = from(t1);
|
|
94 |
Class<?> c2 = from(t2);
|
|
95 |
if (c1.isAssignableFrom(c2)) {
|
|
96 |
return t1;
|
|
97 |
} else if (c2.isAssignableFrom(c1)) {
|
|
98 |
return t2;
|
|
99 |
} else {
|
|
100 |
return "Ljava/lang/Object;";
|
|
101 |
}
|
|
102 |
} catch (Exception e) {
|
|
103 |
return null;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
public Class<?> from(String desc) throws ReflectiveOperationException {
|
|
109 |
if (desc.startsWith("[")) {
|
|
110 |
return Class.forName(desc.replaceAll("/", "."));
|
|
111 |
} else {
|
|
112 |
return Class.forName(symbol(desc).replaceAll("/", "."));
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
@Override
|
|
117 |
public Iterator<String> parameterTypes(String s) {
|
|
118 |
//TODO: gracefully non-method types
|
|
119 |
return new Iterator<String>() {
|
|
120 |
int ch = 1;
|
|
121 |
|
|
122 |
@Override
|
|
123 |
public boolean hasNext() {
|
|
124 |
return s.charAt(ch) != ')';
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
public String next() {
|
|
129 |
char curr = s.charAt(ch);
|
|
130 |
switch (curr) {
|
|
131 |
case 'C':
|
|
132 |
case 'B':
|
|
133 |
case 'S':
|
|
134 |
case 'I':
|
|
135 |
case 'J':
|
|
136 |
case 'F':
|
|
137 |
case 'D':
|
|
138 |
case 'Z':
|
|
139 |
ch++;
|
|
140 |
return String.valueOf(curr);
|
|
141 |
case '[':
|
|
142 |
ch++;
|
|
143 |
return "[" + next();
|
|
144 |
case 'L':
|
|
145 |
case 'Q':
|
|
146 |
StringBuilder builder = new StringBuilder();
|
|
147 |
while (curr != ';') {
|
|
148 |
builder.append(curr);
|
|
149 |
curr = s.charAt(++ch);
|
|
150 |
}
|
|
151 |
builder.append(';');
|
|
152 |
ch++;
|
|
153 |
return builder.toString();
|
|
154 |
default:
|
|
155 |
throw new AssertionError("cannot parse string: " + s);
|
|
156 |
}
|
|
157 |
}
|
|
158 |
};
|
|
159 |
}
|
|
160 |
|
|
161 |
@Override
|
|
162 |
public String symbolFrom(String s) {
|
|
163 |
return s;
|
|
164 |
}
|
|
165 |
|
|
166 |
@Override
|
|
167 |
public String fromTag(TypeTag tag) {
|
|
168 |
return tag.name();
|
|
169 |
}
|
|
170 |
|
|
171 |
@Override
|
|
172 |
public String symbol(String type) {
|
|
173 |
return (type.startsWith("L") || type.startsWith("Q")) ? type.substring(1, type.length() - 1) : type;
|
|
174 |
}
|
|
175 |
|
|
176 |
@Override
|
|
177 |
public String returnType(String s) {
|
|
178 |
return s.substring(s.indexOf(')') + 1, s.length());
|
|
179 |
}
|
|
180 |
|
|
181 |
}
|