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.MethodHandleInfo;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Objects;
30 import java.util.function.Consumer;
31 import java.util.function.Function;
32 import java.util.function.ToIntBiFunction;
33
34 /**
35 * A helper for building and tracking constant pools whose entries are
36 * represented as byte arrays.
37 *
38 * @param <S> the type of the symbol representation
39 * @param <T> the type of type descriptors representation
40 */
41 public class BytePoolHelper<S, T> implements PoolHelper<S, T, byte[]> {
42
43 GrowableByteBuffer pool = new GrowableByteBuffer();
44 GrowableByteBuffer bsm_attr = new GrowableByteBuffer();
45 //Map<PoolKey, PoolKey> indicesMap = new HashMap<>();
46 int currentIndex = 1;
319
320 @Override
321 public boolean equals(Object obj) {
322 if (obj instanceof BsmKey) {
323 BsmKey that = (BsmKey)obj;
324 return Objects.equals(bsmClass, that.bsmClass) &&
325 Objects.equals(bsmName, that.bsmName) &&
326 Objects.equals(bsmType, that.bsmType) &&
327 Objects.deepEquals(bsmArgs, that.bsmArgs);
328 } else {
329 return false;
330 }
331 }
332 }
333
334 @Override
335 public int putClass(S symbol) {
336 return putClassInternal(symbolToString.apply(symbol));
337 }
338
339 private int putClassInternal(String symbol) {
340 key.setClass(symbol);
341 PoolKey poolKey = entries.lookup(key);
342 if (poolKey == null) {
343 poolKey = key.dup();
344 int utf8_idx = putUtf8(symbol);
345 poolKey.at(currentIndex++);
346 entries.enter(poolKey);
347 pool.writeByte(PoolTag.CONSTANT_CLASS.tag);
348 pool.writeChar(utf8_idx);
349 }
350 return poolKey.index;
351 }
352
353 @Override
354 public int putFieldRef(S owner, CharSequence name, T type) {
355 return putMemberRef(PoolTag.CONSTANT_FIELDREF, owner, name, type);
356 }
357
358 @Override
|
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.MethodHandleInfo;
27 import java.lang.invoke.MethodType;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.function.Consumer;
32 import java.util.function.Function;
33 import java.util.function.ToIntBiFunction;
34
35 /**
36 * A helper for building and tracking constant pools whose entries are
37 * represented as byte arrays.
38 *
39 * @param <S> the type of the symbol representation
40 * @param <T> the type of type descriptors representation
41 */
42 public class BytePoolHelper<S, T> implements PoolHelper<S, T, byte[]> {
43
44 GrowableByteBuffer pool = new GrowableByteBuffer();
45 GrowableByteBuffer bsm_attr = new GrowableByteBuffer();
46 //Map<PoolKey, PoolKey> indicesMap = new HashMap<>();
47 int currentIndex = 1;
320
321 @Override
322 public boolean equals(Object obj) {
323 if (obj instanceof BsmKey) {
324 BsmKey that = (BsmKey)obj;
325 return Objects.equals(bsmClass, that.bsmClass) &&
326 Objects.equals(bsmName, that.bsmName) &&
327 Objects.equals(bsmType, that.bsmType) &&
328 Objects.deepEquals(bsmArgs, that.bsmArgs);
329 } else {
330 return false;
331 }
332 }
333 }
334
335 @Override
336 public int putClass(S symbol) {
337 return putClassInternal(symbolToString.apply(symbol));
338 }
339
340 @Override
341 public int putInlineClass(S symbol) {
342 return putClassInternal("Q" + symbolToString.apply(symbol) + ";");
343 }
344
345 private int putClassInternal(String symbol) {
346 key.setClass(symbol);
347 PoolKey poolKey = entries.lookup(key);
348 if (poolKey == null) {
349 poolKey = key.dup();
350 int utf8_idx = putUtf8(symbol);
351 poolKey.at(currentIndex++);
352 entries.enter(poolKey);
353 pool.writeByte(PoolTag.CONSTANT_CLASS.tag);
354 pool.writeChar(utf8_idx);
355 }
356 return poolKey.index;
357 }
358
359 @Override
360 public int putFieldRef(S owner, CharSequence name, T type) {
361 return putMemberRef(PoolTag.CONSTANT_FIELDREF, owner, name, type);
362 }
363
364 @Override
|