Skip to content

Commit

Permalink
Merge pull request #2076 from Antlion12/TrulyDynamicStructs
Browse files Browse the repository at this point in the history
Updated TypeReference and TypeDecoder to support decoding of dynamic structs and dynamic struct arrays without a priori Class definitions.
  • Loading branch information
gtebrean authored Aug 8, 2024
2 parents b73dba0 + ca59055 commit bb9dc1c
Show file tree
Hide file tree
Showing 8 changed files with 587 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

* bump snapshot version to 4.12.1 [#2058](https://github.com/hyperledger/web3j/pull/2058)
* Update maintainer requirements status [#2064](https://github.com/hyperledger/web3j/pull/2064)
* Add struct support in java without the need of having a corresponding Java class [#2076](https://github.com/hyperledger/web3j/pull/2076)

### BREAKING CHANGES

Expand Down
295 changes: 272 additions & 23 deletions abi/src/main/java/org/web3j/abi/TypeDecoder.java

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions abi/src/main/java/org/web3j/abi/TypeReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -39,10 +40,17 @@ public abstract class TypeReference<T extends org.web3j.abi.datatypes.Type>
private final Type type;
private final boolean indexed;

protected List<TypeReference<?>> innerTypes;

protected TypeReference() {
this(false);
}

protected TypeReference(boolean indexed, List<TypeReference<?>> innerTypesIn) {
this(indexed);
this.innerTypes = innerTypesIn;
}

protected TypeReference(boolean indexed) {
Type superclass = getClass().getGenericSuperclass();
if (superclass instanceof Class) {
Expand All @@ -59,10 +67,14 @@ protected TypeReference(boolean indexed) {
*
* @return the type wrapped by this Array TypeReference, or null if not Array
*/
TypeReference getSubTypeReference() {
public TypeReference getSubTypeReference() {
return null;
}

public List<TypeReference<?>> getInnerTypes() {
return this.innerTypes;
}

public int compareTo(TypeReference<T> o) {
// taken from the blog post comments - this results in an error if the
// type parameter is left out.
Expand Down Expand Up @@ -175,7 +187,7 @@ public static TypeReference makeTypeReference(
arrayWrappedType =
new TypeReference<DynamicArray>(indexed) {
@Override
TypeReference getSubTypeReference() {
public TypeReference getSubTypeReference() {
return baseTr;
}

Expand Down Expand Up @@ -213,7 +225,7 @@ public java.lang.reflect.Type getOwnerType() {
new TypeReference.StaticArrayTypeReference<StaticArray>(arraySizeInt) {

@Override
TypeReference getSubTypeReference() {
public TypeReference getSubTypeReference() {
return baseTr;
}

Expand Down
2 changes: 1 addition & 1 deletion abi/src/main/java/org/web3j/abi/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static String getStructType(Class type) {
public static TypeReference<DynamicArray> getDynamicArrayTypeReference(Class parameter) {
return new TypeReference<DynamicArray>() {
@Override
TypeReference getSubTypeReference() {
public TypeReference getSubTypeReference() {
return TypeReference.create(parameter);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public class DynamicStruct extends DynamicArray<Type> implements StructType {

private final List<Class<Type>> itemTypes = new ArrayList<>();
public final List<Class<Type>> itemTypes = new ArrayList<>();

public DynamicStruct(List<Type> values) {
this(Type.class, values);
Expand Down
290 changes: 290 additions & 0 deletions abi/src/test/java/org/web3j/abi/FunctionReturnDecoderTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ public void testArrayOfStructAndStructCompareJavaFile() throws Exception {
@Test
public void testStaticArrayOfStructsInStructGeneration() throws Exception {
testCodeGeneration(
"staticarrayofstructsinstruct", "StaticArrayOfStructsInStruct", JAVA_TYPES_ARG, false);
"staticarrayofstructsinstruct",
"StaticArrayOfStructsInStruct",
JAVA_TYPES_ARG,
false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ public void setComponents(final List<NamedType> components) {

public String structIdentifier() {
return ((internalType == null ? type : internalType.isEmpty() ? type : internalType)
+ components.stream()
.map(NamedType::structIdentifier)
.collect(Collectors.joining()));
+ components.stream()
.map(NamedType::structIdentifier)
.collect(Collectors.joining()));
}

public int nestedness() {
Expand Down

0 comments on commit bb9dc1c

Please sign in to comment.