Skip to content

Commit

Permalink
Merge pull request #1975 from web3j/nicks/issue-1773
Browse files Browse the repository at this point in the history
Fixed java reserved words codegen errors
  • Loading branch information
gtebrean authored Oct 18, 2023
2 parents 2944092 + a835a28 commit dba4dfb
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,12 @@ private List<TypeSpec> buildStructTypes(final List<AbiDefinition> 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 =
Expand Down Expand Up @@ -513,22 +518,25 @@ private List<TypeSpec> buildStructTypes(final List<AbiDefinition> 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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b506102f7806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634f9f14921461003b578063c82ef2191461006b575b600080fd5b6100556004803603810190610050919061014e565b610089565b6040516100629190610194565b60405180910390f35b610073610097565b604051610080919061023f565b60405180910390f35b600081600001359050919050565b600080546100a490610290565b80601f01602080910402602001604051908101604052809291908181526020018280546100d090610290565b801561011d5780601f106100f25761010080835404028352916020019161011d565b820191906000526020600020905b81548152906001019060200180831161010057829003601f168201915b505050505081565b600080fd5b600080fd5b6000602082840312156101455761014461012a565b5b81905092915050565b60006020828403121561016457610163610125565b5b60006101728482850161012f565b91505092915050565b6000819050919050565b61018e8161017b565b82525050565b60006020820190506101a96000830184610185565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b838110156101e95780820151818401526020810190506101ce565b60008484015250505050565b6000601f19601f8301169050919050565b6000610211826101af565b61021b81856101ba565b935061022b8185602086016101cb565b610234816101f5565b840191505092915050565b600060208201905081810360008301526102598184610206565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102a857607f821691505b6020821081036102bb576102ba610261565b5b5091905056fea2646970667358221220362294dad2b9d49b00406366bbec2361741b4ccb8917e718b625160087de448464736f6c63430008120033
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/test/resources/JavaReservedWords.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b506102f7806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634f9f14921461003b578063c82ef2191461006b575b600080fd5b6100556004803603810190610050919061014e565b610089565b6040516100629190610194565b60405180910390f35b610073610097565b604051610080919061023f565b60405180910390f35b600081600001359050919050565b600080546100a490610290565b80601f01602080910402602001604051908101604052809291908181526020018280546100d090610290565b801561011d5780601f106100f25761010080835404028352916020019161011d565b820191906000526020600020905b81548152906001019060200180831161010057829003601f168201915b505050505081565b600080fd5b600080fd5b6000602082840312156101455761014461012a565b5b81905092915050565b60006020828403121561016457610163610125565b5b60006101728482850161012f565b91505092915050565b6000819050919050565b61018e8161017b565b82525050565b60006020820190506101a96000830184610185565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b838110156101e95780820151818401526020810190506101ce565b60008484015250505050565b6000601f19601f8301169050919050565b6000610211826101af565b61021b81856101ba565b935061022b8185602086016101cb565b610234816101f5565b840191505092915050565b600060208201905081810360008301526102598184610206565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102a857607f821691505b6020821081036102bb576102ba610261565b5b5091905056fea2646970667358221220362294dad2b9d49b00406366bbec2361741b4ccb8917e718b625160087de448464736f6c63430008120033

0 comments on commit dba4dfb

Please sign in to comment.