-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_ref.go
More file actions
37 lines (31 loc) · 1.25 KB
/
object_ref.go
File metadata and controls
37 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package jni
import (
"unsafe"
"github.com/AndroidGoLab/jni/capi"
)
// CAPIObject is an alias for capi.Object (C.jobject), exported so that
// packages outside capi can reference the type for unsafe conversions
// between different CGO compilation units.
type CAPIObject = capi.Object
// ObjectFromRef wraps a raw JNI jobject reference in an Object.
// The caller is responsible for ensuring the reference is valid.
func ObjectFromRef(ref capi.Object) *Object {
return &Object{ref: ref}
}
// ObjectFromPtr wraps a raw C jobject pointer in an Object, mirroring
// VMFromPtr. Prefer ObjectFromUintptr for NativeActivity callbacks to
// avoid go vet "possible misuse of unsafe.Pointer" warnings:
//
// jni.ObjectFromUintptr(uintptr(activity.clazz))
func ObjectFromPtr(ptr unsafe.Pointer) *Object {
return &Object{ref: capi.Object(uintptr(ptr))}
}
// ObjectFromUintptr wraps a uintptr-encoded jobject in an Object.
// This is the convention used by gomobile (RunOnJVM passes context
// as uintptr) and other Go Android frameworks.
//
// unsafe.Add(nil, ptr) is used instead of unsafe.Pointer(ptr) to avoid
// a go vet "possible misuse of unsafe.Pointer" false positive.
func ObjectFromUintptr(ptr uintptr) *Object {
return ObjectFromPtr(unsafe.Add(unsafe.Pointer(nil), ptr))
}