diff --git a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java index d451fba18..e7e3838d3 100644 --- a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java +++ b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java @@ -449,7 +449,12 @@ private List buildStructTypes(final List functionDefini } else { final String fullStructName = internalType.substring(internalType.lastIndexOf(" ") + 1); - structName = fullStructName.substring(fullStructName.lastIndexOf(".") + 1); + final String tempStructName = + fullStructName.substring(fullStructName.lastIndexOf(".") + 1); + structName = + SourceVersion.isName(tempStructName) + ? tempStructName + : "_" + tempStructName; } final TypeSpec.Builder builder = @@ -513,22 +518,25 @@ private List buildStructTypes(final List functionDefini .build(); } } - builder.addField(typeName, component.getName(), Modifier.PUBLIC); - constructorBuilder.addParameter(typeName, component.getName()); + final String componentName = + !SourceVersion.isName(component.getName()) + ? "_" + component.getName() + : component.getName(); + builder.addField(typeName, componentName, Modifier.PUBLIC); + constructorBuilder.addParameter(typeName, componentName); ParameterSpec.Builder nativeParameterBuilder = - ParameterSpec.builder(nativeTypeName, component.getName()); + ParameterSpec.builder(nativeTypeName, componentName); if (annotationSpec != null) { nativeParameterBuilder.addAnnotation(annotationSpec); } nativeConstructorBuilder.addParameter(nativeParameterBuilder.build()); - constructorBuilder.addStatement( - "this." + component.getName() + " = " + component.getName()); + constructorBuilder.addStatement("this." + componentName + " = " + componentName); nativeConstructorBuilder.addStatement( "this." - + component.getName() + + componentName + " = " - + component.getName() + + componentName + adjustToNativeTypeIfNecessary(component), Collectors.class); } @@ -1014,8 +1022,11 @@ String addParameters( for (int i = 0; i < inputParameterTypes.size(); ++i) { final TypeName typeName = getTypenameForArray(namedTypes.get(i), useJavaPrimitiveTypes); - nativeInputParameterTypes.add( - ParameterSpec.builder(typeName, inputParameterTypes.get(i).name).build()); + final String paramName = + !SourceVersion.isName(inputParameterTypes.get(i).name) + ? "_" + inputParameterTypes.get(i).name + : inputParameterTypes.get(i).name; + nativeInputParameterTypes.add(ParameterSpec.builder(typeName, paramName).build()); } methodBuilder.addParameters(nativeInputParameterTypes); @@ -1273,6 +1284,9 @@ static String createValidParamName(String name, int idx) { if (name == null || name.equals("")) { return "param" + idx; } else { + if (!SourceVersion.isName(name)) { + name = "_" + name; + } return name; } } diff --git a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java index 2f15a1130..0faa58a5a 100644 --- a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java +++ b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperTest.java @@ -77,6 +77,7 @@ public void setUp() throws Exception { public void testCreateValidParamName() { assertEquals(createValidParamName("param", 1), ("param")); assertEquals(createValidParamName("", 1), ("param1")); + assertEquals(createValidParamName("class", 1), ("_class")); } @Test diff --git a/codegen/src/test/resources/solidity/javareservedwords/JavaReservedWords.sol b/codegen/src/test/resources/solidity/javareservedwords/JavaReservedWords.sol new file mode 100644 index 000000000..291aa9838 --- /dev/null +++ b/codegen/src/test/resources/solidity/javareservedwords/JavaReservedWords.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract JavaReservedWords { + + string public goto; + + struct extends { + uint class; + } + + function void(extends calldata protected) external pure returns (uint) { + return protected.class; + } +} \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/javareservedwords/build/JavaReservedWords.abi b/codegen/src/test/resources/solidity/javareservedwords/build/JavaReservedWords.abi new file mode 100644 index 000000000..8a1dc513f --- /dev/null +++ b/codegen/src/test/resources/solidity/javareservedwords/build/JavaReservedWords.abi @@ -0,0 +1 @@ +[{"inputs":[],"name":"goto","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"class","type":"uint256"}],"internalType":"struct JavaReservedWords.extends","name":"protected","type":"tuple"}],"name":"void","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"}] \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/javareservedwords/build/JavaReservedWords.bin b/codegen/src/test/resources/solidity/javareservedwords/build/JavaReservedWords.bin new file mode 100644 index 000000000..f9ea67423 --- /dev/null +++ b/codegen/src/test/resources/solidity/javareservedwords/build/JavaReservedWords.bin @@ -0,0 +1 @@ +608060405234801561001057600080fd5b506102f7806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634f9f14921461003b578063c82ef2191461006b575b600080fd5b6100556004803603810190610050919061014e565b610089565b6040516100629190610194565b60405180910390f35b610073610097565b604051610080919061023f565b60405180910390f35b600081600001359050919050565b600080546100a490610290565b80601f01602080910402602001604051908101604052809291908181526020018280546100d090610290565b801561011d5780601f106100f25761010080835404028352916020019161011d565b820191906000526020600020905b81548152906001019060200180831161010057829003601f168201915b505050505081565b600080fd5b600080fd5b6000602082840312156101455761014461012a565b5b81905092915050565b60006020828403121561016457610163610125565b5b60006101728482850161012f565b91505092915050565b6000819050919050565b61018e8161017b565b82525050565b60006020820190506101a96000830184610185565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b838110156101e95780820151818401526020810190506101ce565b60008484015250505050565b6000601f19601f8301169050919050565b6000610211826101af565b61021b81856101ba565b935061022b8185602086016101cb565b610234816101f5565b840191505092915050565b600060208201905081810360008301526102598184610206565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102a857607f821691505b6020821081036102bb576102ba610261565b5b5091905056fea2646970667358221220362294dad2b9d49b00406366bbec2361741b4ccb8917e718b625160087de448464736f6c63430008120033 \ No newline at end of file diff --git a/integration-tests/src/test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java b/integration-tests/src/test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java index 103a74012..968036d03 100644 --- a/integration-tests/src/test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java +++ b/integration-tests/src/test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java @@ -23,6 +23,7 @@ import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.protocol.http.HttpService; import org.web3j.test.contract.Fibonacci; +import org.web3j.test.contract.JavaReservedWords; import org.web3j.tx.gas.ContractGasProvider; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -38,12 +39,32 @@ public class FunctionWrappersIT extends Scenario { private static Fibonacci fib; + private static JavaReservedWords javaReservedWords; @BeforeAll public static void setUp(Web3j web3j, ContractGasProvider contractGasProvider) throws Exception { Scenario.web3j = web3j; FunctionWrappersIT.fib = Fibonacci.deploy(web3j, ALICE, contractGasProvider).send(); + FunctionWrappersIT.javaReservedWords = + JavaReservedWords.deploy(web3j, ALICE, contractGasProvider).send(); + } + + @Test + public void testJavaReservedWords() throws Exception { + + JavaReservedWords javaReservedWordsContract = + JavaReservedWords.load( + javaReservedWords.getContractAddress(), + Web3j.build(new HttpService()), + ALICE, + STATIC_GAS_PROVIDER); + + BigInteger result = + javaReservedWordsContract + ._void(new JavaReservedWords._extends(BigInteger.TEN)) + .send(); + assertEquals(result, (BigInteger.valueOf(10))); } @Test diff --git a/integration-tests/src/test/resources/JavaReservedWords.bin b/integration-tests/src/test/resources/JavaReservedWords.bin new file mode 100644 index 000000000..f9ea67423 --- /dev/null +++ b/integration-tests/src/test/resources/JavaReservedWords.bin @@ -0,0 +1 @@ +608060405234801561001057600080fd5b506102f7806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634f9f14921461003b578063c82ef2191461006b575b600080fd5b6100556004803603810190610050919061014e565b610089565b6040516100629190610194565b60405180910390f35b610073610097565b604051610080919061023f565b60405180910390f35b600081600001359050919050565b600080546100a490610290565b80601f01602080910402602001604051908101604052809291908181526020018280546100d090610290565b801561011d5780601f106100f25761010080835404028352916020019161011d565b820191906000526020600020905b81548152906001019060200180831161010057829003601f168201915b505050505081565b600080fd5b600080fd5b6000602082840312156101455761014461012a565b5b81905092915050565b60006020828403121561016457610163610125565b5b60006101728482850161012f565b91505092915050565b6000819050919050565b61018e8161017b565b82525050565b60006020820190506101a96000830184610185565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b838110156101e95780820151818401526020810190506101ce565b60008484015250505050565b6000601f19601f8301169050919050565b6000610211826101af565b61021b81856101ba565b935061022b8185602086016101cb565b610234816101f5565b840191505092915050565b600060208201905081810360008301526102598184610206565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102a857607f821691505b6020821081036102bb576102ba610261565b5b5091905056fea2646970667358221220362294dad2b9d49b00406366bbec2361741b4ccb8917e718b625160087de448464736f6c63430008120033 \ No newline at end of file