-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: expose Java API to gomobile bind programs #16876
Comments
CL https://golang.org/cl/27751 mentions this issue. |
I have no opinion on this directly. But if this is considered, would it be possible to use some sort of prefix char to denote this package is provided by the build tool. Rational: currently package tools need to understand special packages like "C". While it wouldn't be the end of the world to add other common special cases (starts with "Java/" or "objc/") it might be worth adding a convention for them. If this was generalized:
|
@kardianos I would prefer the current naming scheme similar to Cgo's import "C". Adding the * special case for just gomobile seems like overkill. Besides, I wouldn't be surprised if the Java/* packages were someday pre-generated and thus accessible to tools as any other Go package. (also, the "appengine" import is a misleading example, since it is being replaced by the go gettable "google.golang.org/appengine..." family of packages) |
@eliasnaur That's fine. It is just another thing tools that deal with package tools will need to special case. If they are pre-generated, they would have full normal go import paths discoverable on disk. If you noticed the final example, you could also use for current appengine and C imports as well. |
I really like this idea not only for mobile dev, the proposal should also specify how to vendor java dependencies |
The proposal doesn't address one of the toughest problems that also arise in go->c++ interop, one of the reasons why libs are rewritten in pure Go: when a goroutine calls a blocking Java API, should entire OS thread be occupied by Java Run-time and just sit idle while it could be used by other goroutines? |
@nodirt If it is possible to rewrite in pure Go, go ahead! This proposal is for accessing (Android) API that is otherwise not available to Go. Can you think of an Android API that takes up a significant amount of threads? Even if you can, I don't see how it is possible to avoid the problem from Go. One thread per blocking call is the execution model of Java. |
I'm not suggesting to rewrite anything. It is not about API that takes up more than one thread, but about blocking calls. For example, imagine a goroutine calls FileOutputStream.write to write a file locally (it may be a bad example, I'm not familiar with Android API. You can probably come up with a better example); this is a blocking call. In a typical go app a goroutine waiting for a blocking call doesn't hold a thread, but let's other goroutines use it. Java doesn't do that, obviously; an entire thread will be just waiting for the blocking call to return. If GOMAXPROCS=4 and 4 goroutines made blocking calls into Java API, all threads may be waiting for the blocking calls to return and program may freeze. Maybe you have a solution to this problem, but it is not obvious from this proposal |
@nodirt As far as I know, Go programs never freezes or deadlocks because of blocking C call. Gomobile is implemented through Cgo, and Cgo assumes every C call might block, so it creates new threads as necessary for goroutines to run on. There is even an issue about optimizing the non-blocking case: #16051 |
You meant "...directly in Go", right? :-) |
@teknico Actually I didn't :) The point I'm trying to convey is that this proposal doesn't enable access to platform APIs (they're already indirectly accessible) as much as improve the convenience of access. |
For overloaded methods, I disagree with having two different methods for determining the name. The first method of appending number of arguments is simply deficient for handling a number of cases and should be discarded. I think the full bytecode name of the object appended is overkill but can't confirm otherwise. I think the likelihood of an overloaded method that has positional arguments with the same class name but different namespaces is simply nonexistent.
Still, that's short-sighted and I think it'd be worthwhile to list out all the overloaded methods from android.jar to see what kind of cases can be expected instead of assuming worse-case with use of bytecode names. |
I printed out a list of overloaded methods under
Two of the methods contain the same number of arguments (13) making the the first proposed rule for naming ineffective and leaving an unusually long name for the first method listed. |
I believe multiple methods are necessary for readability. Only the last rule has to cover everything, and then it might as well be the JNI rules. For JNI methods in C, there in a sense already two methods: (1) Use the method name if it is not overloaded (2) If it is overloaded, mangle the argument types to disambiguate. From that view, I'm merely introducing a second, much more readable, fallback before the final catch-all rule. |
CL https://golang.org/cl/28596 mentions this issue. |
CL https://golang.org/cl/28597 mentions this issue. |
CL https://golang.org/cl/28595 mentions this issue. |
One thing I don't see this proposal discussing is why this needs to be a language extension / cannot be done as a standard Go package (cgo bindings to the JNI). |
It's all implemented in golang.org/x/mobile. Nothing changes in Go itself. |
I've updated the implementation and proposal to replace the crude Go import scanning with a more sophisticated method of determining what Java types and identifiers are used. |
CL https://golang.org/cl/29172 mentions this issue. |
sgtm, it does at least afford a basis to build upon. |
The importers package adds functions to traverse the AST of a Go file or set of packages and extract references to Java packages and types. The java package adds a parser that uses the javap command to extract type information about Java classes and interfaces. The resulting type information is needed to generate Java API wrappers in Go. This is the first part of the implementation of proposal golang/go#16876. For golang/go#16876 Change-Id: Ic844472a1101354d61401d9e8c120acdee2519df Reviewed-on: https://go-review.googlesource.com/28595 Reviewed-by: David Crawshaw <crawshaw@golang.org>
Using the new Java class analyzer API, scan the bound packages for references to Java classes and interfaces and generate Go wrappers for them. This is the second part of the implementation of proposal golang/go#16876. For golang/go#16876 Change-Id: I59ec0ebdae0081a615dc34d450f344c20c03f871 Reviewed-on: https://go-review.googlesource.com/28596 Reviewed-by: David Crawshaw <crawshaw@golang.org>
Sorry. but I just want to know how to get a public attribute in a java class ? It just cause this error. but when I call a function of the PackageInfo it can compile correctly.
|
I'm sorry, but Java fields are not (yet) exposed by reverse bindings. |
Sorry, I want to pass a list(for example: []string) from go to java. How can I do that ? |
Go Mobile only supports []byte at the moment, sorry.
Den søn. 7. maj 2017 05.43 skrev cable309 <notifications@github.com>:
… Sorry, I want to pass a list(for example: []string) from go to java. How
can I do that ?
type Printer interface { Print(s []string) Lost(s string) }
func Get() *[]string{ return &[]string{} }
the code above will not do this.
@eliasnaur <https://github.com/eliasnaur>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#16876 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAgCDB5EBEwk0f87Vddv4sh3NtvneNW3ks5r3T3IgaJpZM4JtBeP>
.
|
In project : golang.org/x/mobile/example/reverse
In reverse.go
when I build , gradle assembleDebug It just cause this error
I need your help! @eliasnaur |
Has reverse bindings supported Java public fields now? Thanks. @eliasnaur |
Accept Java API interface types as arguments and return values from bound Go package functions and methods. Also, allow Go structs to extend Java classes and implement Java interfaces as well as override and implement methods. This is the third and final part of the implementation of the golang/go#16876 proposal. Fixes golang/go#16876 Change-Id: I6951dd87235553ce09abe5117a39a503466163c0 Reviewed-on: https://go-review.googlesource.com/28597 Reviewed-by: David Crawshaw <crawshaw@golang.org>
Accept Java API interface types as arguments and return values from bound Go package functions and methods. Also, allow Go structs to extend Java classes and implement Java interfaces as well as override and implement methods. This is the third and final part of the implementation of the golang/go#16876 proposal. Fixes golang/go#16876 Change-Id: I6951dd87235553ce09abe5117a39a503466163c0 Reviewed-on: https://go-review.googlesource.com/28597 Reviewed-by: David Crawshaw <crawshaw@golang.org>
Abstract
Today, gomobile bind can take a set og Go packages and expose their public API to Java or ObjC apps. The proposal is to support the reverse, exposing Java API to the bound Go packages.
Motivation
Even though mobile apps can access Go code already, there are still large parts of a typical app that is impossible or awkward to implement in Go. The most notable case is UI code, which interacts most with the platform APIs.
Platform APIs can be accessed from Go in an indirect way already. Creating a Java (or ObjC) class wrapping the desired API and passing it to Go does work. However, this is not nearly as convenient as writing the code directly in Java.
To improve support for writing platform specific code in Go, direct access to the platform is needed.
Proposed features
Importing Java classes and interfaces from Go
The Go wrappers for all Java API are generated each time gomobile bind is called. To access a Java package, use import statements on the form:
To access the static methods or constants on a Java class or interface, use
or
for an inner class.
Static methods and constants
After importing, the resulting packages SomeClass and InnerClass will contain the static methods and static final constants from their Java classes. For example
will expose (among others) the constant Float.MIN_VALUE and the function Float.ParseFloat.
Java classes and interfaces
The package
"Java/some/pkg"
contains Go interfaces wrapping every referenced Java type insome.pkg
. The wrapper types are used to represent their wrapped Java types across the language barrier and to call methods on wrapped instances. For example, with the following Go function is now possible:Creating new Java instances
To create a new instance of a Java class, use the
New
function defined in the class package. For example:Errors and exceptions
Exceptions are normally translated to explicit Go errors, but since we don't control the platform API, we don't know which Java methods can result in an exception worth catching. Instead, a simple heuristic is used: If a Java method is declared to throw one or more exceptions, its Go function or method will return an error. If no exception is declared, any exception thrown will be converted to a panic with the exception as argument.
In addition, any Java class which inherits from
java.lang.Throwable
will satisfy theerror
interface. ItsError
method will delegate to the toString() method.Extending or implementing Java types from Go
Gomobile already exposes exported Go structs to Java; this proposal adds support for constructing Go structs directly from Java. In addition, Go structs will be able to extend Java classes and implement Java interfaces.
To declare a Go struct that extends or implements Java types, use the form:
Java constructors
To allow Java to create instances of a Go struct, S, add one or more constructor on the form:
For each such Go constructor a Java constructor will be added taking the same arguments. The Java constructor calls its super constructor with its arguments before calling calling NewS. For example:
will allow Java to construct instances of GoObject:
Overriding Java methods
To implement or override a method from a super class or interface, declare a Go method with the same name and its first letter capitalized. For example, to override the toString method in GoObject:
Exposing
this
Whenever an foreign object is passed across the language barrier, a proxy is created to represent it. In the example above, there is a GoObject Java instance created in Java, and it contains a reference to its counterpart GoObject Go instance in Go. That means that when a Go method is called from Java, its method receiver contains the Go instance, while the Java instance is only accessible to Java.
To access the Java instance (for passing back to other Java APIs), any Go method can declare a
this
argument with one of the Java types the enclosing class extends or implements. For example, to access thethis
from the ToString method, use:The
t
variable will behave just as if it were a pure Java Object, and if passed to Java, it will have the same identity as the Java reference.Calling
super
In Go, delegation is achieved through delegation, but in Java, the keyword
super
is needed to access overridden methods. To call a super method from Go, use the Super() method on thethis
variable:Overloaded methods and constructors
Java supports overloading; Go doesn't. To access or override overloaded methods and cosntructors from Go, a mangling scheme is used:
are called
M
andM2
, respectively, in Go.are called
M_I
andM_Ljava_lang_String_2
in Go.The JNI name mangling scheme is ugly. In particular, Java constructors are only distinguished by their arguments and are therefore often mangled. Suggestions for improved schemes are most welcome!
The text was updated successfully, but these errors were encountered: