Skip to content

Commit 5628cf1

Browse files
committed
update setter name to match spec
1 parent d06c172 commit 5628cf1

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ extension ImportedFunc {
169169
}
170170

171171
var javaSetterName: String {
172-
"set\(self.name.firstCharacterUppercased)"
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/Asserts/TextAssertions.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ func assertOutput(
115115
print("==== ---------------------------------------------------------------")
116116

117117
#expect(output.contains(expectedChunk), sourceLocation: sourceLocation)
118-
fatalError("Failed: \(filePath):\(line)")
119118
continue
120119
}
121120

Tests/JExtractSwiftTests/JNI/JNIVariablesTests.swift

Lines changed: 31 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

@@ -463,7 +463,7 @@ struct JNIVariablesTests {
463463
/**
464464
* Downcall to Swift:
465465
* {@snippet lang=swift :
466-
* public let isBoolean: Bool
466+
* public var isBoolean: Bool
467467
* }
468468
*/
469469
public boolean isBoolean() {
@@ -472,8 +472,23 @@ struct JNIVariablesTests {
472472
}
473473
""",
474474
"""
475+
/**
476+
* Downcall to Swift:
477+
* {@snippet lang=swift :
478+
* public var isBoolean: Bool
479+
* }
480+
*/
481+
public void setBoolean(boolean newValue) {
482+
long self$ = this.$memoryAddress();
483+
MyClass.$setBoolean(newValue, self$);
484+
}
485+
""",
486+
"""
475487
private static native boolean $isBoolean(long selfPointer);
476488
""",
489+
"""
490+
private static native void $setBoolean(boolean newValue, long selfPointer);
491+
"""
477492
]
478493
)
479494
}
@@ -501,6 +516,20 @@ struct JNIVariablesTests {
501516
return result.getJNIValue(in: environment)
502517
}
503518
""",
519+
"""
520+
@_cdecl("Java_com_example_swift_MyClass__00024setBoolean__ZJ")
521+
func Java_com_example_swift_MyClass__00024setBoolean__ZJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jboolean, selfPointer: jlong) {
522+
guard let env$ = environment else {
523+
fatalError("Missing JNIEnv in downcall to \\(#function)")
524+
}
525+
assert(selfPointer != 0, "selfPointer memory address was null")
526+
let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
527+
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
528+
fatalError("self memory address was null in call to \\(#function)!")
529+
}
530+
self$.pointee.isBoolean = Bool(fromJNI: newValue, in: environment!)
531+
}
532+
"""
504533
]
505534
)
506535
}

0 commit comments

Comments
 (0)