Skip to content

Commit

Permalink
<fix>(V2,V3): fix call get struct value bug. (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay authored Jun 4, 2024
1 parent 59a4815 commit 3866d69
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest]
os: [macos-12]
steps:
- uses: actions/checkout@v2
with:
Expand Down
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ ext {
ossrhPassword = "xxx"
}
// jackson version
jacksonVersion = '2.14.1'
jacksonVersion = '2.17.0'
javapoetVersion = '1.13.0'
picocliVersion = '4.6.1'
picocliVersion = '4.7.6'
junitVersion = '4.13.2'
commonsLang3Version = '3.12.0'
commonsLang3Version = '3.14.0'

javaSDKVersion3 = "3.7.0"
javaSDKVersion2 = "2.10.0"
slf4jVersion = "1.7.32"
slf4jVersion = "1.7.36"
}

sourceSets {
Expand All @@ -54,7 +54,7 @@ configurations.all {
// integrationTest.mustRunAfter test
allprojects {
group = 'org.fisco-bcos.code-generator'
version = '1.5.0'
version = '1.6.0-SNAPSHOT'
apply plugin: 'maven-publish'
apply plugin: 'idea'
apply plugin: 'eclipse'
Expand Down Expand Up @@ -86,7 +86,7 @@ allprojects {
api("org.slf4j:slf4j-api:${slf4jVersion}")
api("org.apache.commons:commons-lang3:${commonsLang3Version}")
testImplementation("junit:junit:${junitVersion}")
testImplementation("org.mockito:mockito-core:4.6.0")
testImplementation('org.mockito:mockito-core:5.11.0')
}

clean.doLast {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/fisco/bcos/codegen/CodeGenMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public void run() {
packageName)
.generateJavaFiles();
} catch (Exception e) {
e.printStackTrace();
org.fisco.bcos.codegen.v2.utils.CodeGenUtils.exitError(e);
}
} else if (version.equals(Version.V3)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,10 @@ private MethodSpec buildFunctionWithInputDecoder(
List.class,
FunctionReturnDecoder.class);

buildTupleResultContainer0(
buildTupleResultContainer(
methodBuilder,
parameterizedTupleType,
inputTypes,
buildTypeNames(functionDefinition.getInputs()));

return methodBuilder.build();
Expand Down Expand Up @@ -963,9 +964,10 @@ private MethodSpec buildFunctionWithOutputDecoder(
List.class,
FunctionReturnDecoder.class);

buildTupleResultContainer0(
buildTupleResultContainer(
methodBuilder,
parameterizedTupleType,
outputTypes,
buildTypeNames(functionDefinition.getOutputs()));

return methodBuilder.build();
Expand Down Expand Up @@ -1070,7 +1072,15 @@ private void buildConstantFunction(
buildVariableLengthReturnFunctionConstructor(
methodBuilder, functionName, inputParams, outputParameterTypes);

buildTupleResultContainer(methodBuilder, parameterizedTupleType, outputParameterTypes);
methodBuilder.addStatement(
"$T results = executeCallWithMultipleValueReturn(function)",
ParameterizedTypeName.get(
List.class, org.fisco.bcos.sdk.abi.datatypes.Type.class));
buildTupleResultContainer(
methodBuilder,
parameterizedTupleType,
functionDefinition.getOutputs(),
outputParameterTypes);
}
}

Expand Down Expand Up @@ -1497,31 +1507,33 @@ private static void buildVariableLengthReturnFunctionConstructor(
private void buildTupleResultContainer(
MethodSpec.Builder methodBuilder,
ParameterizedTypeName tupleType,
List<ABIDefinition.NamedType> namedTypes,
List<TypeName> outputParameterTypes) {

List<TypeName> typeArguments = tupleType.typeArguments;

CodeBlock.Builder tupleConstructor = CodeBlock.builder();
tupleConstructor
.addStatement(
"$T results = executeCallWithMultipleValueReturn(function)",
ParameterizedTypeName.get(List.class, Type.class))
.add("return new $T(", tupleType)
.add("$>$>");
CodeBlock.Builder codeBuilder = CodeBlock.builder();

String resultStringSimple = "\n($T) results.get($L)";
resultStringSimple += ".getValue()";
String getValueString = ".getValue()";

String resultStringNativeList = "\nconvertToNative(($T) results.get($L).getValue())";
String dynamicResultStringList =
"\nnew DynamicArray<>($T.class,($T) results.get($L).getValue())";

int size = typeArguments.size();
ClassName classList = ClassName.get(List.class);
ClassName dynamicArray = ClassName.get(org.fisco.bcos.sdk.abi.datatypes.DynamicArray.class);
ClassName staticArray = ClassName.get(org.fisco.bcos.sdk.abi.datatypes.StaticArray.class);

for (int i = 0; i < size; i++) {
TypeName param = outputParameterTypes.get(i);
TypeName convertTo = typeArguments.get(i);

ABIDefinition.NamedType namedType = namedTypes.get(i);
String resultString = resultStringSimple;
if (!namedType.getType().contains("tuple")) {
resultString += getValueString;
}

// If we use native java types we need to convert
// elements of arrays to native java types too
Expand All @@ -1534,50 +1546,15 @@ private void buildTupleResultContainer(
ParameterizedTypeName.get(classList, oldContainer.typeArguments.get(0));
resultString = resultStringNativeList;
}
}

tupleConstructor.add(resultString, convertTo, i);
tupleConstructor.add(i < size - 1 ? ", " : ");\n");
}
tupleConstructor.add("$<$<");
methodBuilder.returns(tupleType).addCode(tupleConstructor.build());
}

private void buildTupleResultContainer0(
MethodSpec.Builder methodBuilder,
ParameterizedTypeName tupleType,
List<TypeName> outputParameterTypes) {

List<TypeName> typeArguments = tupleType.typeArguments;

CodeBlock.Builder codeBuilder = CodeBlock.builder();

String resultStringSimple = "\n($T) results.get($L)";
String resultGetValue = ".getValue()";

String resultStringNativeList = "\nconvertToNative(($T) results.get($L).getValue())";

int size = typeArguments.size();
ClassName classList = ClassName.get(List.class);

for (int i = 0; i < size; i++) {
TypeName param = outputParameterTypes.get(i);
TypeName convertTo = typeArguments.get(i);

String resultString = resultStringSimple + resultGetValue;

// If we use native java types we need to convert
// elements of arrays to native java types too
if (param.equals(convertTo)) {
resultString = resultStringSimple;
} else if (param instanceof ParameterizedTypeName) {
ParameterizedTypeName oldContainer = (ParameterizedTypeName) param;
ParameterizedTypeName newContainer = (ParameterizedTypeName) convertTo;
if (newContainer.rawType.compareTo(classList) == 0
if ((newContainer.rawType.compareTo(dynamicArray) == 0
|| newContainer.rawType.compareTo(staticArray) == 0)
&& newContainer.typeArguments.size() == 1) {
resultString = dynamicResultStringList;
convertTo =
ParameterizedTypeName.get(classList, oldContainer.typeArguments.get(0));
resultString = resultStringNativeList;
codeBuilder.add(resultString, oldContainer.typeArguments.get(0), convertTo, i);
codeBuilder.add(i < size - 1 ? ", " : "\n");
continue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ private MethodSpec buildFunctionWithInputDecoder(
"$T<Type> results = this.functionReturnDecoder.decode(data, function.getOutputParameters())",
List.class);

this.buildTupleResultContainer0(
this.buildTupleResultContainer(
methodBuilder,
parameterizedTupleType,
inputTypes,
Expand Down Expand Up @@ -1450,7 +1450,7 @@ private MethodSpec buildFunctionWithOutputDecoder(
"$T<Type> results = this.functionReturnDecoder.decode(data, function.getOutputParameters())",
List.class);

this.buildTupleResultContainer0(
this.buildTupleResultContainer(
methodBuilder,
parameterizedTupleType,
outputTypes,
Expand Down Expand Up @@ -1769,8 +1769,14 @@ private void buildConstantFunction(
buildVariableLengthReturnFunctionConstructor(
methodBuilder, functionName, inputParams, outputParameterTypes);

methodBuilder.addStatement(
"$T results = executeCallWithMultipleValueReturn(function)",
ParameterizedTypeName.get(List.class, Type.class));
this.buildTupleResultContainer(
methodBuilder, parameterizedTupleType, outputParameterTypes);
methodBuilder,
parameterizedTupleType,
functionDefinition.getOutputs(),
outputParameterTypes);
}
}

Expand Down Expand Up @@ -2230,68 +2236,6 @@ private static void buildVariableLengthReturnFunctionConstructor(
}

private void buildTupleResultContainer(
MethodSpec.Builder methodBuilder,
ParameterizedTypeName tupleType,
List<TypeName> outputParameterTypes) {

List<TypeName> typeArguments = tupleType.typeArguments;

CodeBlock.Builder tupleConstructor = CodeBlock.builder();
tupleConstructor
.addStatement(
"$T results = executeCallWithMultipleValueReturn(function)",
ParameterizedTypeName.get(List.class, Type.class))
.add("return new $T(", tupleType)
.add("$>$>");

String resultStringSimple = "\n($T) results.get($L)";
resultStringSimple += ".getValue()";

String resultStringNativeList = "\nconvertToNative(($T) results.get($L).getValue())";
String dynamicResultStringList =
"\nnew DynamicArray<>($T.class,($T) results.get($L).getValue())";

int size = typeArguments.size();
ClassName classList = ClassName.get(List.class);
ClassName dynamicArray = ClassName.get(DynamicArray.class);

for (int i = 0; i < size; i++) {
TypeName param = outputParameterTypes.get(i);
TypeName convertTo = typeArguments.get(i);

String resultString = resultStringSimple;

// If we use native java types we need to convert
// elements of arrays to native java types too
if (param instanceof ParameterizedTypeName) {
ParameterizedTypeName oldContainer = (ParameterizedTypeName) param;
ParameterizedTypeName newContainer = (ParameterizedTypeName) convertTo;
if (newContainer.rawType.compareTo(classList) == 0
&& newContainer.typeArguments.size() == 1) {
convertTo =
ParameterizedTypeName.get(classList, oldContainer.typeArguments.get(0));
resultString = resultStringNativeList;
}
if (newContainer.rawType.compareTo(dynamicArray) == 0
&& newContainer.typeArguments.size() == 1) {
resultString = dynamicResultStringList;
convertTo =
ParameterizedTypeName.get(classList, oldContainer.typeArguments.get(0));
tupleConstructor.add(
resultString, oldContainer.typeArguments.get(0), convertTo, i);
tupleConstructor.add(i < size - 1 ? ", " : ");\n");
continue;
}
}

tupleConstructor.add(resultString, convertTo, i);
tupleConstructor.add(i < size - 1 ? ", " : ");\n");
}
tupleConstructor.add("$<$<");
methodBuilder.returns(tupleType).addCode(tupleConstructor.build());
}

private void buildTupleResultContainer0(
MethodSpec.Builder methodBuilder,
ParameterizedTypeName tupleType,
List<ABIDefinition.NamedType> namedTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public void codecTestABICodeGen() throws IOException {
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void RechargeTestCodeGen() throws IOException {
final String ABI_FILE = "IRechargeV2.abi";
final String CONTRACT_NAME = "IRechargeV2";
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void weidABICodeGen() throws IOException {
final String ABI_FILE = "Weid.abi";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ public void payableTestCodeGen() throws IOException {
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void RechargeTestCodeGen() throws IOException {
final String ABI_FILE = "IRechargeV2.abi";
final String CONTRACT_NAME = "IRechargeV2";
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void docTestCodeGen() throws IOException {
final String CONTRACT_NAME = "ERC721";
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/IRechargeV2.abi

Large diffs are not rendered by default.

0 comments on commit 3866d69

Please sign in to comment.