Skip to content

Further improvements #5

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 8 commits into from
Aug 3, 2017
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
File renamed without changes.
File renamed without changes.
257 changes: 129 additions & 128 deletions JNISwift.xcodeproj/project.pbxproj → JNI.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "9999"
version = "1.8">
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
Expand All @@ -14,10 +14,10 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "OBJ_27"
BuildableName = "JNISwift.framework"
BlueprintName = "JNISwift"
ReferencedContainer = "container:JNISwift.xcodeproj">
BlueprintIdentifier = "JNI::JNI"
BuildableName = "JNI.framework"
BlueprintName = "JNI"
ReferencedContainer = "container:JNI.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
Expand All @@ -28,10 +28,10 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "OBJ_43"
BlueprintIdentifier = "JNI::CJNI"
BuildableName = "CJNI.framework"
BlueprintName = "CJNI"
ReferencedContainer = "container:JNISwift.xcodeproj">
ReferencedContainer = "container:JNI.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
Expand All @@ -51,6 +51,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand All @@ -60,10 +61,10 @@
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "OBJ_27"
BuildableName = "JNISwift.framework"
BlueprintName = "JNISwift"
ReferencedContainer = "container:JNISwift.xcodeproj">
BlueprintIdentifier = "JNI::JNI"
BuildableName = "JNI.framework"
BlueprintName = "JNI"
ReferencedContainer = "container:JNI.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<dict>
<key>SchemeUserState</key>
<dict>
<key>JNISwift-Package.xcscheme</key>
<key>JNI-Package.xcscheme</key>
<dict></dict>
</dict>
<key>SuppressBuildableAutocreation</key>
Expand Down

This file was deleted.

This file was deleted.

13 changes: 7 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// swift-tools-version:3.1
// swift-tools-version:4.0

import PackageDescription

let package = Package(
name: "JNISwift",
name: "JNI",
products: [
.library(name: "JNI", type: .dynamic, targets: ["JNI", "CJNI"])
],
targets: [
Target(name: "JNISwift", dependencies: ["CJNI"]),
Target(name: "CJNI", dependencies: [])
.target(name: "CJNI"),
.target(name: "JNI", dependencies: ["CJNI"])
]
)

products.append(Product(name: "JNISwift", type: .Library(.Dynamic), modules: ["JNISwift"]))
1 change: 1 addition & 0 deletions Sources/CJNI/cjni.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

58 changes: 29 additions & 29 deletions Sources/CJNI/include/jni.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ struct JNINativeInterface

jstring (* _Nonnull NewString)(JNIEnv * _Nonnull, const jchar * _Nonnull, jsize);
jsize (* _Nonnull GetStringLength)(JNIEnv * _Nonnull, jstring);
const jchar *(* _Nonnull GetStringChars)(JNIEnv * _Nonnull, jstring, jboolean * _Nullable);
const jchar * _Nullable(* _Nonnull GetStringChars)(JNIEnv * _Nonnull, jstring, jboolean * _Nullable);
void (* _Nonnull ReleaseStringChars)(JNIEnv * _Nonnull, jstring, const jchar * _Nonnull);
jstring (* _Nonnull NewStringUTF)(JNIEnv * _Nonnull, const char * _Nonnull);
jsize (* _Nonnull GetStringUTFLength)(JNIEnv * _Nonnull, jstring);
Expand Down Expand Up @@ -413,14 +413,14 @@ struct JNINativeInterface
jfloat * _Nullable (* _Nonnull GetFloatArrayElements)(JNIEnv * _Nonnull, jfloatArray, jboolean * _Nonnull);
jdouble * _Nullable (* _Nonnull GetDoubleArrayElements)(JNIEnv * _Nonnull, jdoubleArray, jboolean * _Nonnull);

void (* _Nonnull ReleaseBooleanArrayElements)(JNIEnv * _Nonnull, jbooleanArray, jboolean *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseByteArrayElements)(JNIEnv * _Nonnull, jbyteArray, jbyte *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseCharArrayElements)(JNIEnv * _Nonnull, jcharArray, jchar *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseShortArrayElements)(JNIEnv * _Nonnull, jshortArray, jshort *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseIntArrayElements)(JNIEnv * _Nonnull, jintArray, jint *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseLongArrayElements)(JNIEnv * _Nonnull, jlongArray, jlong *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseFloatArrayElements)(JNIEnv * _Nonnull, jfloatArray, jfloat *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseDoubleArrayElements)(JNIEnv * _Nonnull, jdoubleArray, jdouble *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseBooleanArrayElements)(JNIEnv * _Nonnull, jbooleanArray, jboolean * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseByteArrayElements)(JNIEnv * _Nonnull, jbyteArray, jbyte * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseCharArrayElements)(JNIEnv * _Nonnull, jcharArray, jchar * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseShortArrayElements)(JNIEnv * _Nonnull, jshortArray, jshort * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseIntArrayElements)(JNIEnv * _Nonnull, jintArray, jint * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseLongArrayElements)(JNIEnv * _Nonnull, jlongArray, jlong * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseFloatArrayElements)(JNIEnv * _Nonnull, jfloatArray, jfloat * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
void (* _Nonnull ReleaseDoubleArrayElements)(JNIEnv * _Nonnull, jdoubleArray, jdouble * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);

void (* _Nonnull GetBooleanArrayRegion)(JNIEnv * _Nonnull, jbooleanArray, jsize, jsize, jboolean * _Nonnull);
void (* _Nonnull GetByteArrayRegion)(JNIEnv * _Nonnull, jbyteArray, jsize, jsize, jbyte * _Nonnull);
Expand All @@ -441,7 +441,7 @@ struct JNINativeInterface
void (* _Nonnull SetFloatArrayRegion)(JNIEnv * _Nonnull, jfloatArray, jsize, jsize, const jfloat * _Nonnull) CF_SWIFT_NAME(SetArrayRegion);
void (* _Nonnull SetDoubleArrayRegion)(JNIEnv * _Nonnull, jdoubleArray, jsize, jsize, const jdouble * _Nonnull) CF_SWIFT_NAME(SetArrayRegion);

jint (* _Nonnull RegisterNatives)(JNIEnv * _Nonnull, jclass, const JNINativeMethod *, jint);
jint (* _Nonnull RegisterNatives)(JNIEnv * _Nonnull, jclass, const JNINativeMethod * _Nonnull, jint);
jint (* _Nonnull UnregisterNatives)(JNIEnv * _Nonnull, jclass);
jint (* _Nonnull MonitorEnter)(JNIEnv * _Nonnull, jobject);
jint (* _Nonnull MonitorExit)(JNIEnv * _Nonnull, jobject);
Expand All @@ -450,11 +450,11 @@ struct JNINativeInterface
void (* _Nonnull GetStringRegion)(JNIEnv * _Nonnull, jstring, jsize, jsize, jchar * _Nonnull);
void (* _Nonnull GetStringUTFRegion)(JNIEnv * _Nonnull, jstring, jsize, jsize, char * _Nonnull);

void *(* _Nonnull GetPrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, jboolean * _Nonnull);
void (* _Nonnull ReleasePrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, void *, jint);
void * _Nullable(* _Nonnull GetPrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, jboolean * _Nonnull);
void (* _Nonnull ReleasePrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, void * _Nullable, jint);

const jchar *(* _Nonnull GetStringCritical)(JNIEnv * _Nonnull, jstring, jboolean * _Nonnull);
void (* _Nonnull ReleaseStringCritical)(JNIEnv * _Nonnull, jstring, const jchar * _Nonnull);
const jchar * _Nullable(* _Nonnull GetStringCritical)(JNIEnv * _Nonnull, jstring, jboolean * _Nonnull);
void (* _Nonnull ReleaseStringCritical)(JNIEnv * _Nonnull, jstring, const jchar * _Nullable);

jweak (* _Nonnull NewWeakGlobalRef)(JNIEnv * _Nonnull, jobject);
void (* _Nonnull DeleteWeakGlobalRef)(JNIEnv * _Nonnull, jweak);
Expand Down Expand Up @@ -1332,23 +1332,23 @@ struct _JNIEnv
*/
struct JNIInvokeInterface
{
void *reserved0;
void *reserved1;
void *reserved2;
void * _Null_unspecified reserved0;
void * _Null_unspecified reserved1;
void * _Null_unspecified reserved2;

jint (* _Nonnull DestroyJavaVM)(JavaVM * _Nonnull);
jint (* _Nonnull AttachCurrentThread)(JavaVM *, JNIEnv **, void * _Nullable);
jint (* _Nonnull AttachCurrentThread)(JavaVM * _Nonnull, JNIEnv * _Nullable * _Nullable, void * _Nullable);
jint (* _Nonnull DetachCurrentThread)(JavaVM * _Nonnull);
jint (* _Nonnull GetEnv)(JavaVM *, void **, jint);
jint (* _Nonnull AttachCurrentThreadAsDaemon)(JavaVM *, JNIEnv **, void * _Nonnull);
jint (* _Nonnull GetEnv)(JavaVM * _Nonnull, void * _Nullable * _Nullable, jint);
jint (* _Nonnull AttachCurrentThreadAsDaemon)(JavaVM * _Nonnull, JNIEnv * _Nullable * _Nullable, void * _Nullable);
};

/*
* C++ version.
*/
struct _JavaVM
{
const struct JNIInvokeInterface *functions;
const struct JNIInvokeInterface * _Nonnull functions;

#if defined(__cplusplus)
jint DestroyJavaVM()
Expand Down Expand Up @@ -1376,9 +1376,9 @@ struct _JavaVM

struct JavaVMAttachArgs
{
jint version; /* must be >= JNI_VERSION_1_2 */
const char *name; /* NULL or name of thread as modified UTF-8 str */
jobject group; /* global ref of a ThreadGroup object, or NULL */
jint version; /** must be >= JNI_VERSION_1_2 */
const char * _Nullable name; /** NULL or name of thread as modified UTF-8 str */
jobject group; /** global ref of a ThreadGroup object, or NULL */
};
typedef struct JavaVMAttachArgs JavaVMAttachArgs;

Expand All @@ -1388,16 +1388,16 @@ typedef struct JavaVMAttachArgs JavaVMAttachArgs;
*/
typedef struct JavaVMOption
{
const char *optionString;
void *extraInfo;
const char * _Nullable optionString;
void * _Nullable extraInfo;
} JavaVMOption;

typedef struct JavaVMInitArgs
{
jint version; /* use JNI_VERSION_1_2 or later */

jint nOptions;
JavaVMOption *options;
JavaVMOption * _Nonnull options;
jboolean ignoreUnrecognized;
} JavaVMInitArgs;

Expand All @@ -1423,8 +1423,8 @@ jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize* _Nonnull);
* Prototypes for functions exported by loadable shared libs. These are
* called by JNI, not provided by JNI.
*/
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved);
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved);
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM * _Nonnull vm, void * _Null_unspecified reserved);
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM * _Nonnull vm, void * _Null_unspecified reserved);

#ifdef __cplusplus
}
Expand Down
1 change: 0 additions & 1 deletion Sources/CJNI/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ module CJNI {
header "jni.h"
export *
}

4 changes: 0 additions & 4 deletions Sources/CJNI/jni.c

This file was deleted.

39 changes: 39 additions & 0 deletions Sources/JNI/Array+JavaParameterConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Array+JavaParameterConvertible.swift
// JNI
//
// Created by flowing erik on 19.07.17.
//

import CJNI

extension String {
func replacingFullstopsWithSlashes() -> String {
let replacedCharacters = self.characters.map { ($0 == ".") ? "/" : $0 }
return String(String.CharacterView(replacedCharacters))
}
}

extension Array where Element == JavaParameterConvertible {
func asJavaParameters() -> [JavaParameter] {
return self.map { $0.toJavaParameter() }
}

/// Returns the String of ordererd arguments for use in JNI method signatures.
/// For example, the `"II"` in `(II)V`
private func argumentSignature() -> String {
return self.reduce("", { $0 + type(of: $1).asJNIParameterString })
}

/// Returns the String of ordered arguments enclosed in brackets, followed by the `returnType`'s type string, or 'V'
/// (Void) if nil is provided. e.g. Returns "(II)V" for `[JavaInt(1), JavaInt(99)].methodSignature(returnType: nil)`
func methodSignature(returnType: JavaParameterConvertible.Type?) -> String {
let returnTypeString = returnType?.asJNIParameterString ?? "V"
return "(" + self.argumentSignature() + ")" + returnTypeString
}

func methodSignature(customReturnType: String) -> String {
let returnTypeString = customReturnType.replacingFullstopsWithSlashes()
return "(" + self.argumentSignature() + ")" + returnTypeString
}
}
2 changes: 1 addition & 1 deletion Sources/JNISwift/JNI.swift → Sources/JNI/JNI.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CJNI
@_exported import CJNI // "_exported" so we don't have to import both CJNI and JNI all the time

public class JNI {
/// Our reference to the Java Virtual Machine, to be set on init
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public extension JNI {
// return env.pointee.pointee.DefineClass(env, name, loader, buffer, bufferLength)!
// }

public func FindClass(name: String) -> JavaClass {
public func FindClass(name: String) -> JavaClass? {
let env = self._env
return env.pointee.pointee.FindClass(env, name)!
return env.pointee.pointee.FindClass(env, name)
}

public func FromReflectedMethod(method: JavaObject) -> JavaMethodID {
Expand Down
File renamed without changes.
Loading