Skip to content

Commit 9492423

Browse files
committed
bring back javakit example
1 parent 2890625 commit 9492423

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 JavaKit
16+
import JavaRuntime
17+
18+
enum SwiftWrappedError: Error {
19+
case message(String)
20+
}
21+
22+
@JavaClass("com.example.swift.HelloSwift")
23+
struct HelloSwift {
24+
@JavaMethod
25+
init(environment: JNIEnvironment)
26+
27+
@JavaMethod
28+
func sayHelloBack(_ i: Int32) -> Double
29+
30+
@JavaMethod
31+
func greet(_ name: String)
32+
33+
@JavaMethod
34+
func doublesToStrings(doubles: [Double]) -> [String]
35+
36+
@JavaMethod
37+
func throwMessage(message: String) throws
38+
39+
@JavaField
40+
var value: Double
41+
42+
@JavaField
43+
var name: String
44+
45+
@ImplementsJava
46+
func sayHello(i: Int32, _ j: Int32) -> Int32 {
47+
print("Hello from Swift!")
48+
let answer = self.sayHelloBack(i + j)
49+
print("Swift got back \(answer) from Java")
50+
51+
print("We expect the above value to be the initial value, \(self.javaClass.initialValue)")
52+
53+
print("Updating Java field value to something different")
54+
self.value = 2.71828
55+
56+
let newAnswer = self.sayHelloBack(17)
57+
print("Swift got back updated \(newAnswer) from Java")
58+
59+
let newHello = HelloSwift(environment: javaEnvironment)
60+
print("Swift created a new Java instance with the value \(newHello.value)")
61+
62+
let name = newHello.name
63+
print("Hello to \(name)")
64+
newHello.greet("Swift 👋🏽 How's it going")
65+
66+
self.name = "a 🗑️-collected language"
67+
_ = self.sayHelloBack(42)
68+
69+
let strings = doublesToStrings(doubles: [3.14159, 2.71828])
70+
print("Converting doubles to strings: \(strings)")
71+
72+
// Try downcasting
73+
if let helloSub = self.as(HelloSubclass.self) {
74+
print("Hello from the subclass!")
75+
helloSub.greetMe()
76+
77+
assert(helloSub.super.value == 2.71828)
78+
} else {
79+
fatalError("Expected subclass here")
80+
}
81+
82+
// Check "is" behavior
83+
assert(newHello.is(HelloSwift.self))
84+
assert(!newHello.is(HelloSubclass.self))
85+
86+
// Create a new instance.
87+
let helloSubFromSwift = HelloSubclass(greeting: "Hello from Swift", environment: javaEnvironment)
88+
helloSubFromSwift.greetMe()
89+
90+
do {
91+
try throwMessage(message: "I am an error")
92+
} catch {
93+
print("Caught Java error: \(error)")
94+
}
95+
96+
return i * j
97+
}
98+
99+
@ImplementsJava
100+
func throwMessageFromSwift(message: String) throws -> String {
101+
throw SwiftWrappedError.message(message)
102+
}
103+
}
104+
105+
extension JavaClass<HelloSwift> {
106+
@JavaField
107+
var initialValue: Double
108+
}
109+
110+
@JavaClass("com.example.swift.HelloSubclass", extends: HelloSwift.self)
111+
struct HelloSubclass {
112+
@JavaField
113+
var greeting: String
114+
115+
@JavaMethod
116+
func greetMe()
117+
118+
@JavaMethod
119+
init(greeting: String, environment: JNIEnvironment)
120+
}

0 commit comments

Comments
 (0)