Skip to content

API Enhancements #18

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

Merged
merged 3 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/JNI/JNI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public extension JNI {
let env = self._env
return env.pointee.pointee.MonitorExit(env, obj)
}

func GetDirectBufferAddress(buffer: JavaObject) -> UnsafeMutableRawPointer? {
let env = self._env
return env.pointee.pointee.GetDirectBufferAddress(env, buffer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

}
}
11 changes: 11 additions & 0 deletions Sources/JNI/JNIFields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public extension JNI {
try checkAndThrowOnJNIError()
return try T.fromField(fieldID!, on: javaObject)
}

func GetField(_ fieldName: String, fieldJavaClassName: String, from javaObject: JavaObject) throws -> JavaObject {
let env = self._env
let javaClass = try GetObjectClass(obj: javaObject)
defer {
jni.DeleteLocalRef(javaClass)
}
let fieldID = env.pointee.pointee.GetFieldID(env, javaClass, fieldName, "L\(fieldJavaClassName);")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but I think we might need to delete the local ref to the class here after we use it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes, totally oversaw this. we do it also in the other methods above :+1

try checkAndThrowOnJNIError()
return try JavaObject.fromField(fieldID!, on: javaObject)
}
}


Expand Down
4 changes: 4 additions & 0 deletions Sources/JNI/JNIObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ open class JNIObject {
return try jni.GetField(fieldName, from: self.instance)
}

public func getField(_ fieldName: String, fieldJavaClassName: String) throws -> JavaObject {
return try jni.GetField(fieldName, fieldJavaClassName: fieldJavaClassName, from: self.instance)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool looks good, ideally we'd add getters for primitive fields too (Int etc) but we can also do that later when/if we need it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you mean is already covered by the generic methods above.

}

public static func callStatic(methodName: String, arguments: [JavaParameterConvertible] = []) throws {
try jni.callStatic(methodName, on: self.javaClass, arguments: arguments)
}
Expand Down