Skip to content

Commit 3a48760

Browse files
committed
Explicit types + ClosureRunnable
1 parent 77d016c commit 3a48760

File tree

5 files changed

+105
-90
lines changed

5 files changed

+105
-90
lines changed

Sources/JNICore.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Dispatch
1717
@_silgen_name("JNI_OnLoad")
1818
public func JNI_OnLoad( jvm: UnsafeMutablePointer<JavaVM?>, ptr: UnsafeRawPointer ) -> jint {
1919
JNI.jvm = jvm
20-
let env = JNI.GetEnv()
20+
let env: UnsafeMutablePointer<JNIEnv?>? = JNI.GetEnv()
2121
JNI.api = env!.pointee!.pointee
2222
JNI.envCache[JNI.threadKey] = env
2323
#if os(Android)
@@ -84,10 +84,10 @@ open class JNICore {
8484
return true
8585
}
8686

87-
var options = options
87+
var options: [String]? = options
8888
if options == nil {
89-
var classpath = String( cString: getenv("HOME") )+"/.swiftjava.jar"
90-
if let CLASSPATH = getenv( "CLASSPATH" ) {
89+
var classpath: String = String( cString: getenv("HOME") )+"/.swiftjava.jar"
90+
if let CLASSPATH: UnsafeMutablePointer<Int8> = getenv( "CLASSPATH" ) {
9191
classpath += ":"+String( cString: CLASSPATH )
9292
}
9393
options = ["-Djava.class.path="+classpath,
@@ -105,7 +105,7 @@ open class JNICore {
105105
vmArgs.nOptions = jint(options?.count ?? 0)
106106
vmArgs.options = vmOptionsPtr
107107

108-
if let options = options {
108+
if let options: [String] = options {
109109
for i in 0..<options.count {
110110
options[i].withCString {
111111
(cString) in
@@ -169,7 +169,7 @@ open class JNICore {
169169
open func FindClass( _ name: UnsafePointer<Int8>, _ file: StaticString = #file, _ line: Int = #line ) -> jclass? {
170170
autoInit()
171171
ExceptionReset()
172-
let clazz = api.FindClass( env, name )
172+
let clazz: jclass? = api.FindClass( env, name )
173173
if clazz == nil {
174174
report( "Could not find class \(String( cString: name ))", file, line )
175175
if strncmp( name, "org/swiftjava/", 14 ) == 0 {
@@ -183,7 +183,7 @@ open class JNICore {
183183

184184
open func CachedFindClass( _ name: UnsafePointer<Int8>, _ classCache: UnsafeMutablePointer<jclass?>,
185185
_ file: StaticString = #file, _ line: Int = #line ) {
186-
if classCache.pointee == nil, let clazz = FindClass( name, file, line ) {
186+
if classCache.pointee == nil, let clazz: jclass = FindClass( name, file, line ) {
187187
classCache.pointee = api.NewGlobalRef( env, clazz )
188188
api.DeleteLocalRef( env, clazz )
189189
}
@@ -195,7 +195,7 @@ open class JNICore {
195195
if object == nil {
196196
report( "GetObjectClass with nil object", file, line )
197197
}
198-
let clazz = api.GetObjectClass( env, object )
198+
let clazz: jclass? = api.GetObjectClass( env, object )
199199
if clazz == nil {
200200
report( "GetObjectClass returns nil class", file, line )
201201
}
@@ -207,9 +207,9 @@ open class JNICore {
207207

208208
private static var java_lang_ObjectClass: jclass?
209209

210-
open func NewObjectArray( _ count: Int, _ array: [jobject?]?, _ locals: UnsafeMutablePointer<[jobject]>, _ file: StaticString = #file, _ line: Int = #line ) -> jobject? {
210+
open func NewObjectArray( _ count: Int, _ array: [jobject?]?, _ locals: UnsafeMutablePointer<[jobject]>, _ file: StaticString = #file, _ line: Int = #line ) -> jobjectArray? {
211211
CachedFindClass( "java/lang/Object", &JNICore.java_lang_ObjectClass, file, line )
212-
var arrayClass = JNICore.java_lang_ObjectClass
212+
var arrayClass: jclass? = JNICore.java_lang_ObjectClass
213213
if array?.count != 0 {
214214
arrayClass = JNI.GetObjectClass(array![0], locals)
215215
}
@@ -218,7 +218,7 @@ open class JNICore {
218218
return nil
219219
#endif
220220
}
221-
let array = api.NewObjectArray( env, jsize(count), arrayClass, nil )
221+
let array: jobjectArray? = api.NewObjectArray( env, jsize(count), arrayClass, nil )
222222
if array == nil {
223223
report( "Could not create array", file, line )
224224
}
@@ -231,7 +231,7 @@ open class JNICore {
231231
}
232232
}
233233

234-
private var thrownCache = [pthread_t:jthrowable]()
234+
private var thrownCache = [pthread_t: jthrowable]()
235235
private let thrownLock = NSLock()
236236

237237
open func check<T>( _ result: T, _ locals: UnsafeMutablePointer<[jobject]>, removeLast: Bool = false, _ file: StaticString = #file, _ line: Int = #line ) -> T {
@@ -241,7 +241,7 @@ open class JNICore {
241241
for local in locals.pointee {
242242
DeleteLocalRef( local )
243243
}
244-
if api.ExceptionCheck( env ) != 0, let throwable = api.ExceptionOccurred( env ) {
244+
if api.ExceptionCheck( env ) != 0, let throwable: jthrowable = api.ExceptionOccurred( env ) {
245245
report( "Exception occured", file, line )
246246
thrownLock.lock()
247247
thrownCache[threadKey] = throwable
@@ -252,8 +252,8 @@ open class JNICore {
252252
}
253253

254254
open func ExceptionCheck() -> jthrowable? {
255-
let currentThread = threadKey
256-
if let throwable = thrownCache[currentThread] {
255+
let currentThread: pthread_t = threadKey
256+
if let throwable: jthrowable = thrownCache[currentThread] {
257257
thrownLock.lock()
258258
thrownCache.removeValue(forKey: currentThread)
259259
thrownLock.unlock()
@@ -263,7 +263,7 @@ open class JNICore {
263263
}
264264

265265
open func ExceptionReset() {
266-
if let throwable = ExceptionCheck() {
266+
if let throwable: jthrowable = ExceptionCheck() {
267267
report( "Left over exception" )
268268
Throwable( javaObject: throwable ).printStackTrace()
269269
}

Sources/JNIMethod.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class JNIMethod {
2525
_ methodCache: UnsafeMutablePointer<jmethodID?>, _ object: jobject?,
2626
_ locals: UnsafeMutablePointer<[jobject]>,
2727
_ file: StaticString = #file, _ line: Int = #line ) {
28-
let clazz = JNI.GetObjectClass( object, locals, file, line )
28+
let clazz: jclass? = JNI.GetObjectClass( object, locals, file, line )
2929
methodCache.pointee = JNI.api.GetMethodID( JNI.env, clazz, methodName, methodSig )
3030
if methodCache.pointee == nil {
3131
JNI.report( "Failed to lookup method \(String(describing: object)).\(String(describing: clazz)).\(String(cString: methodName))( \(String(cString: methodSig)) )", file, line )

Sources/JNIObject.swift

+26-26
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension JNIObjectProtocol {
2828

2929
public func withJavaObject<Result>( _ body: @escaping (jobject?) throws -> Result ) rethrows -> Result {
3030
var locals = [jobject]()
31-
let javaObject = localJavaObject( &locals )
31+
let javaObject: jobject? = localJavaObject( &locals )
3232
defer {
3333
for local in locals {
3434
JNI.DeleteLocalRef( local )
@@ -66,7 +66,7 @@ open class JNIObject: JNIObjectProtocol, JNIObjectInit {
6666
}
6767
set(newValue) {
6868
if newValue != _javaObject {
69-
let oldValue = _javaObject
69+
let oldValue: jobject? = _javaObject
7070
if newValue != nil {
7171
_javaObject = JNI.api.NewGlobalRef( JNI.env, newValue )
7272
}
@@ -93,7 +93,7 @@ open class JNIObject: JNIObjectProtocol, JNIObjectInit {
9393
}
9494

9595
open func localJavaObject( _ locals: UnsafeMutablePointer<[jobject]> ) -> jobject? {
96-
if let local = _javaObject != nil ? JNI.api.NewLocalRef( JNI.env, _javaObject ) : nil {
96+
if let local: jobject = _javaObject != nil ? JNI.api.NewLocalRef( JNI.env, _javaObject ) : nil {
9797
locals.pointee.append( local )
9898
return local
9999
}
@@ -114,7 +114,7 @@ open class JNIObjectForward: JNIObject {
114114
extension String: JNIObjectProtocol {
115115

116116
public func localJavaObject( _ locals: UnsafeMutablePointer<[jobject]> ) -> jobject? {
117-
if let javaObject = Array(utf16).withUnsafeBufferPointer( {
117+
if let javaObject: jstring = Array(utf16).withUnsafeBufferPointer( {
118118
JNI.env?.pointee?.pointee.NewString( JNI.env, $0.baseAddress, jsize($0.count) )
119119
} ) {
120120
locals.pointee.append( javaObject )
@@ -128,7 +128,7 @@ extension String: JNIObjectInit {
128128

129129
public init( javaObject: jobject? ) {
130130
var isCopy: jboolean = 0
131-
if let javaObject = javaObject, let value = JNI.api.GetStringChars( JNI.env, javaObject, &isCopy ) {
131+
if let javaObject: jobject = javaObject, let value: UnsafePointer<jchar> = JNI.api.GetStringChars( JNI.env, javaObject, &isCopy ) {
132132
self.init( utf16CodeUnits: value, count: Int(JNI.api.GetStringLength( JNI.env, javaObject )) )
133133
if isCopy != 0 || true {
134134
JNI.api.ReleaseStringChars( JNI.env, javaObject, value ) ////
@@ -144,7 +144,7 @@ extension jobject {
144144

145145
public func arrayMap<T>( block: ( _ javaObject: jobject? ) -> T ) -> [T] {
146146
return (0 ..< JNI.api.GetArrayLength( JNI.env, self )).map {
147-
let element = JNI.api.GetObjectArrayElement( JNI.env, self, $0 )
147+
let element: jobject? = JNI.api.GetObjectArrayElement( JNI.env, self, $0 )
148148
defer { JNI.DeleteLocalRef( element ) }
149149
return block( element )
150150
}
@@ -167,15 +167,15 @@ extension JNIType {
167167
public static func toJavaArray<T>( value: [T]?, locals: UnsafeMutablePointer<[jobject]> ,
168168
block: (_ value: T, _ locals: UnsafeMutablePointer<[jobject]> ) -> jvalue ) -> jvalue {
169169
var array: jarray?
170-
if let value = value {
170+
if let value: [T] = value {
171171
for i in 0 ..< value.count {
172172
var sublocals = [jobject]()
173-
let element = block( value[i], &sublocals ).l
173+
let element: jobject? = block( value[i], &sublocals ).l
174174
if array == nil {
175175
if element == nil {
176176
break
177177
}
178-
let elementClass = JNI.GetObjectClass( element, &sublocals )
178+
let elementClass: jclass? = JNI.GetObjectClass( element, &sublocals )
179179
array = JNI.api.NewObjectArray( JNI.env, jsize(value.count), elementClass, nil )
180180
}
181181
JNI.api.SetObjectArrayElement( JNI.env, array, jsize(i), element )
@@ -210,19 +210,19 @@ extension JNIType {
210210
}
211211

212212
public static func toSwift<T: JNIObjectInit>( type: T.Type, from: jobject?, consume: Bool = true ) -> T? {
213-
guard let from = from else { return nil }
213+
guard let from: jobject = from else { return nil }
214214
defer { if consume { JNI.DeleteLocalRef( from ) } }
215215
return T( javaObject: from )
216216
}
217217

218218
public static func toSwift<T: JNIObjectInit>( type: [T].Type, from: jobject?, consume: Bool = true ) -> [T]? {
219-
guard let from = from else { return nil }
219+
guard let from: jobject = from else { return nil }
220220
defer { if consume { JNI.DeleteLocalRef( from ) } }
221221
return from.arrayMap { T( javaObject: $0 ) }
222222
}
223223

224224
public static func toSwift<T: JNIObjectInit>( type: [[T]].Type, from: jobject?, consume: Bool = true ) -> [[T]]? {
225-
guard let from = from else { return nil }
225+
guard let from: jobject = from else { return nil }
226226
defer { if consume { JNI.DeleteLocalRef( from ) } }
227227
return from.arrayMap { toSwift( type: [T].self, from: $0, consume: false ) ?? [T]() }
228228
}
@@ -231,14 +231,14 @@ extension JNIType {
231231
return jvalue( l: value?.localJavaObject( locals ) )
232232
}
233233

234-
public static func toJava( value: [String:JNIObjectProtocol]?, mapClass: String, locals: UnsafeMutablePointer<[jobject]> ) -> jvalue {
235-
guard let value = value else { return jvalue( l: nil ) }
234+
public static func toJava( value: [String: JNIObjectProtocol]?, mapClass: String, locals: UnsafeMutablePointer<[jobject]> ) -> jvalue {
235+
guard let value: [String: JNIObjectProtocol] = value else { return jvalue( l: nil ) }
236236

237237
var classCache: jclass?
238238
var methodID: jmethodID?
239239
var __locals = [jobject]()
240240
var __args = [jvalue]( repeating: jvalue(), count: 1 )
241-
guard let __object = JNIMethod.NewObject( className: mapClass, classCache: &classCache,
241+
guard let __object: jobject = JNIMethod.NewObject( className: mapClass, classCache: &classCache,
242242
methodSig: "()V", methodCache: &methodID, args: &__args, locals: &__locals ) else {
243243
JNI.report( "Unable to create HashMap of class \(mapClass)" )
244244
return jvalue( l: nil )
@@ -261,22 +261,22 @@ extension JNIType {
261261
return jvalue( l: __object )
262262
}
263263

264-
public static func toJava( value: [String:[JNIObjectProtocol]]?, mapClass: String, locals: UnsafeMutablePointer<[jobject]> ) -> jvalue {
265-
guard let value = value else { return jvalue( l: nil ) }
264+
public static func toJava( value: [String: [JNIObjectProtocol]]?, mapClass: String, locals: UnsafeMutablePointer<[jobject]> ) -> jvalue {
265+
guard let value: [String: [JNIObjectProtocol]] = value else { return jvalue( l: nil ) }
266266

267267
var classCache: jclass?
268268
var methodID: jmethodID?
269269
var __args = [jvalue]( repeating: jvalue(), count: 1 )
270270
var __locals = [jobject]()
271-
guard let __object = JNIMethod.NewObject( className: mapClass, classCache: &classCache,
271+
guard let __object: jobject = JNIMethod.NewObject( className: mapClass, classCache: &classCache,
272272
methodSig: "()V", methodCache: &methodID, args: &__args, locals: &__locals ) else {
273273
JNI.report( "Unable to create HashMap of class \(mapClass)" )
274274
return jvalue( l: nil )
275275
}
276276

277277
JNI.api.DeleteGlobalRef( JNI.env, classCache )
278278

279-
let map = HashMap( javaObject: __object )
279+
let map: HashMap = HashMap( javaObject: __object )
280280
for (key, item) in value {
281281
var sublocals = [jobject]()
282282
let javaKey = JavaObject( javaObject: toJava( value: key, locals: &sublocals ).l )
@@ -291,8 +291,8 @@ extension JNIType {
291291
return jvalue( l: __object )
292292
}
293293

294-
public static func toSwift<T: JNIObjectInit>( type: [String:T].Type, from: jobject?, consume: Bool = true ) -> [String:T]? {
295-
guard let from = from else { return nil }
294+
public static func toSwift<T: JNIObjectInit>( type: [String: T].Type, from: jobject?, consume: Bool = true ) -> [String: T]? {
295+
guard let from: jobject = from else { return nil }
296296
defer { if consume { JNI.DeleteLocalRef( from ) } }
297297
let map = HashMap( javaObject: from )
298298
var out = [String:T]()
@@ -301,7 +301,7 @@ extension JNIType {
301301
keyObject in
302302
map.get(key).withJavaObject {
303303
itemObject in
304-
if let keystr = JNIType.toSwift( type: String.self, from: keyObject, consume: false ) {
304+
if let keystr: String = JNIType.toSwift( type: String.self, from: keyObject, consume: false ) {
305305
out[keystr] = T( javaObject: itemObject )
306306
}
307307
}
@@ -310,8 +310,8 @@ extension JNIType {
310310
return out
311311
}
312312

313-
public static func toSwift<T: JNIObjectInit>( type: [String:[T]].Type, from: jobject?, consume: Bool = true ) -> [String:[T]]? {
314-
guard let from = from else { return nil }
313+
public static func toSwift<T: JNIObjectInit>( type: [String: [T]].Type, from: jobject?, consume: Bool = true ) -> [String: [T]]? {
314+
guard let from: jobject = from else { return nil }
315315
defer { if consume { JNI.DeleteLocalRef( from ) } }
316316
let map = HashMap( javaObject: from )
317317
var out = [String:[T]]()
@@ -320,8 +320,8 @@ extension JNIType {
320320
keyObject in
321321
map.get(key).withJavaObject {
322322
itemObject in
323-
if let keystr = JNIType.toSwift( type: String.self, from: keyObject, consume: false ),
324-
let value = JNIType.toSwift( type: [T].self, from: itemObject, consume: false ) {
323+
if let keystr: String = JNIType.toSwift( type: String.self, from: keyObject, consume: false ),
324+
let value: [T] = JNIType.toSwift( type: [T].self, from: itemObject, consume: false ) {
325325
out[keystr] = value
326326
}
327327
}

Sources/JNIProxy.swift

+24-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ open class JNIReleasableProxy {
2929
}
3030

3131
static public func canrelease( swiftObject: jlong ) {
32-
let toRelease = unsafeBitCast( recoverPointer( swiftObject ), to: JNIReleasableProxy.self )
32+
let toRelease: JNIReleasableProxy = unsafeBitCast( recoverPointer( swiftObject ), to: JNIReleasableProxy.self )
3333
toRelease.clearLocal()
3434
Unmanaged.passUnretained(toRelease).release()
3535
}
@@ -51,8 +51,8 @@ public let JNIReleasableProxy__finalize_thunk: JNIReleasableProxy__finalize_type
5151
open class JNILocalProxy<Owned, OwnedType>: JNIReleasableProxy, JNIObjectProtocol {
5252

5353
public func localJavaObject( _ locals: UnsafeMutablePointer<[jobject]> ) -> jobject? {
54-
let proxy = createProxy( className: type(of: self).proxyClassName(),
55-
classObject: type(of: self).proxyClass() )
54+
let proxy: jobject? = createProxy( className: type(of: self).proxyClassName(),
55+
classObject: type(of: self).proxyClass() )
5656
locals.pointee.append( proxy! )
5757
return proxy
5858
}
@@ -78,8 +78,8 @@ open class JNILocalProxy<Owned, OwnedType>: JNIReleasableProxy, JNIObjectProtoco
7878
guard javaObject != nil else { return }
7979
var locals = [jobject]()
8080
var fieldID: jfieldID?
81-
let existing = JNIField.GetLongField( fieldName: "__swiftObject", fieldType: "J", fieldCache: &fieldID,
82-
object: javaObject, file, line )
81+
let existing: jlong = JNIField.GetLongField( fieldName: "__swiftObject", fieldType: "J", fieldCache: &fieldID,
82+
object: javaObject, file, line )
8383
JNIField.SetLongField( fieldName: "__swiftObject", fieldType: "J", fieldCache: &fieldID,
8484
object: javaObject, value: swiftValue().j, locals: &locals, file, line )
8585
if existing != 0 {
@@ -95,9 +95,9 @@ open class JNILocalProxy<Owned, OwnedType>: JNIReleasableProxy, JNIObjectProtoco
9595
var locals = [jobject]()
9696
var methodID: jmethodID?
9797
var args: [jvalue] = [swiftValue()]
98-
if let newObject = JNIMethod.NewObject( className: className, classObject: classObject,
99-
methodSig: "(J)V", methodCache: &methodID,
100-
args: &args, locals: &locals ) {
98+
if let newObject: jobject = JNIMethod.NewObject( className: className, classObject: classObject,
99+
methodSig: "(J)V", methodCache: &methodID,
100+
args: &args, locals: &locals ) {
101101
return newObject
102102
}
103103
else {
@@ -111,7 +111,7 @@ open class JNILocalProxy<Owned, OwnedType>: JNIReleasableProxy, JNIObjectProtoco
111111
open class JNIObjectProxy<ObjectOwned> : JNILocalProxy<ObjectOwned, JNIObject> where ObjectOwned: JNIObject {
112112

113113
override public func localJavaObject(_ locals: UnsafeMutablePointer<[jobject]>) -> jobject? {
114-
let local = JNI.api.NewLocalRef( JNI.env, owned.javaObject )
114+
let local: jobject? = JNI.api.NewLocalRef( JNI.env, owned.javaObject )
115115
if local != nil {
116116
locals.pointee.append( local! )
117117
}
@@ -123,3 +123,18 @@ open class JNIObjectProxy<ObjectOwned> : JNILocalProxy<ObjectOwned, JNIObject> w
123123
}
124124

125125
}
126+
127+
public class ClosureRunnable: Runnable {
128+
129+
let closure: () -> ()
130+
131+
public init( _ closure: @escaping () -> () ) {
132+
self.closure = closure
133+
}
134+
135+
public func run() {
136+
closure()
137+
JNI.envCache[JNI.threadKey] = nil
138+
}
139+
140+
}

0 commit comments

Comments
 (0)