author | psandoz |
Wed, 31 Jan 2018 11:20:36 -0800 | |
changeset 48827 | 8772acd913e5 |
parent 48826 | c4d9d1b08e2e |
permissions | -rw-r--r-- |
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.lang.invoke.MethodHandles; |
|
27 |
import java.lang.invoke.MethodHandles.Lookup; |
|
28 |
import java.lang.reflect.Field; |
|
29 |
import java.lang.reflect.Method; |
|
30 |
import java.nio.ByteBuffer; |
|
31 |
import java.util.Arrays; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.function.Consumer; |
|
35 |
import java.util.function.ToIntFunction; |
|
36 |
||
37 |
public class IsolatedMethodBuilder extends MethodBuilder<Class<?>, String, Object[]> { |
|
38 |
||
39 |
public IsolatedMethodBuilder(Lookup lookup, String name, String type) { |
|
40 |
super(null, name, type, new IsolatedMethodPoolHelper(lookup), null); |
|
41 |
} |
|
42 |
||
43 |
static class IsolatedMethodPoolHelper implements PoolHelper<Class<?>, String, Object[]> { |
|
44 |
Map<Object, Integer> constants = new HashMap<>(); |
|
45 |
Lookup lookup; |
|
46 |
||
47 |
private IsolatedMethodPoolHelper(Lookup lookup) { |
|
48 |
this.lookup = lookup; |
|
49 |
} |
|
50 |
||
51 |
@Override |
|
52 |
public int putClass(Class<?> symbol) { |
|
53 |
return putIfAbsent(symbol); |
|
54 |
} |
|
55 |
||
56 |
@Override |
|
57 |
public int putFieldRef(Class<?> owner, CharSequence name, String type) { |
|
58 |
try { |
|
59 |
Field f = owner.getDeclaredField(name.toString()); //TODO: we should unreflect for a var handle |
|
60 |
return putIfAbsent(lookup.unreflectGetter(f)); |
|
61 |
} catch (Throwable ex) { |
|
62 |
ex.printStackTrace(); |
|
63 |
return -1; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
@Override |
|
68 |
public int putMethodRef(Class<?> owner, CharSequence name, String type, boolean isInterface) { |
|
69 |
try { |
|
70 |
Method m = owner.getDeclaredMethod(name.toString()); //we should unreflect according to method vs. constructor |
|
71 |
//and static vs. private etc. |
|
72 |
return putIfAbsent(lookup.unreflect(m)); |
|
73 |
} catch (Throwable ex) { |
|
74 |
ex.printStackTrace(); |
|
75 |
return -1; |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
@Override |
|
80 |
public int putInt(int i) { |
|
81 |
return putIfAbsent(i); |
|
82 |
} |
|
83 |
||
84 |
@Override |
|
85 |
public int putFloat(float f) { |
|
86 |
return putIfAbsent(f); |
|
87 |
} |
|
88 |
||
89 |
@Override |
|
90 |
public int putLong(long l) { |
|
91 |
return putIfAbsent(l); |
|
92 |
} |
|
93 |
||
94 |
@Override |
|
95 |
public int putDouble(double d) { |
|
96 |
return putIfAbsent(d); |
|
97 |
} |
|
98 |
||
99 |
@Override |
|
100 |
public int putString(String s) { |
|
101 |
return putIfAbsent(s); |
|
102 |
} |
|
103 |
||
104 |
@Override |
|
105 |
public int putInvokeDynamic(CharSequence invokedName, String invokedType, Class<?> bsmClass, CharSequence bsmName, String bsmType, Consumer<StaticArgListBuilder<Class<?>, String, Object[]>> staticArgs) { |
|
106 |
return 0; //??? |
|
107 |
} |
|
108 |
||
109 |
@Override |
|
110 |
public int putDynamicConstant(CharSequence constName, String constType, Class<?> bsmClass, CharSequence bsmName, String bsmType, Consumer<StaticArgListBuilder<Class<?>, String, Object[]>> staticArgs) { |
|
111 |
return 0; //??? |
|
112 |
} |
|
113 |
||
114 |
@Override |
|
115 |
public int putHandle(int refKind, Class<?> owner, CharSequence name, String type) { |
|
116 |
return 0; //??? |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
48827
8772acd913e5
8187742: Minimal set of bootstrap methods for constant dynamic
psandoz
parents:
48826
diff
changeset
|
120 |
public int putHandle(int refKind, Class<?> owner, CharSequence name, String type, boolean isInterface) { |
8772acd913e5
8187742: Minimal set of bootstrap methods for constant dynamic
psandoz
parents:
48826
diff
changeset
|
121 |
return 0; //??? |
8772acd913e5
8187742: Minimal set of bootstrap methods for constant dynamic
psandoz
parents:
48826
diff
changeset
|
122 |
} |
8772acd913e5
8187742: Minimal set of bootstrap methods for constant dynamic
psandoz
parents:
48826
diff
changeset
|
123 |
|
8772acd913e5
8187742: Minimal set of bootstrap methods for constant dynamic
psandoz
parents:
48826
diff
changeset
|
124 |
@Override |
48826 | 125 |
public int putMethodType(String s) { |
126 |
return 0; //??? |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
public int putUtf8(CharSequence s) { |
|
131 |
return putIfAbsent(s); |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public int putType(String s) { |
|
136 |
return putIfAbsent(s); |
|
137 |
} |
|
138 |
||
139 |
@Override |
|
140 |
public int size() { |
|
141 |
return constants.size(); |
|
142 |
} |
|
143 |
||
144 |
@Override |
|
145 |
public Object[] entries() { |
|
146 |
return constants.keySet().toArray(); |
|
147 |
} |
|
148 |
||
149 |
int putIfAbsent(Object o) { |
|
150 |
int nextIndex = constants.size() + 1; |
|
151 |
Object res = constants.putIfAbsent(o, nextIndex); |
|
152 |
return res == null ? |
|
153 |
nextIndex : (Integer)res; |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
public Object[] entries() { |
|
158 |
return poolHelper.entries(); |
|
159 |
} |
|
160 |
||
161 |
@Override |
|
162 |
public byte[] build() { |
|
163 |
byte[] arr = super.build(); |
|
164 |
int codelength_offset = 2 + 2 + 2 + 2 + |
|
165 |
2 + 4 + 2 + 2; |
|
166 |
int code_offset = codelength_offset + 4; |
|
167 |
int length = ByteBuffer.wrap(arr).getInt(codelength_offset); |
|
168 |
byte[] opcodes = new byte[length]; |
|
169 |
System.arraycopy(arr, code_offset, opcodes, 0, length); |
|
170 |
return opcodes; |
|
171 |
} |
|
172 |
||
173 |
public static void main(String[] args) { |
|
174 |
IsolatedMethodBuilder imb = new IsolatedMethodBuilder(MethodHandles.lookup(), "foo", "(java/lang/String;)I"); |
|
175 |
imb.withCode(C -> |
|
176 |
C.aload_0() |
|
177 |
.invokevirtual(String.class, "length", "()I", false) |
|
178 |
.ireturn()); |
|
179 |
byte[] opcodes = imb.build(); |
|
180 |
System.out.println(Arrays.toString(opcodes)); |
|
181 |
} |
|
182 |
} |