Skip to content

[JExtract/JNI] Fix boolean naming for variables already called isX #325

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 2 commits into from
Jul 22, 2025
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
3 changes: 1 addition & 2 deletions Sources/JExtractSwiftLib/Convenience/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

extension String {

// TODO: naive implementation good enough for our simple case `methodMethodSomething` -> `MethodSomething`
var toCamelCase: String {
var firstCharacterUppercased: String {
guard let f = first else {
return self
}
Expand Down
18 changes: 14 additions & 4 deletions Sources/JExtractSwiftLib/ImportedDecls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,25 @@ extension ImportedFunc {
let returnsBoolean = self.functionSignature.result.type.asNominalTypeDeclaration?.knownTypeKind == .bool

if !returnsBoolean {
return "get\(self.name.toCamelCase)"
return "get\(self.name.firstCharacterUppercased)"
} else if !self.name.hasJavaBooleanNamingConvention {
return "is\(self.name.toCamelCase)"
return "is\(self.name.firstCharacterUppercased)"
} else {
return self.name.toCamelCase
return self.name
}
}

var javaSetterName: String {
"set\(self.name.toCamelCase)"
let isBooleanSetter = self.functionSignature.parameters.first?.type.asNominalTypeDeclaration?.knownTypeKind == .bool

// If the variable is already named "isX", then we make
// the setter "setX" to match beans spec.
if isBooleanSetter && self.name.hasJavaBooleanNamingConvention {
// Safe to force unwrap due to `hasJavaBooleanNamingConvention` check.
let propertyName = self.name.split(separator: "is", maxSplits: 1).last!
return "set\(propertyName)"
} else {
return "set\(self.name.firstCharacterUppercased)"
}
}
}
1 change: 0 additions & 1 deletion Tests/JExtractSwiftTests/Asserts/TextAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func assertOutput(
print("==== ---------------------------------------------------------------")

#expect(output.contains(expectedChunk), sourceLocation: sourceLocation)
fatalError("Failed: \(filePath):\(line)")
continue
}

Expand Down
87 changes: 85 additions & 2 deletions Tests/JExtractSwiftTests/JNI/JNIVariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct JNIVariablesTests {
set { }
}
public var someBoolean: Bool
public let isBoolean: Bool
public var isBoolean: Bool
}
"""

Expand Down Expand Up @@ -411,7 +411,7 @@ struct JNIVariablesTests {
}

@Test
func boolean_swiftThunks() throws {
func someBoolean_swiftThunks() throws {
try assertOutput(
input: membersSource,
.jni,
Expand Down Expand Up @@ -450,4 +450,87 @@ struct JNIVariablesTests {
]
)
}

@Test
func isBoolean_javaBindings() throws {
try assertOutput(
input: membersSource,
.jni,
.java,
detectChunkByInitialLines: 8,
expectedChunks: [
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public var isBoolean: Bool
* }
*/
public boolean isBoolean() {
long self$ = this.$memoryAddress();
return MyClass.$isBoolean(self$);
}
""",
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public var isBoolean: Bool
* }
*/
public void setBoolean(boolean newValue) {
long self$ = this.$memoryAddress();
MyClass.$setBoolean(newValue, self$);
}
""",
"""
private static native boolean $isBoolean(long selfPointer);
""",
"""
private static native void $setBoolean(boolean newValue, long selfPointer);
"""
]
)
}

@Test
func isBoolean_swiftThunks() throws {
try assertOutput(
input: membersSource,
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
@_cdecl("Java_com_example_swift_MyClass__00024isBoolean__J")
func Java_com_example_swift_MyClass__00024isBoolean__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) -> jboolean {
guard let env$ = environment else {
fatalError("Missing JNIEnv in downcall to \\(#function)")
}
assert(selfPointer != 0, "selfPointer memory address was null")
let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
fatalError("self memory address was null in call to \\(#function)!")
}
let result = self$.pointee.isBoolean
return result.getJNIValue(in: environment)
}
""",
"""
@_cdecl("Java_com_example_swift_MyClass__00024setBoolean__ZJ")
func Java_com_example_swift_MyClass__00024setBoolean__ZJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jboolean, selfPointer: jlong) {
guard let env$ = environment else {
fatalError("Missing JNIEnv in downcall to \\(#function)")
}
assert(selfPointer != 0, "selfPointer memory address was null")
let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
fatalError("self memory address was null in call to \\(#function)!")
}
self$.pointee.isBoolean = Bool(fromJNI: newValue, in: environment!)
}
"""
]
)
}
}
Loading