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;
	}
This commit is contained in:
Marius van der Wijden 2020-04-27 14:20:07 +02:00
parent a070e23178
commit 000c7d034b
3 changed files with 20 additions and 6 deletions

View file

@ -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]),

View file

@ -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)
}
}
}

View file

@ -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}}.