Skip to content

Commit

Permalink
remove TupleType#getNonCapturing, TypeFactory.createNonCapturing
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Sep 9, 2024
1 parent f399509 commit 44e90a1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
17 changes: 10 additions & 7 deletions src/main/java/com/esaulpaugh/headlong/abi/TupleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T>
*/
@SuppressWarnings("unchecked")
public <T extends ABIType<?>> T get(int index) {
return (T) elementTypes[index];
}

public ABIType<Object> getNonCapturing(int index) {
return get(index);
}

@Override
Class<?> arrayClass() {
return Tuple[].class;
Expand Down Expand Up @@ -121,7 +124,7 @@ private static int measureObject(ABIType<Object> 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.<ABIType<Object>>get(i).byteLengthPacked(elements[i]));
}

private int countBytes(IntUnaryOperator counter) {
Expand Down Expand Up @@ -174,15 +177,15 @@ 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.<ABIType<Object>>get(i).encodeTail(value.elements[i], 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.<ABIType<Object>>get(i).encodePackedUnchecked(value.elements[i], dest);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/esaulpaugh/headlong/abi/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ private static void mapByteArray(Map<String, ABIType<?>> map, String type, int a
map.put(type, new ArrayType<ByteType, Byte, byte[]>(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 <T>
*/
public static <T extends ABIType<?>> T create(String rawType) {
return create(ABIType.FLAGS_NONE, rawType);
}
Expand All @@ -114,11 +121,6 @@ public static <T extends ABIType<?>> T create(int flags, String rawType) {
return (T) build(rawType, null, null, flags);
}

@SuppressWarnings("unchecked")
public static ABIType<Object> createNonCapturing(String rawType) {
return (ABIType<Object>) build(rawType, null, null, ABIType.FLAGS_NONE);
}

/** If you don't need any {@code elementNames}, use {@link TypeFactory#create(String)}. */
@SuppressWarnings("unchecked")
public static <X extends Tuple> TupleType<X> createTupleTypeWithNames(String rawType, String... elementNames) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.<ABIType<Object>>get(0).encode(val);
final Object b = out.<ABIType<? super Object>>get(0).encode(obj);
assertEquals(a, b);
final ABIType<? super Object> type = out.getNonCapturing(0);
final ABIType<? super Object> type = out.get(0);
assertEquals(a, type.encode(obj));
assertEquals(a, type.encode(val));

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/esaulpaugh/headlong/abi/DecodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ public void testDecodeIndexedDynamicType() throws Throwable {
byte[][] topics = {
FastHex.decode("d78fe195906f002940f4b32985f1daa40764f8481c05447b6751db32e70d744b"),
FastHex.decode("392791df626408017a264f53fde61065d5a93a32b60171df9d8a46afdf82992d"),
TypeFactory.createNonCapturing("int8").encode(12).array()
TypeFactory.<IntType>create("int8").encode(12).array()
};
Tuple result = event.decodeArgs(topics, Strings.EMPTY_BYTE_ARRAY);
assertEquals("392791df626408017a264f53fde61065d5a93a32b60171df9d8a46afdf82992d", Strings.encode((byte[]) result.get(0)));
Expand Down Expand Up @@ -795,7 +795,7 @@ public void testBadTopics() throws Throwable {
byte[][] badTopics1 = {
badTopics0[0],
badTopics0[1],
TypeFactory.createNonCapturing("int8").encode(12).array(),
TypeFactory.<IntType>create("int8").encode(12).array(),
Strings.EMPTY_BYTE_ARRAY
};
assertThrown(IllegalArgumentException.class, "expected topics.length 3 but found length 4", () -> event.decodeArgs(badTopics1, null));
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,31 +661,31 @@ 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<Object> int8 = TypeFactory.createNonCapturing("int8");
final ABIType<Object> int8 = TypeFactory.create("int8");
assertEquals(Integer.class, int8.clazz);
for (Object arg : args) {
testCast(arg.getClass(), int8, arg);
}
testCast(Long.class, int8, 3L);
}
{
final ABIType<Object> uint24 = TypeFactory.createNonCapturing("uint24");
final ABIType<Object> uint24 = TypeFactory.create("uint24");
assertEquals(Integer.class, uint24.clazz);
for (Object arg : args) {
testCast(arg.getClass(), uint24, arg);
}
testCast(Long.class, uint24, 3L);
}
{
final ABIType<Object> int64 = TypeFactory.createNonCapturing("int64");
final ABIType<Object> int64 = TypeFactory.create("int64");
assertEquals(Long.class, int64.clazz);
for (Object arg : args) {
testCast(arg.getClass(), int64, arg);
}
testCast(Integer.class, int64, 5);
}
{
final ABIType<Object> uint56 = TypeFactory.createNonCapturing("uint56");
final ABIType<Object> uint56 = TypeFactory.create("uint56");
assertEquals(Long.class, uint56.clazz);
for (Object arg : args) {
testCast(arg.getClass(), uint56, arg);
Expand All @@ -704,11 +704,11 @@ private static void testCast(Class<?> from, ABIType<Object> type, Object arg) th
@Test
public void testCasts2() throws Throwable {
assertThrown(ClassCastException.class, () -> ((ABIType<Object>) (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
Expand Down

0 comments on commit 44e90a1

Please sign in to comment.