diff --git a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java index 3461d1c3a..410ed2c01 100644 --- a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java +++ b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java @@ -14,6 +14,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; + +import javax.lang.model.SourceVersion; import javax.lang.model.element.Modifier; import com.fasterxml.jackson.databind.ObjectMapper; @@ -680,6 +682,12 @@ MethodSpec buildFunction( AbiDefinition functionDefinition) throws ClassNotFoundException { String functionName = functionDefinition.getName(); + // If the solidity function name is a reserved word + // in the current java version prepend it with "_" + if(!SourceVersion.isName(functionName)) { + functionName = "_" + functionName; + } + MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(functionName) .addModifiers(Modifier.PUBLIC); diff --git a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java index 09b63da95..b5f64be8c 100644 --- a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java +++ b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java @@ -85,6 +85,12 @@ public void testShipIt() throws Exception { testCodeGenerationSolidityTypes("shipit", "ShipIt"); } + @Test + public void testMisc() throws Exception { + testCodeGenerationJvmTypes("misc", "Misc"); + testCodeGenerationSolidityTypes("misc", "Misc"); + } + @Test public void testContractsNoBin() throws Exception { testCodeGeneration("contracts", "HumanStandardToken", JAVA_TYPES_ARG, false); diff --git a/codegen/src/test/resources/solidity/build.sh b/codegen/src/test/resources/solidity/build.sh index f7ab4ee86..a038792a0 100755 --- a/codegen/src/test/resources/solidity/build.sh +++ b/codegen/src/test/resources/solidity/build.sh @@ -4,7 +4,8 @@ targets=" arrays/Arrays contracts/HumanStandardToken fibonacci/Fibonacci -greeter/Greeter +greeter/Greeter] +misc/Misc shipit/ShipIt simplestorage/SimpleStorage " diff --git a/codegen/src/test/resources/solidity/misc/Misc.sol b/codegen/src/test/resources/solidity/misc/Misc.sol new file mode 100644 index 000000000..95ebff181 --- /dev/null +++ b/codegen/src/test/resources/solidity/misc/Misc.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.4.0; + +contract Misc { + + constructor() public {} + + function double(uint a) public pure returns(uint) { + return 2*a; + } +} diff --git a/codegen/src/test/resources/solidity/misc/build/Misc.abi b/codegen/src/test/resources/solidity/misc/build/Misc.abi new file mode 100644 index 000000000..2f0ff46d0 --- /dev/null +++ b/codegen/src/test/resources/solidity/misc/build/Misc.abi @@ -0,0 +1 @@ +[{"constant":true,"inputs":[{"name":"a","type":"uint256"}],"name":"double","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}] \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/misc/build/Misc.bin b/codegen/src/test/resources/solidity/misc/build/Misc.bin new file mode 100644 index 000000000..d2eb6ba9c --- /dev/null +++ b/codegen/src/test/resources/solidity/misc/build/Misc.bin @@ -0,0 +1 @@ +6080604052348015600f57600080fd5b50609c8061001e6000396000f300608060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663eee9720681146043575b600080fd5b348015604e57600080fd5b506058600435606a565b60408051918252519081900360200190f35b600202905600a165627a7a72305820463dc8fd1001eab87e0d065568e214f4bcf701a33b61a213fa5c607b116689ed0029 \ No newline at end of file