Skip to content

Commit 4111f62

Browse files
committed
Merge branch 'main' into types-as-arguments
2 parents 51c4233 + 86da9ee commit 4111f62

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

Sources/JExtractSwiftLib/Convenience/String+Extensions.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
extension String {
1616

17-
// TODO: naive implementation good enough for our simple case `methodMethodSomething` -> `MethodSomething`
18-
var toCamelCase: String {
17+
var firstCharacterUppercased: String {
1918
guard let f = first else {
2019
return self
2120
}

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,25 @@ extension ImportedFunc {
160160
let returnsBoolean = self.functionSignature.result.type.asNominalTypeDeclaration?.knownTypeKind == .bool
161161

162162
if !returnsBoolean {
163-
return "get\(self.name.toCamelCase)"
163+
return "get\(self.name.firstCharacterUppercased)"
164164
} else if !self.name.hasJavaBooleanNamingConvention {
165-
return "is\(self.name.toCamelCase)"
165+
return "is\(self.name.firstCharacterUppercased)"
166166
} else {
167-
return self.name.toCamelCase
167+
return self.name
168168
}
169169
}
170170

171171
var javaSetterName: String {
172-
"set\(self.name.toCamelCase)"
172+
let isBooleanSetter = self.functionSignature.parameters.first?.type.asNominalTypeDeclaration?.knownTypeKind == .bool
173+
174+
// If the variable is already named "isX", then we make
175+
// the setter "setX" to match beans spec.
176+
if isBooleanSetter && self.name.hasJavaBooleanNamingConvention {
177+
// Safe to force unwrap due to `hasJavaBooleanNamingConvention` check.
178+
let propertyName = self.name.split(separator: "is", maxSplits: 1).last!
179+
return "set\(propertyName)"
180+
} else {
181+
return "set\(self.name.firstCharacterUppercased)"
182+
}
173183
}
174184
}

Tests/JExtractSwiftTests/JNI/JNIVariablesTests.swift

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct JNIVariablesTests {
3333
set { }
3434
}
3535
public var someBoolean: Bool
36-
public let isBoolean: Bool
36+
public var isBoolean: Bool
3737
}
3838
"""
3939

@@ -372,7 +372,7 @@ struct JNIVariablesTests {
372372
}
373373

374374
@Test
375-
func boolean_swiftThunks() throws {
375+
func someBoolean_swiftThunks() throws {
376376
try assertOutput(
377377
input: membersSource,
378378
.jni,
@@ -404,4 +404,87 @@ struct JNIVariablesTests {
404404
]
405405
)
406406
}
407+
408+
@Test
409+
func isBoolean_javaBindings() throws {
410+
try assertOutput(
411+
input: membersSource,
412+
.jni,
413+
.java,
414+
detectChunkByInitialLines: 8,
415+
expectedChunks: [
416+
"""
417+
/**
418+
* Downcall to Swift:
419+
* {@snippet lang=swift :
420+
* public var isBoolean: Bool
421+
* }
422+
*/
423+
public boolean isBoolean() {
424+
long self$ = this.$memoryAddress();
425+
return MyClass.$isBoolean(self$);
426+
}
427+
""",
428+
"""
429+
/**
430+
* Downcall to Swift:
431+
* {@snippet lang=swift :
432+
* public var isBoolean: Bool
433+
* }
434+
*/
435+
public void setBoolean(boolean newValue) {
436+
long self$ = this.$memoryAddress();
437+
MyClass.$setBoolean(newValue, self$);
438+
}
439+
""",
440+
"""
441+
private static native boolean $isBoolean(long selfPointer);
442+
""",
443+
"""
444+
private static native void $setBoolean(boolean newValue, long selfPointer);
445+
"""
446+
]
447+
)
448+
}
449+
450+
@Test
451+
func isBoolean_swiftThunks() throws {
452+
try assertOutput(
453+
input: membersSource,
454+
.jni,
455+
.swift,
456+
detectChunkByInitialLines: 1,
457+
expectedChunks: [
458+
"""
459+
@_cdecl("Java_com_example_swift_MyClass__00024isBoolean__J")
460+
func Java_com_example_swift_MyClass__00024isBoolean__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) -> jboolean {
461+
guard let env$ = environment else {
462+
fatalError("Missing JNIEnv in downcall to \\(#function)")
463+
}
464+
assert(selfPointer != 0, "selfPointer memory address was null")
465+
let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
466+
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
467+
fatalError("self memory address was null in call to \\(#function)!")
468+
}
469+
let result = self$.pointee.isBoolean
470+
return result.getJNIValue(in: environment)
471+
}
472+
""",
473+
"""
474+
@_cdecl("Java_com_example_swift_MyClass__00024setBoolean__ZJ")
475+
func Java_com_example_swift_MyClass__00024setBoolean__ZJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jboolean, selfPointer: jlong) {
476+
guard let env$ = environment else {
477+
fatalError("Missing JNIEnv in downcall to \\(#function)")
478+
}
479+
assert(selfPointer != 0, "selfPointer memory address was null")
480+
let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
481+
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
482+
fatalError("self memory address was null in call to \\(#function)!")
483+
}
484+
self$.pointee.isBoolean = Bool(fromJNI: newValue, in: environment!)
485+
}
486+
"""
487+
]
488+
)
489+
}
407490
}

0 commit comments

Comments
 (0)