From 44e90a1f761425a6731f549bc5eada0955206173 Mon Sep 17 00:00:00 2001 From: Evan Saulpaugh Date: Mon, 9 Sep 2024 04:44:20 -0500 Subject: [PATCH] remove TupleType#getNonCapturing, TypeFactory.createNonCapturing --- .../com/esaulpaugh/headlong/abi/TupleType.java | 17 ++++++++++------- .../esaulpaugh/headlong/abi/TypeFactory.java | 12 +++++++----- .../esaulpaugh/headlong/abi/ABIJSONTest.java | 6 +++--- .../esaulpaugh/headlong/abi/DecodeTest.java | 4 ++-- .../esaulpaugh/headlong/abi/EncodeTest.java | 18 +++++++++--------- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/esaulpaugh/headlong/abi/TupleType.java b/src/main/java/com/esaulpaugh/headlong/abi/TupleType.java index 5b59bbbd..206b2f2f 100644 --- a/src/main/java/com/esaulpaugh/headlong/abi/TupleType.java +++ b/src/main/java/com/esaulpaugh/headlong/abi/TupleType.java @@ -75,15 +75,18 @@ public String getElementInternalType(int index) { return elementInternalTypes == null ? null : elementInternalTypes[index]; } + /** + * If the compiler can't infer the return type, use a type witness. + * + * @param index + * @return + * @param + */ @SuppressWarnings("unchecked") public > T get(int index) { return (T) elementTypes[index]; } - public ABIType getNonCapturing(int index) { - return get(index); - } - @Override Class arrayClass() { return Tuple[].class; @@ -121,7 +124,7 @@ private static int measureObject(ABIType type, Object value) { @Override public int byteLengthPacked(Tuple value) { final Object[] elements = value != null ? value.elements : new Object[size()]; - return countBytes(i -> getNonCapturing(i).byteLengthPacked(elements[i])); + return countBytes(i -> this.>get(i).byteLengthPacked(elements[i])); } private int countBytes(IntUnaryOperator counter) { @@ -174,7 +177,7 @@ void encodeTail(Tuple value, ByteBuffer dest) { encodeDynamic(value.elements, dest); } else { for (int i = 0; i < value.elements.length; i++) { - getNonCapturing(i).encodeTail(value.elements[i], dest); + this.>get(i).encodeTail(value.elements[i], dest); } } } @@ -182,7 +185,7 @@ void encodeTail(Tuple value, ByteBuffer dest) { @Override void encodePackedUnchecked(Tuple value, ByteBuffer dest) { for (int i = 0; i < value.elements.length; i++) { - getNonCapturing(i).encodePackedUnchecked(value.elements[i], dest); + this.>get(i).encodePackedUnchecked(value.elements[i], dest); } } diff --git a/src/main/java/com/esaulpaugh/headlong/abi/TypeFactory.java b/src/main/java/com/esaulpaugh/headlong/abi/TypeFactory.java index ad40c303..b7c2738c 100644 --- a/src/main/java/com/esaulpaugh/headlong/abi/TypeFactory.java +++ b/src/main/java/com/esaulpaugh/headlong/abi/TypeFactory.java @@ -105,6 +105,13 @@ private static void mapByteArray(Map> map, String type, int a map.put(type, new ArrayType(type, byte[].class, ByteType.INSTANCE, arrayLen, byte[][].class, ABIType.FLAGS_NONE)); } + /** + * If the compiler can't infer the return type, use a type witness. + * + * @param rawType + * @return + * @param + */ public static > T create(String rawType) { return create(ABIType.FLAGS_NONE, rawType); } @@ -114,11 +121,6 @@ public static > T create(int flags, String rawType) { return (T) build(rawType, null, null, flags); } - @SuppressWarnings("unchecked") - public static ABIType createNonCapturing(String rawType) { - return (ABIType) build(rawType, null, null, ABIType.FLAGS_NONE); - } - /** If you don't need any {@code elementNames}, use {@link TypeFactory#create(String)}. */ @SuppressWarnings("unchecked") public static TupleType createTupleTypeWithNames(String rawType, String... elementNames) { diff --git a/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java b/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java index d193aeba..b2f343d1 100644 --- a/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java +++ b/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java @@ -289,10 +289,10 @@ public void testParseFunctionA() throws Throwable { final BigInteger val = BigInteger.valueOf(40L); final Object obj = val; - final Object a = out.getNonCapturing(0).encode(val); - final Object b = out.getNonCapturing(0).encode(obj); + final Object a = out.>get(0).encode(val); + final Object b = out.>get(0).encode(obj); assertEquals(a, b); - final ABIType type = out.getNonCapturing(0); + final ABIType type = out.get(0); assertEquals(a, type.encode(obj)); assertEquals(a, type.encode(val)); diff --git a/src/test/java/com/esaulpaugh/headlong/abi/DecodeTest.java b/src/test/java/com/esaulpaugh/headlong/abi/DecodeTest.java index 8228a053..06333888 100644 --- a/src/test/java/com/esaulpaugh/headlong/abi/DecodeTest.java +++ b/src/test/java/com/esaulpaugh/headlong/abi/DecodeTest.java @@ -730,7 +730,7 @@ public void testDecodeIndexedDynamicType() throws Throwable { byte[][] topics = { FastHex.decode("d78fe195906f002940f4b32985f1daa40764f8481c05447b6751db32e70d744b"), FastHex.decode("392791df626408017a264f53fde61065d5a93a32b60171df9d8a46afdf82992d"), - TypeFactory.createNonCapturing("int8").encode(12).array() + TypeFactory.create("int8").encode(12).array() }; Tuple result = event.decodeArgs(topics, Strings.EMPTY_BYTE_ARRAY); assertEquals("392791df626408017a264f53fde61065d5a93a32b60171df9d8a46afdf82992d", Strings.encode((byte[]) result.get(0))); @@ -795,7 +795,7 @@ public void testBadTopics() throws Throwable { byte[][] badTopics1 = { badTopics0[0], badTopics0[1], - TypeFactory.createNonCapturing("int8").encode(12).array(), + TypeFactory.create("int8").encode(12).array(), Strings.EMPTY_BYTE_ARRAY }; assertThrown(IllegalArgumentException.class, "expected topics.length 3 but found length 4", () -> event.decodeArgs(badTopics1, null)); diff --git a/src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java b/src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java index 51620ba1..ab2c6501 100644 --- a/src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java +++ b/src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java @@ -661,7 +661,7 @@ public void testFixedUFixedMinMax() throws Throwable { public void testCasts() throws Throwable { final Object[] args = new Object[] { (byte) -1, (short) 10, BigInteger.valueOf(10L), new BigDecimal(BigInteger.valueOf(57L), 1), 0f, -2.1d, new AtomicInteger(), new AtomicLong(98L) }; { - final ABIType int8 = TypeFactory.createNonCapturing("int8"); + final ABIType int8 = TypeFactory.create("int8"); assertEquals(Integer.class, int8.clazz); for (Object arg : args) { testCast(arg.getClass(), int8, arg); @@ -669,7 +669,7 @@ public void testCasts() throws Throwable { testCast(Long.class, int8, 3L); } { - final ABIType uint24 = TypeFactory.createNonCapturing("uint24"); + final ABIType uint24 = TypeFactory.create("uint24"); assertEquals(Integer.class, uint24.clazz); for (Object arg : args) { testCast(arg.getClass(), uint24, arg); @@ -677,7 +677,7 @@ public void testCasts() throws Throwable { testCast(Long.class, uint24, 3L); } { - final ABIType int64 = TypeFactory.createNonCapturing("int64"); + final ABIType int64 = TypeFactory.create("int64"); assertEquals(Long.class, int64.clazz); for (Object arg : args) { testCast(arg.getClass(), int64, arg); @@ -685,7 +685,7 @@ public void testCasts() throws Throwable { testCast(Integer.class, int64, 5); } { - final ABIType uint56 = TypeFactory.createNonCapturing("uint56"); + final ABIType uint56 = TypeFactory.create("uint56"); assertEquals(Long.class, uint56.clazz); for (Object arg : args) { testCast(arg.getClass(), uint56, arg); @@ -704,11 +704,11 @@ private static void testCast(Class from, ABIType type, Object arg) th @Test public void testCasts2() throws Throwable { assertThrown(ClassCastException.class, () -> ((ABIType) (ABIType) ByteType.INSTANCE).encode(7)); - testCast(Integer.class, TypeFactory.createNonCapturing("uint64"), 10); - testCast(Byte.class, TypeFactory.createNonCapturing("bool"), (byte) 11); - testCast(BigInteger.class, TypeFactory.createNonCapturing("address"), BigInteger.valueOf(12L)); - testCast(Double.class, TypeFactory.createNonCapturing("int96"), 13.2d); - testCast(Long.class, TypeFactory.createNonCapturing("fixed"), 14L); + testCast(Integer.class, TypeFactory.create("uint64"), 10); + testCast(Byte.class, TypeFactory.create("bool"), (byte) 11); + testCast(BigInteger.class, TypeFactory.create("address"), BigInteger.valueOf(12L)); + testCast(Double.class, TypeFactory.create("int96"), 13.2d); + testCast(Long.class, TypeFactory.create("fixed"), 14L); } @Test