From 000c7d034b377552bab0c221b1a85080b8b16e72 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Mon, 27 Apr 2020 14:20:07 +0200 Subject: [PATCH] accounts/abi/bind: generate java structs This commit enables struct generation for contracts on java. It will generate for the TUPLE contract the following: // TupleP is an auto generated low-level Java binding around an user-defined struct. public class TupleP { public BigInt x; public BigInt y; } // TupleQ is an auto generated low-level Java binding around an user-defined struct. public class TupleQ { public BigInt x; public BigInt y; } // TupleS is an auto generated low-level Java binding around an user-defined struct. public class TupleS { public BigInt a; public BigInts b; public TupleT[] c; } // TupleT is an auto generated low-level Java binding around an user-defined struct. public class TupleT { public BigInt x; public BigInt y; } // Func1Results is the output of a call to func1. public class Func1Results { public TupleS Return0; public TupleT[][] Return1; public TupleT[][] Return2; public TupleS[] Return3; public BigInts Return4; } --- accounts/abi/bind/bind.go | 5 ----- accounts/abi/bind/bind_test.go | 13 ++++++++++++- accounts/abi/bind/template.go | 8 ++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index 4c6a9e9ce1..5ba6681d6c 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -22,7 +22,6 @@ package bind import ( "bytes" - "errors" "fmt" "go/format" "regexp" @@ -165,10 +164,6 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string] if evmABI.HasReceive() { receive = &tmplMethod{Original: evmABI.Receive} } - // There is no easy way to pass arbitrary java objects to the Go side. - if len(structs) > 0 && lang == LangJava { - return "", errors.New("java binding for tuple arguments is not supported yet") - } contracts[types[i]] = &tmplContract{ Type: capitalise(types[i]), diff --git a/accounts/abi/bind/bind_test.go b/accounts/abi/bind/bind_test.go index a5f08499d2..fbacb08f87 100644 --- a/accounts/abi/bind/bind_test.go +++ b/accounts/abi/bind/bind_test.go @@ -2150,7 +2150,7 @@ public class Test { for _, line := range lines { if strings.TrimSpace(line) != "" { lines[index] = line - index += 1 + index++ } } lines = lines[:index] @@ -2163,3 +2163,14 @@ public class Test { } } } + +// Tests binding the bindTests succeeds (does not execute test cases, only checks binding errors) +func TestGenerateBindingingJava(t *testing.T) { + // Generate the test suite for all the contracts + for _, tt := range bindTests { + _, err := Bind([]string{tt.name}, tt.abi, tt.bytecode, nil, "bindtest", LangJava, nil, tt.aliases) + if err != nil { + t.Fatalf("test %s: failed to generate binding: %v", tt.name, err) + } + } +} diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go index c361a49f84..b13d33d3e8 100644 --- a/accounts/abi/bind/template.go +++ b/accounts/abi/bind/template.go @@ -613,6 +613,14 @@ import java.util.*; this(Geth.bindContract(address, ABI, client)); } + {{range $structs}} + // {{capitalise .Name}} is an auto generated low-level Java binding around an user-defined struct. + public class {{capitalise .Name}} { + {{range $index, $item := .Fields}}public {{$item.Type}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}}; + {{end}} + } + {{end}} + {{range .Calls}} {{if gt (len .Normalized.Outputs) 1}} // {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}.