forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaKitExample.swift
More file actions
105 lines (84 loc) · 3.3 KB
/
JavaKitExample.swift
File metadata and controls
105 lines (84 loc) · 3.3 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
//===----------------------------------------------------------------------===//
//
// 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 JavaUtilFunction
import SwiftJava
enum SwiftWrappedError: Error {
case message(String)
}
@JavaImplementation("com.example.swift.HelloSwift")
extension HelloSwift: HelloSwiftNativeMethods {
@JavaMethod
func sayHello(_ i: Int32, _ j: Int32) -> Int32 {
print("Hello from Swift!")
let answer = self.sayHelloBack(i + j)
print("Swift got back \(answer) from Java")
print("We expect the above value to be the initial value, \(self.javaClass.initialValue)")
print("Updating Java field value to something different")
self.value = 2.71828
let newAnswer = self.sayHelloBack(17)
print("Swift got back updated \(newAnswer) from Java")
let newHello = HelloSwift(environment: javaEnvironment)
print("Swift created a new Java instance with the value \(newHello.value)")
let name = newHello.name
print("Hello to \(name)")
newHello.greet("Swift 👋🏽 How's it going")
self.name = "a 🗑️-collected language"
_ = self.sayHelloBack(42)
let predicate: JavaPredicate<JavaInteger> = self.lessThanTen()!
let value = predicate.test(JavaInteger(3))
print("Running a JavaPredicate from swift 3 < 10 = \(value)")
let strings = doublesToStrings([3.14159, 2.71828])
print("Converting doubles to strings: \(strings)")
// Try downcasting
if let helloSub = self.as(HelloSubclass.self) {
print("Hello from the subclass!")
helloSub.greetMe()
assert(helloSub.value == 2.71828)
} else {
fatalError("Expected subclass here")
}
// Check "is" behavior
assert(newHello.is(HelloSwift.self))
assert(!newHello.is(HelloSubclass.self))
// Create a new instance.
let helloSubFromSwift = HelloSubclass("Hello from Swift", environment: javaEnvironment)
helloSubFromSwift.greetMe()
do {
try throwMessage("I am an error")
} catch {
print("Caught Java error: \(error)")
}
// Make sure that the thread safe class is sendable
let helper = ThreadSafeHelperClass(environment: javaEnvironment)
let threadSafe: Sendable = helper
checkOptionals(helper: helper)
return i * j
}
func checkOptionals(helper: ThreadSafeHelperClass) {
let text: JavaString? = helper.textOptional
let value: String? = helper.getValueOptional(Optional<JavaString>.none)
let textFunc: JavaString? = helper.getTextOptional()
let doubleOpt: Double? = helper.valOptional
let longOpt: Int64? = helper.fromOptional(21 as Int32?)
print("Optional text = \(text)")
print("Optional string value = \(value)")
print("Optional text function returned \(textFunc)")
print("Optional double function returned \(doubleOpt)")
print("Optional long function returned \(longOpt)")
}
@JavaMethod
func throwMessageFromSwift(_ message: String) throws -> String {
throw SwiftWrappedError.message(message)
}
}