-
Notifications
You must be signed in to change notification settings - Fork 45
/
helper.go
163 lines (154 loc) · 3.23 KB
/
helper.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package jnigi
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
// AttemptToFindJVMLibPath tries to find the full path to the JVM shared library file
func AttemptToFindJVMLibPath() string {
// for these linux is the "default"
prefix := os.Getenv("JAVA_HOME")
if prefix == "" {
if runtime.GOOS == "windows" {
prefix = filepath.Join("c:", "Program Files", "Java", "jdk")
} else if runtime.GOOS == "darwin" {
prefix = "/Library/Java/Home"
} else {
prefix = "/usr/lib/jvm/default-java"
}
}
dirPath := prefix
if runtime.GOOS == "windows" {
dirPath = filepath.Join(dirPath, "bin", "server")
} else if runtime.GOOS == "darwin" {
dirPath = filepath.Join(dirPath, "lib", "server")
} else {
dirPath = filepath.Join(dirPath, "lib", "server")
}
var libPath string
if runtime.GOOS == "windows" {
libPath = filepath.Join(dirPath, "jvm.dll")
} else if runtime.GOOS == "darwin" {
libPath = filepath.Join(dirPath, "libjvm.dylib")
} else {
libPath = filepath.Join(dirPath, "libjvm.so")
}
return libPath
}
// there should be no cases where val underlying type is an int or []int
func assignDest(val interface{}, dest interface{}) error {
if dest == nil {
return nil
}
var assigned bool
switch v := val.(type) {
case bool:
if dv, ok := dest.(*bool); ok {
*dv = v
assigned = true
}
case byte:
if dv, ok := dest.(*byte); ok {
*dv = v
assigned = true
}
case uint16:
if dv, ok := dest.(*uint16); ok {
*dv = v
assigned = true
}
case int16:
if dv, ok := dest.(*int16); ok {
*dv = v
assigned = true
}
case int32:
if dv, ok := dest.(*int32); ok {
*dv = v
assigned = true
} else if dv, ok := dest.(*int); ok {
*dv = int(v)
assigned = true
}
case int64:
if dv, ok := dest.(*int64); ok {
*dv = v
assigned = true
}
case float32:
if dv, ok := dest.(*float32); ok {
*dv = v
assigned = true
}
case float64:
if dv, ok := dest.(*float64); ok {
*dv = v
assigned = true
}
case []bool:
if dv, ok := dest.(*[]bool); ok {
*dv = v
assigned = true
}
case []byte:
if dv, ok := dest.(*[]byte); ok {
*dv = v
assigned = true
}
case []uint16:
if dv, ok := dest.(*[]uint16); ok {
*dv = v
assigned = true
}
case []int16:
if dv, ok := dest.(*[]int16); ok {
*dv = v
assigned = true
}
case []int32:
if dv, ok := dest.(*[]int32); ok {
*dv = v
assigned = true
} else if dv, ok := dest.(*[]int); ok {
sliceAssigned := make([]int, len(v))
for i, e := range v {
sliceAssigned[i] = int(e)
}
*dv = sliceAssigned
assigned = true
}
case []int64:
if dv, ok := dest.(*[]int64); ok {
*dv = v
assigned = true
}
case []float32:
if dv, ok := dest.(*[]float32); ok {
*dv = v
assigned = true
}
case []float64:
if dv, ok := dest.(*[]float64); ok {
*dv = v
assigned = true
}
case *ObjectRef:
if dv, ok := dest.(*ObjectRef); ok {
*dv = *v
assigned = true
}
}
if !assigned {
return fmt.Errorf("JNIGI Error: expected dest argument to be %T (not %T) or nil", val, dest)
}
return nil
}
// returned int32 can be converted to jint
func assignJavaIntFromInt(v int) int32 {
v2 := int32(v)
if int(v2) != v {
fmt.Fprintf(os.Stderr, "JNIGI WARNING int -> Int conversion changed value")
}
return v2
}