Skip to content

Commit

Permalink
Merge pull request #37 from kyonRay/main
Browse files Browse the repository at this point in the history
Release v1.5.0
  • Loading branch information
kyonRay authored Mar 25, 2024
2 parents 2279b84 + df1bdf0 commit e87fdb0
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 24 deletions.
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ext {
junitVersion = '4.13.2'
commonsLang3Version = '3.12.0'

javaSDKVersion3 = "3.6.0"
javaSDKVersion3 = "3.7.0-SNAPSHOT"
javaSDKVersion2 = "2.10.0"
slf4jVersion = "1.7.32"
}
Expand All @@ -46,22 +46,22 @@ sourceSets {
}
}

configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

// check.dependsOn integrationTest
// integrationTest.mustRunAfter test
allprojects {
group = 'org.fisco-bcos.code-generator'
version = '1.4.0'
version = '1.5.0-SNAPSHOT'
apply plugin: 'maven-publish'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'signing'

configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/fisco/bcos/codegen/CodeGenMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class CodeGenMain {

public enum TransactionVersion {
V0(0),
V1(1);
V1(1),

V2(2);

private final int v;

Expand Down
126 changes: 118 additions & 8 deletions src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void generateJavaFiles(
classBuilder.addMethod(buildGetBinaryMethod());
classBuilder.addMethod(buildGetABIMethod());
classBuilder.addMethod(buildConstructor());
if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) {
if (this.transactionVersion >= CodeGenMain.TransactionVersion.V1.getV()) {
classBuilder.addMethod(buildTransactionV1Constructor());
}

Expand All @@ -165,7 +165,7 @@ public void generateJavaFiles(
.collect(Collectors.toList()));
classBuilder.addMethods(this.buildFunctionDefinitions(classBuilder, abiDefinitions));
classBuilder.addMethod(buildLoad(className));
if (transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) {
if (transactionVersion >= CodeGenMain.TransactionVersion.V1.getV()) {
classBuilder.addMethod(buildDefaultLoad(className));
}
classBuilder.addMethods(this.buildDeployMethods(isWasm, className, abiDefinitions));
Expand Down Expand Up @@ -303,6 +303,11 @@ private List<MethodSpec> buildFunctionDefinitions(
methodSpecs.add(msCallback);
}
if (!functionDefinition.isConstant()) {
if (this.transactionVersion >= CodeGenMain.TransactionVersion.V2.getV()) {
MethodSpec methodSpec = buildContractWrapperReturn(functionDefinition);
methodSpecs.add(methodSpec);
}

MethodSpec msSeq = this.buildFunctionSignedTransaction(functionDefinition);
methodSpecs.add(msSeq);
MethodSpec msCallback = this.buildFunctionWithCallback(functionDefinition);
Expand Down Expand Up @@ -690,7 +695,7 @@ private MethodSpec buildConstructor() {
CONTRACT_ADDRESS,
CLIENT,
ContractWrapper.CREDENTIAL);
if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) {
if (this.transactionVersion >= CodeGenMain.TransactionVersion.V1.getV()) {
toReturn.addStatement(
"this.$N = new $T($N)",
ContractWrapper.TRANSACTION_MANAGER,
Expand All @@ -701,7 +706,7 @@ private MethodSpec buildConstructor() {
}

private MethodSpec buildTransactionV1Constructor() {
if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) {
if (this.transactionVersion >= CodeGenMain.TransactionVersion.V1.getV()) {
MethodSpec.Builder toReturn;
toReturn =
MethodSpec.constructorBuilder()
Expand Down Expand Up @@ -749,7 +754,7 @@ private MethodSpec buildDeployWithParams(
Arrays.class,
Type.class,
inputParams);
if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) {
if (this.transactionVersion >= CodeGenMain.TransactionVersion.V1.getV()) {
String format =
"$L contract = deploy($L.class, $L, $L, $L, $L, encodedConstructor, $L)";
if (isPayable) {
Expand Down Expand Up @@ -786,7 +791,7 @@ private MethodSpec buildDeployWithParams(

private MethodSpec buildDeployNoParams(
boolean isWasm, MethodSpec.Builder methodBuilder, String className, boolean isPayable) {
if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) {
if (this.transactionVersion >= CodeGenMain.TransactionVersion.V1.getV()) {
String format = "$L contract = deploy($L.class, $L, $L, $L, $L, null, $L)";
if (isPayable) {
format = "$L contract = deploy($L.class, $L, $L, $L, $L, null, $L, value)";
Expand Down Expand Up @@ -840,7 +845,7 @@ private static MethodSpec.Builder getDeployMethodSpec(

private MethodSpec buildLoad(String className) {
MethodSpec.Builder toReturn;
if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) {
if (this.transactionVersion >= CodeGenMain.TransactionVersion.V1.getV()) {
toReturn =
MethodSpec.methodBuilder("load")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
Expand Down Expand Up @@ -1217,7 +1222,7 @@ private MethodSpec buildFunction(ABIDefinition functionDefinition)

private boolean isPayable(ABIDefinition functionDefinition) {
return functionDefinition.isPayable()
&& this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV();
&& this.transactionVersion >= CodeGenMain.TransactionVersion.V1.getV();
}

private MethodSpec buildFunctionSignedTransaction(ABIDefinition functionDefinition)
Expand Down Expand Up @@ -1510,6 +1515,111 @@ private MethodSpec buildRawFunctionReturn(ABIDefinition functionDefinition)
return methodBuilder.build();
}

private MethodSpec buildContractWrapperReturn(ABIDefinition functionDefinition)
throws ClassNotFoundException {
String functionName = functionDefinition.getName();

if (!SourceVersion.isName(functionName)) {
functionName = "_" + functionName;
}

MethodSpec.Builder methodBuilder =
MethodSpec.methodBuilder(
"buildMethod"
+ org.apache.commons.lang3.StringUtils.capitalize(
functionName))
.addModifiers(Modifier.PUBLIC);

String inputParams = this.addParameters(methodBuilder, functionDefinition.getInputs());

List<TypeName> outputParameterTypes = buildTypeNames(functionDefinition.getOutputs());

if (outputParameterTypes.isEmpty()) {

String format =
"final $T function = "
+ "new $T($N, \n$T.<$T>asList($L), "
+ "\n$T.<$T<?>>asList())";
methodBuilder.addStatement(
format,
Function.class,
Function.class,
funcNameToConst(functionName),
Arrays.class,
Type.class,
inputParams,
Arrays.class,
TypeReference.class);
CodeBlock.Builder callCode = CodeBlock.builder();
callCode.addStatement("return new FunctionWrapper(this, function)");
methodBuilder
.returns(org.fisco.bcos.sdk.v3.contract.FunctionWrapper.class)
.addCode(callCode.build());

} else if (outputParameterTypes.size() == 1) {
TypeName typeName = outputParameterTypes.get(0);
TypeName nativeReturnTypeName;
ABIDefinition.NamedType outputType = functionDefinition.getOutputs().get(0);
if (outputType.getType().equals("tuple")) {
nativeReturnTypeName = structClassNameMap.get(outputType.structIdentifier());
} else if (outputType.getType().startsWith("tuple")
&& outputType.getType().contains("[")) {
TypeName argument =
((ParameterizedTypeName) buildStructArrayTypeName(outputType))
.typeArguments.get(0);
nativeReturnTypeName =
ParameterizedTypeName.get(
ClassName.get(List.class), ClassName.get("", argument.toString()));
} else {
nativeReturnTypeName = this.getWrapperRawType(typeName);
}

methodBuilder.returns(nativeReturnTypeName);

methodBuilder.addStatement(
"final $T function = "
+ "new $T($N, \n$T.<$T>asList($L), "
+ "\n$T.<$T<?>>asList(new $T<$T>() {}))",
Function.class,
Function.class,
funcNameToConst(functionName),
Arrays.class,
Type.class,
inputParams,
Arrays.class,
TypeReference.class,
TypeReference.class,
typeName);
CodeBlock.Builder callCode = CodeBlock.builder();
callCode.addStatement("return new FunctionWrapper(this, function)");

methodBuilder
.returns(org.fisco.bcos.sdk.v3.contract.FunctionWrapper.class)
.addCode(callCode.build());
} else {
List<TypeName> returnTypes = new ArrayList<>();
for (int i = 0; i < functionDefinition.getOutputs().size(); ++i) {
ABIDefinition.NamedType outputType = functionDefinition.getOutputs().get(i);
if (outputType.getType().equals("tuple")) {
returnTypes.add(structClassNameMap.get(outputType.structIdentifier()));
} else if (outputType.getType().startsWith("tuple")
&& outputType.getType().contains("[")) {
returnTypes.add(buildStructArrayTypeName(outputType));
} else {
returnTypes.add(getNativeType(outputParameterTypes.get(i)));
}
}
buildVariableLengthReturnFunctionConstructor(
methodBuilder, functionName, inputParams, outputParameterTypes);
CodeBlock.Builder callCode = CodeBlock.builder();
callCode.addStatement("return new FunctionWrapper(this, function)");
methodBuilder
.returns(org.fisco.bcos.sdk.v3.contract.FunctionWrapper.class)
.addCode(callCode.build());
}
return methodBuilder.build();
}

private void buildConstantFunction(
ABIDefinition functionDefinition,
MethodSpec.Builder methodBuilder,
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
@RunWith(Parameterized.class)
public class CodeGenV3Test {

@Parameterized.Parameter public boolean useV1Tx;
@Parameterized.Parameter public String txVersion;

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][] {
{true}, {false},
});
return Arrays.asList(new Object[][] {{"V2"}, {"V1"}, {"V0"}});
}

private static final String JAVA_OUTPUT_DIR = "sdk";
Expand Down Expand Up @@ -195,10 +192,8 @@ private void codeGenTest(String abiFileName, String codeFilePath, String contrac
javaOutPut,
"-e"));

if (useV1Tx) {
args.add("-t");
args.add("V1");
}
args.add("-t");
args.add(txVersion);
CodeGenMain.main(args.toArray(new String[0]));
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
Expand Down

0 comments on commit e87fdb0

Please sign in to comment.