Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enrich generateBothCallAndSend feature in TruffleJsonFunctionWrapperGenerator #1986

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
Expand All @@ -31,6 +32,7 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import picocli.CommandLine;

import org.web3j.abi.datatypes.Address;
import org.web3j.protocol.ObjectMapperFactory;
Expand All @@ -40,6 +42,15 @@

import static org.web3j.codegen.Console.exitError;
import static org.web3j.utils.Collection.tail;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.ExecutionException;
import static picocli.CommandLine.Help;
import static picocli.CommandLine.Help.Visibility.ALWAYS;
import static picocli.CommandLine.Option;
import static picocli.CommandLine.ParameterException;
import static picocli.CommandLine.Parameters;
import static picocli.CommandLine.ParseResult;
import static picocli.CommandLine.RunLast;

/**
* Java wrapper source code generator for Truffle JSON format. Truffle embeds the Solidity ABI
Expand All @@ -50,66 +61,107 @@
@SuppressWarnings("deprecation")
public class TruffleJsonFunctionWrapperGenerator extends FunctionWrapperGenerator {

private static final String USAGE =
"truffle generate "
+ "[--javaTypes|--solidityTypes] "
+ "<input truffle json file>.json "
+ "-p|--package <base package name> "
+ "-o|--outputDir <destination base directory>";
public static final String COMMAND_TRUFFLE = "truffle";
public static final String COMMAND_GENERATE = "generate";
public static final String COMMAND_PREFIX = COMMAND_TRUFFLE + " " + COMMAND_GENERATE;

private String jsonFileLocation;
private final String jsonFileLocation;
private final boolean generateBothCallAndSend;

public TruffleJsonFunctionWrapperGenerator(
String jsonFileLocation,
String destinationDirLocation,
String basePackageName,
boolean useJavaNativeTypes) {
boolean useJavaNativeTypes,
boolean generateBothCallAndSend) {

super(new File(destinationDirLocation), basePackageName, useJavaNativeTypes);
this.jsonFileLocation = jsonFileLocation;
this.generateBothCallAndSend = generateBothCallAndSend;
}

public static void run(String[] args) throws Exception {
if (args.length < 1 || !"generate".equals(args[0])) {
exitError(USAGE);
} else {
main(tail(args));
public static void main(String[] args) {
if (args.length > 0 && args[0].equals(COMMAND_TRUFFLE)) {
args = tail(args);

Check warning on line 85 in codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java

View check run for this annotation

Codecov / codecov/patch

codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java#L85

Added line #L85 was not covered by tests
}
}

public static void main(String[] args) throws Exception {

String[] fullArgs;
if (args.length == 5) {
fullArgs = new String[args.length + 1];
fullArgs[0] = JAVA_TYPES_ARG;
System.arraycopy(args, 0, fullArgs, 1, args.length);
} else {
fullArgs = args;
if (args.length > 0 && args[0].equals(COMMAND_GENERATE)) {
args = tail(args);

Check warning on line 88 in codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java

View check run for this annotation

Codecov / codecov/patch

codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java#L88

Added line #L88 was not covered by tests
}
CommandLine cmd = new CommandLine(new PicocliRunner());
cmd.parseWithHandlers(
new RunLast().useOut(System.out).useAnsi(Help.Ansi.AUTO),
RethrowExceptionHandler.HANDLER,
args);
}

if (fullArgs.length != 6) {
exitError(USAGE);
@Command(
name = COMMAND_PREFIX,
mixinStandardHelpOptions = true,
version = "4.0",
sortOptions = false)
private static class PicocliRunner implements Callable<Void> {

@Option(
names = {"-jt", JAVA_TYPES_ARG},
description = "use native Java types.",
showDefaultValue = ALWAYS)
boolean useJavaNativeTypes;

@Option(
names = {"-st", SOLIDITY_TYPES_ARG},
description = "use solidity types.")
boolean useSolidityTypes;

@Parameters(index = "0")
String jsonFileLocation;

@Option(
names = {"-o", "--outputDir"},
description = "destination base directory.",
required = true)
private String destinationDirLocation;

@Option(
names = {"-B", "--generateBoth"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this to generateCallAndSend with "-cs"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be better to be consistent with SolidityFunctionWrapperGenerator which use "-B", "--generateBoth" ?

https://github.com/web3j/web3j/blob/master/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapperGenerator.java#L263

description = "generate both call and send functions.",
required = false)
private boolean generateBothCallAndSend;

@Option(
names = {"-p", "--package"},
description = "base package name.",
required = true)
private String basePackageName;

@Override
public Void call() throws Exception {
useJavaNativeTypes = !(useSolidityTypes);
new TruffleJsonFunctionWrapperGenerator(
jsonFileLocation,
destinationDirLocation,
basePackageName,
useJavaNativeTypes,
generateBothCallAndSend)
.generate();
return null;
}
}

boolean useJavaNativeTypes = useJavaNativeTypes(fullArgs[0], USAGE);
public static class RethrowExceptionHandler
implements CommandLine.IExceptionHandler2<List<Object>> {

String jsonFileLocation = parsePositionalArg(fullArgs, 1);
String destinationDirLocation = parseParameterArgument(fullArgs, "-o", "--outputDir");
String basePackageName = parseParameterArgument(fullArgs, "-p", "--package");
public static final RethrowExceptionHandler HANDLER = new RethrowExceptionHandler();

if (Strings.isEmpty(jsonFileLocation)
|| Strings.isEmpty(destinationDirLocation)
|| Strings.isEmpty(basePackageName)) {
exitError(USAGE);
@Override
public List<Object> handleParseException(ParameterException ex, String[] args) {
throw ex;

Check warning on line 157 in codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java

View check run for this annotation

Codecov / codecov/patch

codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java#L157

Added line #L157 was not covered by tests
}

new TruffleJsonFunctionWrapperGenerator(
jsonFileLocation,
destinationDirLocation,
basePackageName,
useJavaNativeTypes)
.generate();
@Override
public List<Object> handleExecutionException(
ExecutionException ex, ParseResult parseResult) {
throw ex;

Check warning on line 163 in codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java

View check run for this annotation

Codecov / codecov/patch

codegen/src/main/java/org/web3j/codegen/TruffleJsonFunctionWrapperGenerator.java#L163

Added line #L163 was not covered by tests
}
}

static Contract loadContractDefinition(File jsonFile) throws IOException {
Expand Down Expand Up @@ -148,7 +200,8 @@
} else {
addresses = Collections.EMPTY_MAP;
}
new SolidityFunctionWrapper(useJavaNativeTypes, Address.DEFAULT_LENGTH)
new SolidityFunctionWrapper(
useJavaNativeTypes, Address.DEFAULT_LENGTH, generateBothCallAndSend)
.generateJavaFiles(
contractName,
c.getBytecode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,30 +57,41 @@ public void testContractGeneration() throws Exception {
private void testCodeGenerationJvmTypes(String contractName, String inputFileName)
throws Exception {

testCodeGeneration(contractName, inputFileName, PackageName, JAVA_TYPES_ARG);
testCodeGeneration(contractName, inputFileName, PackageName, JAVA_TYPES_ARG, false);
testCodeGeneration(contractName, inputFileName, PackageName, JAVA_TYPES_ARG, true);
}

@SuppressWarnings("SameParameterValue")
private void testCodeGenerationSolidtyTypes(String contractName, String inputFileName)
throws Exception {

testCodeGeneration(contractName, inputFileName, PackageName, SOLIDITY_TYPES_ARG);
testCodeGeneration(contractName, inputFileName, PackageName, SOLIDITY_TYPES_ARG, false);
testCodeGeneration(contractName, inputFileName, PackageName, SOLIDITY_TYPES_ARG, true);
}

private void testCodeGeneration(
String contractName, String inputFileName, String packageName, String types)
String contractName,
String inputFileName,
String packageName,
String types,
boolean generateBothCallAndSend)
throws Exception {

TruffleJsonFunctionWrapperGenerator.main(
Arrays.asList(
List<String> argList =
new ArrayList<>(
Arrays.asList(
types,
ContractJsonParseTest.jsonFileLocation(
contractBaseDir, contractName, inputFileName),
"-p",
packageName,
"-o",
tempDirPath)
.toArray(new String[0]));
tempDirPath));

if (generateBothCallAndSend) {
argList.add("-B");
}
TruffleJsonFunctionWrapperGenerator.main(argList.toArray(new String[0]));

GeneraterTestUtils.verifyGeneratedCode(
tempDirPath
Expand Down
Loading