Skip to content

Commit 4f38cea

Browse files
authored
Merge pull request #2025 from web3j/testWrappersFix
Fix for test wrappers generation
2 parents 16b133b + 141f2aa commit 4f38cea

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
77

88
### Bug Fixes
99

10-
*
10+
* Fix for test wrappers generation [#2025](https://github.com/web3j/web3j/pull/2025)
1111

1212
### Features
1313

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ private Set<String> getDuplicateFunctionNames(List<AbiDefinition> functionDefini
672672
private static MethodSpec buildGetDeploymentBinaryMethod() {
673673
MethodSpec.Builder toReturn =
674674
MethodSpec.methodBuilder("getDeploymentBinary")
675-
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
675+
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
676676
.returns(ClassName.get(String.class));
677677

678678
CodeBlock codeBlock =

codegen/src/main/java/org/web3j/codegen/unit/gen/MethodFilter.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.web3j.codegen.unit.gen;
1414

1515
import java.lang.reflect.Method;
16+
import java.lang.reflect.Modifier;
1617
import java.util.ArrayList;
1718
import java.util.Arrays;
1819
import java.util.HashMap;
@@ -61,9 +62,11 @@ public static List<MethodSpec> generateMethodSpecsForEachTest(Class theContract)
6162
.forEach(
6263
method -> {
6364
String uniqueName = getUniqueName(method, methodNameCountMap);
64-
listOfMethodSpecs.add(
65-
new MethodParser(method, theContract, uniqueName)
66-
.getMethodSpec());
65+
if (!Modifier.isPrivate(method.getModifiers())) {
66+
listOfMethodSpecs.add(
67+
new MethodParser(method, theContract, uniqueName)
68+
.getMethodSpec());
69+
}
6770
});
6871

6972
return listOfMethodSpecs;
@@ -76,8 +79,11 @@ public static List<FunSpec> generateFunctionSpecsForEachTest(Class theContract)
7679
.forEach(
7780
method -> {
7881
String uniqueName = getUniqueName(method, functionNameCountMap);
79-
listOfFunSpecs.add(
80-
new FunParser(method, theContract, uniqueName).getFunSpec());
82+
if (!Modifier.isPrivate(method.getModifiers())) {
83+
listOfFunSpecs.add(
84+
new FunParser(method, theContract, uniqueName)
85+
.getFunSpec());
86+
}
8187
});
8288

8389
return listOfFunSpecs;

codegen/src/test/java/org/web3j/codegen/unit/gen/java/MethodParserTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,22 @@ public void testGeneratedDuplicateGreetingMethods() {
7373
greetMethodSpecs.stream().anyMatch(methodSpec -> methodSpec.name.equals("greet1")));
7474
assertEquals(2, greetMethodSpecs.size());
7575
}
76+
77+
@Test
78+
public void testGetDeploymentBinaryMethodNotGenerated() {
79+
List<MethodSpec> allMethodSpecs =
80+
MethodFilter.generateMethodSpecsForEachTest(greeterContractClass);
81+
82+
// Filter all MethodSpecs for those related to "getDeploymentBinary" method
83+
List<MethodSpec> getDeploymentBinaryMethodSpecs =
84+
allMethodSpecs.stream()
85+
.filter(methodSpec -> methodSpec.name.startsWith("getDeploymentBinary"))
86+
.collect(Collectors.toList());
87+
88+
// Ensure no MethodSpecs were generated for getDeploymentBinary method
89+
assertEquals(
90+
0,
91+
getDeploymentBinaryMethodSpecs.size(),
92+
"MethodSpec list should not contain getDeploymentBinary method");
93+
}
7694
}

codegen/src/test/java/org/web3j/codegen/unit/gen/kotlin/FunParserTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,22 @@ public void testGeneratedDuplicateGreetingMethods() {
7878
.anyMatch(methodSpec -> methodSpec.getName().equals("greet1")));
7979
assertEquals(2, greetFunSpecs.size());
8080
}
81+
82+
@Test
83+
public void testGetDeploymentBinaryMethodNotGenerated() {
84+
List<FunSpec> allMethodSpecs =
85+
MethodFilter.generateFunctionSpecsForEachTest(greeterContractClass);
86+
87+
// Filter all FunSpecs for those related to "getDeploymentBinary" method
88+
List<FunSpec> getDeploymentBinaryFunSpecs =
89+
allMethodSpecs.stream()
90+
.filter(funSpec -> funSpec.getName().startsWith("getDeploymentBinary"))
91+
.collect(Collectors.toList());
92+
93+
// Ensure no MethodSpecs were generated for getDeploymentBinary method
94+
assertEquals(
95+
0,
96+
getDeploymentBinaryFunSpecs.size(),
97+
"MethodSpec list should not contain getDeploymentBinary function");
98+
}
8199
}

codegen/src/test/resources/solidity/metacoin/build/java/MetaCoin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static void linkLibraries(List<Contract.LinkReference> references) {
166166
librariesLinkedBinary = linkBinaryWithReferences(BINARY, references);
167167
}
168168

169-
public static String getDeploymentBinary() {
169+
private static String getDeploymentBinary() {
170170
if (librariesLinkedBinary != null) {
171171
return librariesLinkedBinary;
172172
} else {

0 commit comments

Comments
 (0)