|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import JavaKitMacros |
| 16 | +import SwiftSyntax |
| 17 | +import SwiftSyntaxBuilder |
| 18 | +import SwiftSyntaxMacros |
| 19 | +import SwiftSyntaxMacrosTestSupport |
| 20 | +import XCTest |
| 21 | + |
| 22 | +class JavaKitMacroTests: XCTestCase { |
| 23 | + static let javaKitMacros: [String: any Macro.Type] = [ |
| 24 | + "JavaClass": JavaClassMacro.self, |
| 25 | + "JavaMethod": JavaMethodMacro.self, |
| 26 | + "JavaField": JavaFieldMacro.self |
| 27 | + ] |
| 28 | + |
| 29 | + func testJavaClass() throws { |
| 30 | + assertMacroExpansion(""" |
| 31 | + @JavaClass("org.swift.example.HelloWorld") |
| 32 | + public struct HelloWorld { |
| 33 | + @JavaMethod |
| 34 | + public init(_ value: Int32, environment: JNIEnvironment) |
| 35 | +
|
| 36 | + @JavaMethod |
| 37 | + public func isBigEnough(_: Int32) -> Bool |
| 38 | +
|
| 39 | + @JavaField |
| 40 | + public var myField: Int64 |
| 41 | + } |
| 42 | + """, |
| 43 | + expandedSource: """ |
| 44 | +
|
| 45 | + public struct HelloWorld { |
| 46 | + public init(_ value: Int32, environment: JNIEnvironment) { |
| 47 | + self = try! Self.dynamicJavaNewObject(in: environment, arguments: value.self) |
| 48 | + } |
| 49 | + public func isBigEnough(_: Int32) -> Bool { |
| 50 | + return try! dynamicJavaMethodCall(methodName: "isBigEnough", resultType: Bool.self) |
| 51 | + } |
| 52 | + public var myField: Int64 { |
| 53 | + get { |
| 54 | + self[javaFieldName: "myField", fieldType: Int64.self] |
| 55 | + } |
| 56 | + nonmutating set { |
| 57 | + self[javaFieldName: "myField", fieldType: Int64.self] = newValue |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// The full Java class name for this Swift type. |
| 62 | + public static var fullJavaClassName: String { |
| 63 | + "org.swift.example.HelloWorld" |
| 64 | + } |
| 65 | +
|
| 66 | + public typealias JavaSuperclass = JavaObject |
| 67 | +
|
| 68 | + public var javaHolder: JavaObjectHolder |
| 69 | +
|
| 70 | + public var javaThis: jobject { |
| 71 | + javaHolder.object! |
| 72 | + } |
| 73 | +
|
| 74 | + public var javaEnvironment: JNIEnvironment { |
| 75 | + javaHolder.environment |
| 76 | + } |
| 77 | +
|
| 78 | + public init(javaHolder: JavaObjectHolder) { |
| 79 | + self.javaHolder = javaHolder |
| 80 | + } |
| 81 | + } |
| 82 | + """, |
| 83 | + macros: Self.javaKitMacros |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | + |
0 commit comments