Skip to content

Commit df66720

Browse files
Migrate the buildConstantFunction to generate kotlin code properly
Signed-off-by: angrezichatterbox <gouthammohanraj@gmail.com>
1 parent 1bd1e68 commit df66720

1 file changed

Lines changed: 18 additions & 25 deletions

File tree

codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,9 +1557,7 @@ private void buildConstantFunction(
15571557
String functionName = functionDefinition.getName();
15581558

15591559
if (outputParameterTypes.isEmpty()) {
1560-
methodBuilder.addStatement(
1561-
"throw new RuntimeException"
1562-
+ "(\"cannot call constant function with void return type\")");
1560+
methodBuilder.addStatement("throw RuntimeException(\"cannot call constant function with void return type\")")
15631561
} else if (outputParameterTypes.size() == 1) {
15641562

15651563
TypeName typeName = outputParameterTypes.get(0);
@@ -1579,19 +1577,15 @@ private void buildConstantFunction(
15791577
methodBuilder.returns(buildRemoteFunctionCall(nativeReturnTypeName));
15801578

15811579
methodBuilder.addStatement(
1582-
"final $T function = "
1583-
+ "new $T($N, \n$T.<$T>asList($L), "
1584-
+ "\n$T.<$T<?>>asList(new $T<$T>() {}))",
1585-
Function.class,
1580+
"val function = %T(%S, listOf<%T>($L), listOf(object : %T<%T>() {}))",
15861581
Function.class,
1587-
funcNameToConst(functionName, useUpperCase),
1588-
Arrays.class,
1582+
functionName,
15891583
Type.class,
15901584
inputParams,
1591-
Arrays.class,
1592-
TypeReference.class,
15931585
TypeReference.class,
1594-
typeName);
1586+
typeName
1587+
);
1588+
15951589

15961590
if (useNativeJavaTypes) {
15971591
if (nativeReturnTypeName.equals(LIST)) {
@@ -1601,22 +1595,21 @@ private void buildConstantFunction(
16011595

16021596
CodeBlock.Builder callCode = CodeBlock.builder();
16031597
callCode.addStatement(
1604-
"$T result = "
1605-
+ "($T) executeCallSingleValueReturn(function, $T.class)",
1606-
listType,
1607-
listType,
1608-
nativeReturnTypeName);
1598+
"val result = executeCallSingleValueReturn(function, %T::class.java) as %T",
1599+
nativeReturnTypeName,
1600+
listType);
16091601
callCode.addStatement("return convertToNative(result)");
16101602

1603+
TypeName callableInterface = ParameterizedTypeName.get(
1604+
ClassName.get("java.util.concurrent", "Callable"),
1605+
nativeReturnTypeName
1606+
);
1607+
16111608
TypeSpec callableType =
16121609
TypeSpec.anonymousClassBuilder()
1613-
.addSuperinterface(nativeReturnTypeName,
1614-
String.valueOf(ParameterizedTypeName.get(
1615-
ClassName.Companion.bestGuess("java.util.concurrent.Callable")
1616-
)))
1610+
.addSuperinterface(callableInterface)
16171611
.addFunction(
16181612
FunSpec.builder("call")
1619-
.addAnnotation(Override.class)
16201613
.addAnnotation(
16211614
AnnotationSpec.builder(
16221615
SuppressWarnings.class)
@@ -1625,19 +1618,19 @@ private void buildConstantFunction(
16251618
"$S",
16261619
"unchecked")
16271620
.build())
1628-
.addModifiers(KModifier.PUBLIC)
1621+
.addModifiers(KModifier.OVERRIDE)
16291622
.returns(nativeReturnTypeName)
16301623
.addCode(callCode.build())
16311624
.build())
16321625
.build();
16331626

16341627
methodBuilder.addStatement(
1635-
"return new $T(function,\n$L)",
1628+
"return $T(function,\n$L)",
16361629
buildRemoteFunctionCall(nativeReturnTypeName),
16371630
callableType);
16381631
} else {
16391632
methodBuilder.addStatement(
1640-
"return executeRemoteCallSingleValueReturn(function, $T.class)",
1633+
"return executeRemoteCallSingleValueReturn(function, %T::class.java)",
16411634
nativeReturnTypeName);
16421635
}
16431636
} else {

0 commit comments

Comments
 (0)