Skip to content

Commit

Permalink
Fix instantiateType method (hyperledger-web3j#1173)
Browse files Browse the repository at this point in the history
The instantiateArrayType method used by instantiateType did not return an Array with the correct subtype
  • Loading branch information
bhemery authored Mar 12, 2020
1 parent c0b7b9c commit 935b338
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion abi/src/main/java/org/web3j/abi/TypeDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static Type instantiateArrayType(TypeReference ref, Object value)
for (Object o : values) {
transformedList.add(instantiateType(subTypeReference, o));
}
return (Type) listcons.newInstance(ref.getClassType(), transformedList);
return (Type) listcons.newInstance(subTypeReference.getClassType(), transformedList);
}

static Type instantiateAtomicType(Class<?> referenceClass, Object value)
Expand Down
9 changes: 8 additions & 1 deletion abi/src/test/java/org/web3j/abi/TypeDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,10 @@ public void testStaticArray() throws Exception {

assertTrue(arr instanceof StaticArray2);
StaticArray2 staticArray2 = (StaticArray2) arr;
assertEquals(staticArray2.getValue().get(0), (new Uint256(BigInteger.TEN)));

assertEquals(staticArray2.getComponentType(), Uint256.class);

assertEquals(staticArray2.getValue().get(0), (new Uint256(BigInteger.TEN)));
assertEquals(
staticArray2.getValue().get(1), (new Uint256(BigInteger.valueOf(Long.MAX_VALUE))));
}
Expand Down Expand Up @@ -1135,6 +1137,8 @@ public void testDynamicArray() throws Exception {
assertTrue(arr instanceof DynamicArray);
DynamicArray dynamicArray = (DynamicArray) arr;

assertEquals(dynamicArray.getComponentType(), Utf8String.class);

assertEquals(dynamicArray.getValue().get(0), (new Utf8String("Hello, world!")));

assertEquals(dynamicArray.getValue().get(1), (new Utf8String("world! Hello,")));
Expand All @@ -1153,12 +1157,15 @@ public void multiDimArrays() throws Exception {
assertTrue(twoDim instanceof StaticArray3);
StaticArray3<DynamicArray<Uint256>> staticArray3 =
(StaticArray3<DynamicArray<Uint256>>) twoDim;
assertEquals(staticArray3.getComponentType(), DynamicArray.class);
DynamicArray<Uint256> row1 = staticArray3.getValue().get(1);
assertEquals(row1.getValue().get(2), new Uint256(3));

Type threeDim = TypeDecoder.instantiateType("uint256[][3][3]", bytes3d);
assertTrue(threeDim instanceof StaticArray3);
StaticArray3<StaticArray3<DynamicArray<Uint256>>> staticArray3StaticArray3 =
(StaticArray3<StaticArray3<DynamicArray<Uint256>>>) threeDim;
assertEquals(staticArray3StaticArray3.getComponentType(), StaticArray3.class);
row1 = staticArray3StaticArray3.getValue().get(1).getValue().get(1);
assertEquals(row1.getValue().get(1), (new Uint256(2)));
}
Expand Down

0 comments on commit 935b338

Please sign in to comment.