forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaImplementationMacroTests.swift
More file actions
257 lines (232 loc) · 7.85 KB
/
JavaImplementationMacroTests.swift
File metadata and controls
257 lines (232 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import SwiftJavaMacros
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest
class JavaImplementationMacroTests: XCTestCase {
static let javaImplementationMacros: [String: any Macro.Type] = [
"JavaImplementation": JavaImplementationMacro.self,
"JavaMethod": JavaMethodMacro.self,
]
func testJNIIdentifierEscaping() throws {
assertMacroExpansion(
"""
@JavaImplementation("org.swift.example.Hello_World")
extension HelloWorld {
@JavaMethod
func test_method() -> Int32 {
return 42
}
}
""",
expandedSource: """
extension HelloWorld {
func test_method() -> Int32 {
return 42
}
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_org_swift_example_Hello_1World_test_1method")
public func __macro_local_22HelloWorld_test_methodfMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
let obj = HelloWorld(javaThis: thisObj, environment: environment!)
return obj.test_method()
.getJNILocalRefValue(in: environment)
}
""",
macros: Self.javaImplementationMacros
)
}
func testJNIIdentifierEscapingWithDots() throws {
assertMacroExpansion(
"""
@JavaImplementation("com.example.test.MyClass")
extension MyClass {
@JavaMethod
func simpleMethod() -> Int32 {
return 1
}
}
""",
expandedSource: """
extension MyClass {
func simpleMethod() -> Int32 {
return 1
}
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_com_example_test_MyClass_simpleMethod")
public func __macro_local_20MyClass_simpleMethodfMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
let obj = MyClass(javaThis: thisObj, environment: environment!)
return obj.simpleMethod()
.getJNILocalRefValue(in: environment)
}
""",
macros: Self.javaImplementationMacros
)
}
func testJNIIdentifierEscapingStaticMethod() throws {
assertMacroExpansion(
"""
@JavaImplementation("org.example.Utils")
extension Utils {
@JavaMethod
static func static_helper(environment: JNIEnvironment) -> String {
return "hello"
}
}
""",
expandedSource: """
extension Utils {
static func static_helper(environment: JNIEnvironment) -> String {
return "hello"
}
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_org_example_Utils_static_1helper")
public func __macro_local_19Utils_static_helperfMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> String.JNIType {
return Utils.static_helper(environment: environment)
.getJNILocalRefValue(in: environment)
}
""",
macros: Self.javaImplementationMacros
)
}
func testJNIIdentifierEscapingMultipleMethods() throws {
assertMacroExpansion(
"""
@JavaImplementation("test.Class_With_Underscores")
extension ClassWithUnderscores {
@JavaMethod
func method_one() -> Int32 {
return 1
}
@JavaMethod
func method_two() -> Int32 {
return 2
}
}
""",
expandedSource: """
extension ClassWithUnderscores {
func method_one() -> Int32 {
return 1
}
func method_two() -> Int32 {
return 2
}
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_test_Class_1With_1Underscores_method_1one")
public func __macro_local_31ClassWithUnderscores_method_onefMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
let obj = ClassWithUnderscores(javaThis: thisObj, environment: environment!)
return obj.method_one()
.getJNILocalRefValue(in: environment)
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_test_Class_1With_1Underscores_method_1two")
public func __macro_local_31ClassWithUnderscores_method_twofMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
let obj = ClassWithUnderscores(javaThis: thisObj, environment: environment!)
return obj.method_two()
.getJNILocalRefValue(in: environment)
}
""",
macros: Self.javaImplementationMacros
)
}
func testJNIIdentifierEscapingWithJavaMethodNameOverride() throws {
assertMacroExpansion(
"""
@JavaImplementation("org.swift.swiftkit.core.collections.SwiftDictionaryMap")
extension SwiftDictionaryMapJava {
@JavaMethod("$size")
public static func _size(environment: UnsafeMutablePointer<JNIEnv?>!, pointer: Int64) -> Int32 {
return 42
}
@JavaMethod("$destroy")
public static func _destroy(environment: UnsafeMutablePointer<JNIEnv?>!, pointer: Int64) {
// cleanup
}
}
""",
expandedSource: """
extension SwiftDictionaryMapJava {
public static func _size(environment: UnsafeMutablePointer<JNIEnv?>!, pointer: Int64) -> Int32 {
return 42
}
public static func _destroy(environment: UnsafeMutablePointer<JNIEnv?>!, pointer: Int64) {
// cleanup
}
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_org_swift_swiftkit_core_collections_SwiftDictionaryMap__00024size")
public func __macro_local_28SwiftDictionaryMapJava__sizefMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, pointer: Int64.JNIType) -> Int32.JNIType {
return SwiftDictionaryMapJava._size(environment: environment, pointer: Int64(fromJNI: pointer, in: environment!))
.getJNILocalRefValue(in: environment)
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_org_swift_swiftkit_core_collections_SwiftDictionaryMap__00024destroy")
public func __macro_local_31SwiftDictionaryMapJava__destroyfMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, pointer: Int64.JNIType) {
return SwiftDictionaryMapJava._destroy(environment: environment, pointer: Int64(fromJNI: pointer, in: environment!))
}
""",
macros: Self.javaImplementationMacros
)
}
func testJNIIdentifierEscapingVoidReturn() throws {
assertMacroExpansion(
"""
@JavaImplementation("org.example.Processor")
extension Processor {
@JavaMethod
func process_data() {
// do nothing
}
}
""",
expandedSource: """
extension Processor {
func process_data() {
// do nothing
}
}
#if compiler(>=6.3)
@used
#endif
@_cdecl("Java_org_example_Processor_process_1data")
public func __macro_local_22Processor_process_datafMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) {
let obj = Processor(javaThis: thisObj, environment: environment!)
return obj.process_data()
}
""",
macros: Self.javaImplementationMacros
)
}
}