Skip to content

Commit 5cabd15

Browse files
authored
Merge pull request #5 from SwiftAndroid/methodChanges-swift4
Further improvements
2 parents 9227b25 + 4b18b6c commit 5cabd15

27 files changed

+494
-498
lines changed
File renamed without changes.
File renamed without changes.

JNISwift.xcodeproj/project.pbxproj renamed to JNI.xcodeproj/project.pbxproj

Lines changed: 129 additions & 128 deletions
Large diffs are not rendered by default.

JNISwift.xcodeproj/xcshareddata/xcschemes/JNISwift.xcscheme renamed to JNI.xcodeproj/xcshareddata/xcschemes/JNI-Package.xcscheme

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
33
LastUpgradeVersion = "9999"
4-
version = "1.8">
4+
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
77
buildImplicitDependencies = "YES">
@@ -14,10 +14,10 @@
1414
buildForAnalyzing = "YES">
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
17-
BlueprintIdentifier = "OBJ_27"
18-
BuildableName = "JNISwift.framework"
19-
BlueprintName = "JNISwift"
20-
ReferencedContainer = "container:JNISwift.xcodeproj">
17+
BlueprintIdentifier = "JNI::JNI"
18+
BuildableName = "JNI.framework"
19+
BlueprintName = "JNI"
20+
ReferencedContainer = "container:JNI.xcodeproj">
2121
</BuildableReference>
2222
</BuildActionEntry>
2323
<BuildActionEntry
@@ -28,10 +28,10 @@
2828
buildForAnalyzing = "YES">
2929
<BuildableReference
3030
BuildableIdentifier = "primary"
31-
BlueprintIdentifier = "OBJ_43"
31+
BlueprintIdentifier = "JNI::CJNI"
3232
BuildableName = "CJNI.framework"
3333
BlueprintName = "CJNI"
34-
ReferencedContainer = "container:JNISwift.xcodeproj">
34+
ReferencedContainer = "container:JNI.xcodeproj">
3535
</BuildableReference>
3636
</BuildActionEntry>
3737
</BuildActionEntries>
@@ -51,6 +51,7 @@
5151
buildConfiguration = "Debug"
5252
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5353
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
54+
language = ""
5455
launchStyle = "0"
5556
useCustomWorkingDirectory = "NO"
5657
ignoresPersistentStateOnLaunch = "NO"
@@ -60,10 +61,10 @@
6061
<MacroExpansion>
6162
<BuildableReference
6263
BuildableIdentifier = "primary"
63-
BlueprintIdentifier = "OBJ_27"
64-
BuildableName = "JNISwift.framework"
65-
BlueprintName = "JNISwift"
66-
ReferencedContainer = "container:JNISwift.xcodeproj">
64+
BlueprintIdentifier = "JNI::JNI"
65+
BuildableName = "JNI.framework"
66+
BlueprintName = "JNI"
67+
ReferencedContainer = "container:JNI.xcodeproj">
6768
</BuildableReference>
6869
</MacroExpansion>
6970
<AdditionalOptions>

JNISwift.xcodeproj/xcshareddata/xcschemes/xcschememanagement.plist renamed to JNI.xcodeproj/xcshareddata/xcschemes/xcschememanagement.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<dict>
44
<key>SchemeUserState</key>
55
<dict>
6-
<key>JNISwift-Package.xcscheme</key>
6+
<key>JNI-Package.xcscheme</key>
77
<dict></dict>
88
</dict>
99
<key>SuppressBuildableAutocreation</key>

JNISwift.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 7 deletions
This file was deleted.

JNISwift.xcodeproj/xcshareddata/xcschemes/JNISwift-Package.xcscheme

Lines changed: 0 additions & 87 deletions
This file was deleted.

Package.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// swift-tools-version:3.1
1+
// swift-tools-version:4.0
22

33
import PackageDescription
44

55
let package = Package(
6-
name: "JNISwift",
6+
name: "JNI",
7+
products: [
8+
.library(name: "JNI", type: .dynamic, targets: ["JNI", "CJNI"])
9+
],
710
targets: [
8-
Target(name: "JNISwift", dependencies: ["CJNI"]),
9-
Target(name: "CJNI", dependencies: [])
11+
.target(name: "CJNI"),
12+
.target(name: "JNI", dependencies: ["CJNI"])
1013
]
1114
)
12-
13-
products.append(Product(name: "JNISwift", type: .Library(.Dynamic), modules: ["JNISwift"]))

Sources/CJNI/cjni.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Sources/CJNI/include/jni.h

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ struct JNINativeInterface
383383

384384
jstring (* _Nonnull NewString)(JNIEnv * _Nonnull, const jchar * _Nonnull, jsize);
385385
jsize (* _Nonnull GetStringLength)(JNIEnv * _Nonnull, jstring);
386-
const jchar *(* _Nonnull GetStringChars)(JNIEnv * _Nonnull, jstring, jboolean * _Nullable);
386+
const jchar * _Nullable(* _Nonnull GetStringChars)(JNIEnv * _Nonnull, jstring, jboolean * _Nullable);
387387
void (* _Nonnull ReleaseStringChars)(JNIEnv * _Nonnull, jstring, const jchar * _Nonnull);
388388
jstring (* _Nonnull NewStringUTF)(JNIEnv * _Nonnull, const char * _Nonnull);
389389
jsize (* _Nonnull GetStringUTFLength)(JNIEnv * _Nonnull, jstring);
@@ -413,14 +413,14 @@ struct JNINativeInterface
413413
jfloat * _Nullable (* _Nonnull GetFloatArrayElements)(JNIEnv * _Nonnull, jfloatArray, jboolean * _Nonnull);
414414
jdouble * _Nullable (* _Nonnull GetDoubleArrayElements)(JNIEnv * _Nonnull, jdoubleArray, jboolean * _Nonnull);
415415

416-
void (* _Nonnull ReleaseBooleanArrayElements)(JNIEnv * _Nonnull, jbooleanArray, jboolean *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
417-
void (* _Nonnull ReleaseByteArrayElements)(JNIEnv * _Nonnull, jbyteArray, jbyte *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
418-
void (* _Nonnull ReleaseCharArrayElements)(JNIEnv * _Nonnull, jcharArray, jchar *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
419-
void (* _Nonnull ReleaseShortArrayElements)(JNIEnv * _Nonnull, jshortArray, jshort *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
420-
void (* _Nonnull ReleaseIntArrayElements)(JNIEnv * _Nonnull, jintArray, jint *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
421-
void (* _Nonnull ReleaseLongArrayElements)(JNIEnv * _Nonnull, jlongArray, jlong *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
422-
void (* _Nonnull ReleaseFloatArrayElements)(JNIEnv * _Nonnull, jfloatArray, jfloat *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
423-
void (* _Nonnull ReleaseDoubleArrayElements)(JNIEnv * _Nonnull, jdoubleArray, jdouble *, jint) CF_SWIFT_NAME(ReleaseArrayElements);
416+
void (* _Nonnull ReleaseBooleanArrayElements)(JNIEnv * _Nonnull, jbooleanArray, jboolean * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
417+
void (* _Nonnull ReleaseByteArrayElements)(JNIEnv * _Nonnull, jbyteArray, jbyte * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
418+
void (* _Nonnull ReleaseCharArrayElements)(JNIEnv * _Nonnull, jcharArray, jchar * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
419+
void (* _Nonnull ReleaseShortArrayElements)(JNIEnv * _Nonnull, jshortArray, jshort * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
420+
void (* _Nonnull ReleaseIntArrayElements)(JNIEnv * _Nonnull, jintArray, jint * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
421+
void (* _Nonnull ReleaseLongArrayElements)(JNIEnv * _Nonnull, jlongArray, jlong * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
422+
void (* _Nonnull ReleaseFloatArrayElements)(JNIEnv * _Nonnull, jfloatArray, jfloat * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
423+
void (* _Nonnull ReleaseDoubleArrayElements)(JNIEnv * _Nonnull, jdoubleArray, jdouble * _Nullable, jint) CF_SWIFT_NAME(ReleaseArrayElements);
424424

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

444-
jint (* _Nonnull RegisterNatives)(JNIEnv * _Nonnull, jclass, const JNINativeMethod *, jint);
444+
jint (* _Nonnull RegisterNatives)(JNIEnv * _Nonnull, jclass, const JNINativeMethod * _Nonnull, jint);
445445
jint (* _Nonnull UnregisterNatives)(JNIEnv * _Nonnull, jclass);
446446
jint (* _Nonnull MonitorEnter)(JNIEnv * _Nonnull, jobject);
447447
jint (* _Nonnull MonitorExit)(JNIEnv * _Nonnull, jobject);
@@ -450,11 +450,11 @@ struct JNINativeInterface
450450
void (* _Nonnull GetStringRegion)(JNIEnv * _Nonnull, jstring, jsize, jsize, jchar * _Nonnull);
451451
void (* _Nonnull GetStringUTFRegion)(JNIEnv * _Nonnull, jstring, jsize, jsize, char * _Nonnull);
452452

453-
void *(* _Nonnull GetPrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, jboolean * _Nonnull);
454-
void (* _Nonnull ReleasePrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, void *, jint);
453+
void * _Nullable(* _Nonnull GetPrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, jboolean * _Nonnull);
454+
void (* _Nonnull ReleasePrimitiveArrayCritical)(JNIEnv * _Nonnull, jarray, void * _Nullable, jint);
455455

456-
const jchar *(* _Nonnull GetStringCritical)(JNIEnv * _Nonnull, jstring, jboolean * _Nonnull);
457-
void (* _Nonnull ReleaseStringCritical)(JNIEnv * _Nonnull, jstring, const jchar * _Nonnull);
456+
const jchar * _Nullable(* _Nonnull GetStringCritical)(JNIEnv * _Nonnull, jstring, jboolean * _Nonnull);
457+
void (* _Nonnull ReleaseStringCritical)(JNIEnv * _Nonnull, jstring, const jchar * _Nullable);
458458

459459
jweak (* _Nonnull NewWeakGlobalRef)(JNIEnv * _Nonnull, jobject);
460460
void (* _Nonnull DeleteWeakGlobalRef)(JNIEnv * _Nonnull, jweak);
@@ -1332,23 +1332,23 @@ struct _JNIEnv
13321332
*/
13331333
struct JNIInvokeInterface
13341334
{
1335-
void *reserved0;
1336-
void *reserved1;
1337-
void *reserved2;
1335+
void * _Null_unspecified reserved0;
1336+
void * _Null_unspecified reserved1;
1337+
void * _Null_unspecified reserved2;
13381338

13391339
jint (* _Nonnull DestroyJavaVM)(JavaVM * _Nonnull);
1340-
jint (* _Nonnull AttachCurrentThread)(JavaVM *, JNIEnv **, void * _Nullable);
1340+
jint (* _Nonnull AttachCurrentThread)(JavaVM * _Nonnull, JNIEnv * _Nullable * _Nullable, void * _Nullable);
13411341
jint (* _Nonnull DetachCurrentThread)(JavaVM * _Nonnull);
1342-
jint (* _Nonnull GetEnv)(JavaVM *, void **, jint);
1343-
jint (* _Nonnull AttachCurrentThreadAsDaemon)(JavaVM *, JNIEnv **, void * _Nonnull);
1342+
jint (* _Nonnull GetEnv)(JavaVM * _Nonnull, void * _Nullable * _Nullable, jint);
1343+
jint (* _Nonnull AttachCurrentThreadAsDaemon)(JavaVM * _Nonnull, JNIEnv * _Nullable * _Nullable, void * _Nullable);
13441344
};
13451345

13461346
/*
13471347
* C++ version.
13481348
*/
13491349
struct _JavaVM
13501350
{
1351-
const struct JNIInvokeInterface *functions;
1351+
const struct JNIInvokeInterface * _Nonnull functions;
13521352

13531353
#if defined(__cplusplus)
13541354
jint DestroyJavaVM()
@@ -1376,9 +1376,9 @@ struct _JavaVM
13761376

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

@@ -1388,16 +1388,16 @@ typedef struct JavaVMAttachArgs JavaVMAttachArgs;
13881388
*/
13891389
typedef struct JavaVMOption
13901390
{
1391-
const char *optionString;
1392-
void *extraInfo;
1391+
const char * _Nullable optionString;
1392+
void * _Nullable extraInfo;
13931393
} JavaVMOption;
13941394

13951395
typedef struct JavaVMInitArgs
13961396
{
13971397
jint version; /* use JNI_VERSION_1_2 or later */
13981398

13991399
jint nOptions;
1400-
JavaVMOption *options;
1400+
JavaVMOption * _Nonnull options;
14011401
jboolean ignoreUnrecognized;
14021402
} JavaVMInitArgs;
14031403

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

14291429
#ifdef __cplusplus
14301430
}

Sources/CJNI/include/module.modulemap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ module CJNI {
22
header "jni.h"
33
export *
44
}
5-

Sources/CJNI/jni.c

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Array+JavaParameterConvertible.swift
3+
// JNI
4+
//
5+
// Created by flowing erik on 19.07.17.
6+
//
7+
8+
import CJNI
9+
10+
extension String {
11+
func replacingFullstopsWithSlashes() -> String {
12+
let replacedCharacters = self.characters.map { ($0 == ".") ? "/" : $0 }
13+
return String(String.CharacterView(replacedCharacters))
14+
}
15+
}
16+
17+
extension Array where Element == JavaParameterConvertible {
18+
func asJavaParameters() -> [JavaParameter] {
19+
return self.map { $0.toJavaParameter() }
20+
}
21+
22+
/// Returns the String of ordererd arguments for use in JNI method signatures.
23+
/// For example, the `"II"` in `(II)V`
24+
private func argumentSignature() -> String {
25+
return self.reduce("", { $0 + type(of: $1).asJNIParameterString })
26+
}
27+
28+
/// Returns the String of ordered arguments enclosed in brackets, followed by the `returnType`'s type string, or 'V'
29+
/// (Void) if nil is provided. e.g. Returns "(II)V" for `[JavaInt(1), JavaInt(99)].methodSignature(returnType: nil)`
30+
func methodSignature(returnType: JavaParameterConvertible.Type?) -> String {
31+
let returnTypeString = returnType?.asJNIParameterString ?? "V"
32+
return "(" + self.argumentSignature() + ")" + returnTypeString
33+
}
34+
35+
func methodSignature(customReturnType: String) -> String {
36+
let returnTypeString = customReturnType.replacingFullstopsWithSlashes()
37+
return "(" + self.argumentSignature() + ")" + returnTypeString
38+
}
39+
}

Sources/JNISwift/JNI.swift renamed to Sources/JNI/JNI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import CJNI
1+
@_exported import CJNI // "_exported" so we don't have to import both CJNI and JNI all the time
22

33
public class JNI {
44
/// Our reference to the Java Virtual Machine, to be set on init

Sources/JNISwift/JNIClassManipulation.swift renamed to Sources/JNI/JNIClassManipulation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ public extension JNI {
66
// return env.pointee.pointee.DefineClass(env, name, loader, buffer, bufferLength)!
77
// }
88

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

1414
public func FromReflectedMethod(method: JavaObject) -> JavaMethodID {

0 commit comments

Comments
 (0)