diff --git a/build.gradle b/build.gradle index 2a6d525..f2545b0 100644 --- a/build.gradle +++ b/build.gradle @@ -50,7 +50,7 @@ sourceSets { // integrationTest.mustRunAfter test allprojects { group = 'org.fisco-bcos.code-generator' - version = '1.1.0' + version = '1.2.0-SNAPSHOT' apply plugin: 'maven-publish' apply plugin: 'idea' apply plugin: 'eclipse' diff --git a/src/main/java/org/fisco/bcos/codegen/v2/wrapper/SolidityContractWrapper.java b/src/main/java/org/fisco/bcos/codegen/v2/wrapper/SolidityContractWrapper.java index 5de004a..eb97518 100644 --- a/src/main/java/org/fisco/bcos/codegen/v2/wrapper/SolidityContractWrapper.java +++ b/src/main/java/org/fisco/bcos/codegen/v2/wrapper/SolidityContractWrapper.java @@ -241,9 +241,10 @@ private FieldSpec createDefinition(String type, String binayArrayName) { .build(); } - private FieldSpec createEventDefinition(String name, List parameters) { + private FieldSpec createEventDefinition( + String rawEventName, String name, List parameters) { - CodeBlock initializer = buildVariableLengthEventInitializer(name, parameters); + CodeBlock initializer = buildVariableLengthEventInitializer(rawEventName, parameters); return FieldSpec.builder(Event.class, buildEventDefinitionName(name)) .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) @@ -270,6 +271,20 @@ private static boolean isOverLoadFunction( return count > 1; } + private static boolean isOverLoadEvent(String name, List functionDefinitions) { + int count = 0; + for (ABIDefinition functionDefinition : functionDefinitions) { + if (!functionDefinition.getType().equals("event")) { + continue; + } + + if (functionDefinition.getName().equals(name)) { + count += 1; + } + } + return count > 1; + } + private List buildFunctionDefinitions( TypeSpec.Builder classBuilder, List functionDefinitions) throws ClassNotFoundException { @@ -302,7 +317,11 @@ private List buildFunctionDefinitions( } } } else if (functionDefinition.getType().equals("event")) { - methodSpecs.addAll(buildEventFunctions(functionDefinition, classBuilder)); + boolean isOverloadEvent = + isOverLoadEvent(functionDefinition.getName(), functionDefinitions); + + methodSpecs.addAll( + buildEventFunctions(functionDefinition, classBuilder, isOverloadEvent)); } } @@ -1209,9 +1228,10 @@ private MethodSpec buildEventTransactionReceiptFunction( } private List buildEventFunctions( - ABIDefinition functionDefinition, TypeSpec.Builder classBuilder) + ABIDefinition functionDefinition, TypeSpec.Builder classBuilder, boolean isOverload) throws ClassNotFoundException { - String functionName = functionDefinition.getName(); + + String functionName = getInputOutputFunctionName(functionDefinition, isOverload); List inputs = functionDefinition.getInputs(); String responseClassName = StringUtils.capitaliseFirstLetter(functionName) + "EventResponse"; @@ -1255,7 +1275,8 @@ private List buildEventFunctions( parameters.add(parameter); } - classBuilder.addField(createEventDefinition(functionName, parameters)); + classBuilder.addField( + createEventDefinition(functionDefinition.getName(), functionName, parameters)); classBuilder.addType( buildEventResponseObject( @@ -1819,12 +1840,21 @@ private static TypeName buildStructArrayTypeName(ABIDefinition.NamedType namedTy .structIdentifier()) .toString(); } else { - structName = - namedType - .getInternalType() - .substring( - namedType.getInternalType().lastIndexOf(" ") + 1, - namedType.getInternalType().indexOf("[")); + if (namedType.getInternalType().contains(".")) { + structName = + namedType + .getInternalType() + .substring( + namedType.getInternalType().lastIndexOf(".") + 1, + namedType.getInternalType().indexOf("[")); + } else { + structName = + namedType + .getInternalType() + .substring( + namedType.getInternalType().lastIndexOf(" ") + 1, + namedType.getInternalType().indexOf("[")); + } } return ParameterizedTypeName.get( diff --git a/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java b/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java index 7a94dc2..94c6c2b 100644 --- a/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java +++ b/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java @@ -837,12 +837,21 @@ private TypeName buildStructArrayTypeName(ABIDefinition.NamedType namedType) { namedType.getInternalType().lastIndexOf(".") + 1, namedType.getInternalType().indexOf("[")); } else { - structName = - namedType - .getInternalType() - .substring( - namedType.getInternalType().lastIndexOf(" ") + 1, - namedType.getInternalType().indexOf("[")); + if (namedType.getInternalType().contains(".")) { + structName = + namedType + .getInternalType() + .substring( + namedType.getInternalType().lastIndexOf(".") + 1, + namedType.getInternalType().indexOf("[")); + } else { + structName = + namedType + .getInternalType() + .substring( + namedType.getInternalType().lastIndexOf(" ") + 1, + namedType.getInternalType().indexOf("[")); + } } } diff --git a/src/test/java/org/fisco/bcos/codegen/v2/test/CodeGenV2Test.java b/src/test/java/org/fisco/bcos/codegen/v2/test/CodeGenV2Test.java index 1bf167f..6e76193 100644 --- a/src/test/java/org/fisco/bcos/codegen/v2/test/CodeGenV2Test.java +++ b/src/test/java/org/fisco/bcos/codegen/v2/test/CodeGenV2Test.java @@ -100,8 +100,23 @@ public void weidABICodeGen() throws IOException { codeGenTest(ABI_FILE, CONTRACT_NAME); } - // FIXME: v2 event override bug - // @Test + @Test + public void StructReqRespCodeGen() throws IOException { + // abi from https://github.com/FISCO-BCOS/code-generator/issues/19 + final String ABI_FILE = "StructReqRespContract.abi"; + final String CONTRACT_NAME = "StructReqRespContract"; + codeGenTest(ABI_FILE, CONTRACT_NAME); + } + + @Test + public void SupplyChainFinCodeGen() throws IOException { + // abi from https://github.com/FISCO-BCOS/java-sdk/issues/801 + final String ABI_FILE = "SupplyChainFin.abi"; + final String CONTRACT_NAME = "SupplyChainFin"; + codeGenTest(ABI_FILE, CONTRACT_NAME); + } + + @Test public void eventTestCodeGen() throws IOException { final String ABI_FILE = "EventSubDemo.abi"; final String CONTRACT_NAME = "EventSubDemo"; diff --git a/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java b/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java index 9d36a51..9308638 100644 --- a/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java +++ b/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java @@ -100,6 +100,22 @@ public void weidABICodeGen() throws IOException { codeGenTest(ABI_FILE, CONTRACT_NAME); } + @Test + public void StructReqRespCodeGen() throws IOException { + // abi from https://github.com/FISCO-BCOS/code-generator/issues/19 + final String ABI_FILE = "StructReqRespContract.abi"; + final String CONTRACT_NAME = "StructReqRespContract"; + codeGenTest(ABI_FILE, CONTRACT_NAME); + } + + @Test + public void SupplyChainFinCodeGen() throws IOException { + // abi from https://github.com/FISCO-BCOS/java-sdk/issues/801 + final String ABI_FILE = "SupplyChainFin.abi"; + final String CONTRACT_NAME = "SupplyChainFin"; + codeGenTest(ABI_FILE, CONTRACT_NAME); + } + @Test public void liquidCodeTestCodeGen() throws IOException { final String ABI_FILE = "codec_test.abi"; diff --git a/src/test/resources/StructReqRespContract.abi b/src/test/resources/StructReqRespContract.abi new file mode 100644 index 0000000..bfd744e --- /dev/null +++ b/src/test/resources/StructReqRespContract.abi @@ -0,0 +1 @@ +[{"inputs":[{"components":[{"components":[{"internalType":"string","name":"req","type":"string"}],"internalType":"struct StructLibrary.Req[]","name":"reqs","type":"tuple[]"}],"internalType":"struct StructLibrary.StructReq","name":"req","type":"tuple"}],"name":"request","outputs":[{"components":[{"internalType":"string","name":"resp","type":"string"}],"internalType":"struct StructLibrary.StructResp","name":"resp","type":"tuple"}],"stateMutability":"nonpayable","type":"function"}] \ No newline at end of file diff --git a/src/test/resources/SupplyChainFin.abi b/src/test/resources/SupplyChainFin.abi new file mode 100644 index 0000000..b9d67e8 --- /dev/null +++ b/src/test/resources/SupplyChainFin.abi @@ -0,0 +1 @@ +[{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"banks","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"senderAddress","type":"address"},{"name":"accepterAddress","type":"address"},{"name":"amount","type":"uint256"},{"name":"receiptType","type":"uint8"}],"name":"companyToCompanyReceipt","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"companies","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"},{"name":"bankAddress","type":"address"},{"name":"credit","type":"uint256"}],"name":"addBank","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"},{"name":"companyAddress","type":"address"}],"name":"addCompany","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"senderAddress","type":"address"},{"name":"accepterAddress","type":"address"},{"name":"amount","type":"uint256"},{"name":"receiptType","type":"uint8"}],"name":"companyToBankReceipt","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"companyAddress","type":"address"}],"name":"getCompany","outputs":[{"name":"","type":"string"},{"name":"","type":"address"},{"name":"","type":"uint256"},{"name":"","type":"uint256[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getAllBankAddress","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"receiptIndex","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"senderAddress","type":"address"},{"name":"accepterAddress","type":"address"},{"name":"amount","type":"uint256"},{"name":"receiptType","type":"uint8"}],"name":"bankToCompanyReceipt","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"bankAddress","type":"address"}],"name":"getBank","outputs":[{"name":"","type":"string"},{"name":"","type":"address"},{"name":"","type":"uint256"},{"name":"","type":"uint256[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"superviosrIns","outputs":[{"name":"supervisorName","type":"string"},{"name":"supervisorAddress","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"index","type":"uint256"}],"name":"getReceipt","outputs":[{"name":"","type":"address"},{"name":"","type":"address"},{"name":"","type":"uint8"},{"name":"","type":"uint8"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getAllCompanyAddress","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"name","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"}] \ No newline at end of file