Skip to content

Commit

Permalink
Prepend solidity function name with "_"
Browse files Browse the repository at this point in the history
if it is a reserved keyword in the current java version
  • Loading branch information
iikirilov committed Nov 7, 2018
1 parent 6160282 commit 3e51952
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion codegen/src/test/resources/solidity/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ targets="
arrays/Arrays
contracts/HumanStandardToken
fibonacci/Fibonacci
greeter/Greeter
greeter/Greeter]
misc/Misc
shipit/ShipIt
simplestorage/SimpleStorage
"
Expand Down
10 changes: 10 additions & 0 deletions codegen/src/test/resources/solidity/misc/Misc.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pragma solidity ^0.4.0;

contract Misc {

constructor() public {}

function double(uint a) public pure returns(uint) {
return 2*a;
}
}
1 change: 1 addition & 0 deletions codegen/src/test/resources/solidity/misc/build/Misc.abi
Original file line number Diff line number Diff line change
@@ -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"}]
1 change: 1 addition & 0 deletions codegen/src/test/resources/solidity/misc/build/Misc.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6080604052348015600f57600080fd5b50609c8061001e6000396000f300608060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663eee9720681146043575b600080fd5b348015604e57600080fd5b506058600435606a565b60408051918252519081900360200190f35b600202905600a165627a7a72305820463dc8fd1001eab87e0d065568e214f4bcf701a33b61a213fa5c607b116689ed0029

0 comments on commit 3e51952

Please sign in to comment.