90
90
public class SolidityFunctionWrapper extends Generator {
91
91
92
92
private static final String BINARY = "BINARY" ;
93
+ private static final String LIBRARIES_LINKED_BINARY = "librariesLinkedBinary" ;
93
94
private static final String WEB3J = "web3j" ;
94
95
private static final String CREDENTIALS = "credentials" ;
95
96
private static final String CONTRACT_GAS_PROVIDER = "contractGasProvider" ;
@@ -262,6 +263,8 @@ public void generateJavaFiles(
262
263
buildLoad (className , TransactionManager .class , TRANSACTION_MANAGER , true ));
263
264
if (!bin .equals (Contract .BIN_NOT_PROVIDED )) {
264
265
classBuilder .addMethods (buildDeployMethods (className , classBuilder , abi ));
266
+ classBuilder .addMethod (buildLinkLibraryMethod ());
267
+ classBuilder .addMethod (buildGetDeploymentBinaryMethod ());
265
268
}
266
269
267
270
addAddressesSupport (classBuilder , addresses );
@@ -360,11 +363,17 @@ private TypeSpec.Builder createClassBuilder(
360
363
361
364
String javadoc = CODEGEN_WARNING + getWeb3jVersion ();
362
365
363
- return TypeSpec .classBuilder (className )
364
- .addModifiers (Modifier .PUBLIC )
365
- .addJavadoc (javadoc )
366
- .superclass (contractClass )
367
- .addField (createBinaryDefinition (binary ));
366
+ TypeSpec .Builder classBuilder =
367
+ TypeSpec .classBuilder (className )
368
+ .addModifiers (Modifier .PUBLIC )
369
+ .addJavadoc (javadoc )
370
+ .superclass (contractClass )
371
+ .addField (createBinaryDefinition (binary ));
372
+
373
+ if (!binary .equals (Contract .BIN_NOT_PROVIDED )) {
374
+ classBuilder .addField (createLibrariesLinkedBinaryField ());
375
+ }
376
+ return classBuilder ;
368
377
}
369
378
370
379
private String getWeb3jVersion () {
@@ -380,6 +389,12 @@ private String getWeb3jVersion() {
380
389
return "\n <p>Generated with web3j version " + version + ".\n " ;
381
390
}
382
391
392
+ private FieldSpec createLibrariesLinkedBinaryField () {
393
+ return FieldSpec .builder (String .class , LIBRARIES_LINKED_BINARY )
394
+ .addModifiers (Modifier .PRIVATE , Modifier .STATIC )
395
+ .build ();
396
+ }
397
+
383
398
private FieldSpec createBinaryDefinition (String binary ) {
384
399
if (binary .length () < 65534 ) {
385
400
return FieldSpec .builder (String .class , BINARY )
@@ -653,6 +668,26 @@ private Set<String> getDuplicateFunctionNames(List<AbiDefinition> functionDefini
653
668
return duplicateNames ;
654
669
}
655
670
671
+ private static MethodSpec buildGetDeploymentBinaryMethod () {
672
+ MethodSpec .Builder toReturn =
673
+ MethodSpec .methodBuilder ("getDeploymentBinary" )
674
+ .addModifiers (Modifier .PRIVATE , Modifier .STATIC )
675
+ .returns (ClassName .get (String .class ));
676
+
677
+ CodeBlock codeBlock =
678
+ CodeBlock .builder ()
679
+ .beginControlFlow ("if ($L != null)" , LIBRARIES_LINKED_BINARY )
680
+ .addStatement ("return $L" , LIBRARIES_LINKED_BINARY )
681
+ .nextControlFlow ("else" )
682
+ .addStatement ("return $L" , BINARY )
683
+ .endControlFlow ()
684
+ .build ();
685
+
686
+ toReturn .addCode (codeBlock );
687
+
688
+ return toReturn .build ();
689
+ }
690
+
656
691
List <MethodSpec > buildDeployMethods (
657
692
String className ,
658
693
TypeSpec .Builder classBuilder ,
@@ -849,43 +884,39 @@ private static MethodSpec buildDeployWithParams(
849
884
if (isPayable && !withGasProvider ) {
850
885
methodBuilder .addStatement (
851
886
"return deployRemoteCall("
852
- + "$L.class, $L, $L, $L, $L, $L , encodedConstructor, $L)" ,
887
+ + "$L.class, $L, $L, $L, $L, getDeploymentBinary() , encodedConstructor, $L)" ,
853
888
className ,
854
889
WEB3J ,
855
890
authName ,
856
891
GAS_PRICE ,
857
892
GAS_LIMIT ,
858
- BINARY ,
859
893
INITIAL_VALUE );
860
894
methodBuilder .addAnnotation (Deprecated .class );
861
895
} else if (isPayable && withGasProvider ) {
862
896
methodBuilder .addStatement (
863
897
"return deployRemoteCall("
864
- + "$L.class, $L, $L, $L, $L , encodedConstructor, $L)" ,
898
+ + "$L.class, $L, $L, $L, getDeploymentBinary() , encodedConstructor, $L)" ,
865
899
className ,
866
900
WEB3J ,
867
901
authName ,
868
902
CONTRACT_GAS_PROVIDER ,
869
- BINARY ,
870
903
INITIAL_VALUE );
871
904
} else if (!isPayable && !withGasProvider ) {
872
905
methodBuilder .addStatement (
873
- "return deployRemoteCall($L.class, $L, $L, $L, $L, $L , encodedConstructor)" ,
906
+ "return deployRemoteCall($L.class, $L, $L, $L, $L, getDeploymentBinary() , encodedConstructor)" ,
874
907
className ,
875
908
WEB3J ,
876
909
authName ,
877
910
GAS_PRICE ,
878
- GAS_LIMIT ,
879
- BINARY );
911
+ GAS_LIMIT );
880
912
methodBuilder .addAnnotation (Deprecated .class );
881
913
} else {
882
914
methodBuilder .addStatement (
883
- "return deployRemoteCall($L.class, $L, $L, $L, $L , encodedConstructor)" ,
915
+ "return deployRemoteCall($L.class, $L, $L, $L, getDeploymentBinary() , encodedConstructor)" ,
884
916
className ,
885
917
WEB3J ,
886
918
authName ,
887
- CONTRACT_GAS_PROVIDER ,
888
- BINARY );
919
+ CONTRACT_GAS_PROVIDER );
889
920
}
890
921
891
922
return methodBuilder .build ();
@@ -899,42 +930,38 @@ private static MethodSpec buildDeployNoParams(
899
930
boolean withGasProvider ) {
900
931
if (isPayable && !withGasProvider ) {
901
932
methodBuilder .addStatement (
902
- "return deployRemoteCall($L.class, $L, $L, $L, $L, $L , \" \" , $L)" ,
933
+ "return deployRemoteCall($L.class, $L, $L, $L, $L, getDeploymentBinary() , \" \" , $L)" ,
903
934
className ,
904
935
WEB3J ,
905
936
authName ,
906
937
GAS_PRICE ,
907
938
GAS_LIMIT ,
908
- BINARY ,
909
939
INITIAL_VALUE );
910
940
methodBuilder .addAnnotation (Deprecated .class );
911
941
} else if (isPayable && withGasProvider ) {
912
942
methodBuilder .addStatement (
913
- "return deployRemoteCall($L.class, $L, $L, $L, $L , \" \" , $L)" ,
943
+ "return deployRemoteCall($L.class, $L, $L, $L, getDeploymentBinary() , \" \" , $L)" ,
914
944
className ,
915
945
WEB3J ,
916
946
authName ,
917
947
CONTRACT_GAS_PROVIDER ,
918
- BINARY ,
919
948
INITIAL_VALUE );
920
949
} else if (!isPayable && !withGasProvider ) {
921
950
methodBuilder .addStatement (
922
- "return deployRemoteCall($L.class, $L, $L, $L, $L, $L , \" \" )" ,
951
+ "return deployRemoteCall($L.class, $L, $L, $L, $L, getDeploymentBinary() , \" \" )" ,
923
952
className ,
924
953
WEB3J ,
925
954
authName ,
926
955
GAS_PRICE ,
927
- GAS_LIMIT ,
928
- BINARY );
956
+ GAS_LIMIT );
929
957
methodBuilder .addAnnotation (Deprecated .class );
930
958
} else {
931
959
methodBuilder .addStatement (
932
- "return deployRemoteCall($L.class, $L, $L, $L, $L , \" \" )" ,
960
+ "return deployRemoteCall($L.class, $L, $L, $L, getDeploymentBinary() , \" \" )" ,
933
961
className ,
934
962
WEB3J ,
935
963
authName ,
936
- CONTRACT_GAS_PROVIDER ,
937
- BINARY );
964
+ CONTRACT_GAS_PROVIDER );
938
965
}
939
966
940
967
return methodBuilder .build ();
@@ -1456,6 +1483,25 @@ List<MethodSpec> buildFunctions(
1456
1483
return results ;
1457
1484
}
1458
1485
1486
+ MethodSpec buildLinkLibraryMethod () {
1487
+ MethodSpec .Builder methodBuilder =
1488
+ MethodSpec .methodBuilder ("linkLibraries" )
1489
+ .addModifiers (Modifier .PUBLIC , Modifier .STATIC )
1490
+ .addParameter (
1491
+ ParameterizedTypeName .get (
1492
+ ClassName .get (List .class ),
1493
+ ClassName .get (Contract .LinkReference .class )),
1494
+ "references" )
1495
+ .addStatement (
1496
+ LIBRARIES_LINKED_BINARY
1497
+ + " = "
1498
+ + "linkBinaryWithReferences("
1499
+ + BINARY
1500
+ + ", references)" );
1501
+
1502
+ return methodBuilder .build ();
1503
+ }
1504
+
1459
1505
private void buildConstantFunction (
1460
1506
AbiDefinition functionDefinition ,
1461
1507
MethodSpec .Builder methodBuilder ,
0 commit comments