forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaClassMacroTests.swift
119 lines (104 loc) · 3.81 KB
/
JavaClassMacroTests.swift
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
//===----------------------------------------------------------------------===//
//
// 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 JavaKitMacros
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest
class JavaKitMacroTests: XCTestCase {
static let javaKitMacros: [String: any Macro.Type] = [
"JavaClass": JavaClassMacro.self,
"JavaMethod": JavaMethodMacro.self,
"JavaField": JavaFieldMacro.self
]
func testJavaClass() throws {
assertMacroExpansion("""
@JavaClass("org.swift.example.HelloWorld")
public struct HelloWorld {
@JavaMethod
public init(environment: JNIEnvironment? = nil)
@JavaMethod
public init(_ value: Int32, environment: JNIEnvironment? = nil)
@JavaMethod
public func isBigEnough(_: Int32) -> Bool
@JavaField
public var myField: Int64
@JavaField
public var objectField: JavaObject!
@JavaField(isFinal: true)
public var myFinalField: Int64
}
""",
expandedSource: """
public struct HelloWorld {
public init(environment: JNIEnvironment? = nil) {
let _environment = if let environment {
environment
} else {
try! JavaVirtualMachine.shared().environment()
}
self = try! Self.dynamicJavaNewObject(in: _environment)
}
public init(_ value: Int32, environment: JNIEnvironment? = nil) {
let _environment = if let environment {
environment
} else {
try! JavaVirtualMachine.shared().environment()
}
self = try! Self.dynamicJavaNewObject(in: _environment, arguments: value.self)
}
public func isBigEnough(_: Int32) -> Bool {
return try! dynamicJavaMethodCall(methodName: "isBigEnough", resultType: Bool.self)
}
public var myField: Int64 {
get {
self[javaFieldName: "myField", fieldType: Int64.self]
}
nonmutating set {
self[javaFieldName: "myField", fieldType: Int64.self] = newValue
}
}
public var objectField: JavaObject! {
get {
self[javaFieldName: "objectField", fieldType: JavaObject?.self]
}
nonmutating set {
self[javaFieldName: "objectField", fieldType: JavaObject?.self] = newValue
}
}
public var myFinalField: Int64 {
get {
self[javaFieldName: "myFinalField", fieldType: Int64.self]
}
}
/// The full Java class name for this Swift type.
public static var fullJavaClassName: String {
"org.swift.example.HelloWorld"
}
public typealias JavaSuperclass = JavaObject
public var javaHolder: JavaObjectHolder
public init(javaHolder: JavaObjectHolder) {
self.javaHolder = javaHolder
}
/// Casting to ``JavaObject`` will never be nil because ``HelloWorld`` extends it.
public func `as`(_: JavaObject.Type) -> JavaObject {
return JavaObject(javaHolder: javaHolder)
}
}
""",
macros: Self.javaKitMacros
)
}
}