Skip to content

Commit a9cacd3

Browse files
NakaokaReiclaude
andcommitted
fix: use unsafeBitCast for thisObj in @JavaImplementation macro
In C++ interop mode, jobject is UnsafeMutablePointer<_jobject>, but the swift-java library (compiled without C++ interop) expects jobject as OpaquePointer. Use unsafeBitCast to ensure compatibility in both modes since both have the same memory representation (a pointer). This fixes the "cannot convert value of type 'jobject' to expected argument type 'OpaquePointer'" error when building sample apps with C++ interoperability enabled. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 9cd3191 commit a9cacd3

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Sources/SwiftJavaMacros/ImplementsJavaMacro.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,13 @@ extension JavaImplementationMacro: PeerMacro {
128128
return \(raw: tryClause)\(raw: swiftTypeName).\(raw: swiftName)(\(raw: swiftArguments.map { $0.description }.joined(separator: ", ")))\(raw: getJNIValue)
129129
"""
130130
} else {
131+
// Use unsafeBitCast to convert thisObj to OpaquePointer for C++ interoperability.
132+
// In C++ mode, jobject is UnsafeMutablePointer<_jobject>, but the library
133+
// (compiled without C++ interop) expects jobject as OpaquePointer.
134+
// The bitcast ensures compatibility in both modes since both have the same
135+
// memory representation (a pointer).
131136
innerBody = """
132-
let obj = \(raw: swiftTypeName)(javaThis: thisObj, environment: environment!)
137+
let obj = \(raw: swiftTypeName)(javaThis: unsafeBitCast(thisObj, to: OpaquePointer.self), environment: environment!)
133138
return \(raw: tryClause)obj.\(raw: swiftName)(\(raw: swiftArguments.map { $0.description }.joined(separator: ", ")))\(raw: getJNIValue)
134139
"""
135140
}

0 commit comments

Comments
 (0)