Skip to content

Commit edd0ca5

Browse files
committed
Update structure of docs to use snippets
1 parent b720e5e commit edd0ca5

129 files changed

Lines changed: 1561 additions & 803 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Samples/JavaDependencySampleApp/Sources/JavaDependencySample/main.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ print("Start Sample app...")
2828
// TODO: locating the classpath is more complex, need to account for dependencies of our module
2929
let swiftJavaClasspath = findSwiftJavaClasspaths() // scans for .classpath files
3030

31+
// snippet.dependencyUsage
3132
// 1) Start a JVM with appropriate classpath
3233
let jvm = try JavaVirtualMachine.shared(classpath: swiftJavaClasspath)
3334

@@ -50,5 +51,6 @@ for record in try CSVFormatClass.RFC4180.parse(reader)!.getRecords()! {
5051
print("Field: \(field)")
5152
}
5253
}
54+
// snippet.end
5355

5456
print("Done.")

‎Samples/JavaKitSampleApp/Sources/JavaKitExample/JavaKitExample.swift‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum SwiftWrappedError: Error {
1919
case message(String)
2020
}
2121

22+
// snippet.implementation
2223
@JavaImplementation("com.example.swift.HelloSwift")
2324
extension HelloSwift: HelloSwiftNativeMethods {
2425
@JavaMethod
@@ -27,20 +28,24 @@ extension HelloSwift: HelloSwiftNativeMethods {
2728
let answer = self.sayHelloBack(i + j)
2829
print("Swift got back \(answer) from Java")
2930

31+
// snippet.staticFieldAccess
3032
print("We expect the above value to be the initial value, \(self.javaClass.initialValue)")
33+
// snippet.end
3134

3235
print("Updating Java field value to something different")
3336
self.value = 2.71828
3437

3538
let newAnswer = self.sayHelloBack(17)
3639
print("Swift got back updated \(newAnswer) from Java")
3740

41+
// snippet.classDefinition
3842
let newHello = HelloSwift(environment: javaEnvironment)
3943
print("Swift created a new Java instance with the value \(newHello.value)")
4044

4145
let name = newHello.name
4246
print("Hello to \(name)")
4347
newHello.greet("Swift 👋🏽 How's it going")
48+
// snippet.end
4449

4550
self.name = "a 🗑️-collected language"
4651
_ = self.sayHelloBack(42)
@@ -49,9 +54,12 @@ extension HelloSwift: HelloSwiftNativeMethods {
4954
let value = predicate.test(JavaInteger(3))
5055
print("Running a JavaPredicate from swift 3 < 10 = \(value)")
5156

57+
// snippet.arraysWrapper
5258
let strings = doublesToStrings([3.14159, 2.71828])
5359
print("Converting doubles to strings: \(strings)")
60+
// snippet.end
5461

62+
// snippet.castPattern
5563
// Try downcasting
5664
if let helloSub = self.as(HelloSubclass.self) {
5765
print("Hello from the subclass!")
@@ -61,6 +69,7 @@ extension HelloSwift: HelloSwiftNativeMethods {
6169
} else {
6270
fatalError("Expected subclass here")
6371
}
72+
// snippet.end
6473

6574
// Check escaped name
6675
assert(self.`init`(42) == 42)
@@ -70,25 +79,34 @@ extension HelloSwift: HelloSwiftNativeMethods {
7079
assert(newHello.is(HelloSwift.self))
7180
assert(!newHello.is(HelloSubclass.self))
7281

73-
// Create a new instance.
82+
// snippet.inheritance
83+
// Create a new instance of the subclass; Swift mirrors the Java hierarchy.
7484
let helloSubFromSwift = HelloSubclass("Hello from Swift", environment: javaEnvironment)
7585
helloSubFromSwift.greetMe()
86+
// snippet.end
7687

88+
// snippet.throwingMethods
7789
do {
7890
try throwMessage("I am an error")
7991
} catch {
8092
print("Caught Java error: \(error)")
8193
}
94+
// snippet.end
8295

83-
// Make sure that the thread safe class is sendable
96+
// snippet.sendableConformance
97+
// Java classes annotated with @ThreadSafe surface as Sendable on the Swift side.
8498
let helper = ThreadSafeHelperClass(environment: javaEnvironment)
8599
let threadSafe: Sendable = helper
100+
_ = threadSafe
101+
// snippet.end
86102

87103
checkOptionals(helper: helper)
88104

89105
return i * j
90106
}
107+
// snippet.end
91108

109+
// snippet.optionalsWrapper
92110
func checkOptionals(helper: ThreadSafeHelperClass) {
93111
let text: JavaString? = helper.textOptional
94112
let value: String? = helper.getValueOptional(Optional<JavaString>.none)
@@ -101,6 +119,7 @@ extension HelloSwift: HelloSwiftNativeMethods {
101119
print("Optional double function returned \(doubleOpt)")
102120
print("Optional long function returned \(longOpt)")
103121
}
122+
// snippet.end
104123

105124
@JavaMethod
106125
func throwMessageFromSwift(_ message: String) throws -> String {

‎Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloJavaKitArrays.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.example.swift;
1616

17+
// snippet.arrays
1718
public class HelloJavaKitArrays {
1819

1920
public byte[] getFixedBytes() {
@@ -57,3 +58,4 @@ public String[] getGreetings() {
5758
return new String[] { "hello", "world", "from", "java" };
5859
}
5960
}
61+
// snippet.end

‎Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSubclass.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.example.swift;
1616

17+
// snippet.helloSubclass
1718
public class HelloSubclass extends HelloSwift {
1819
private String greeting;
1920

@@ -25,3 +26,4 @@ public void greetMe() {
2526
super.greet(greeting);
2627
}
2728
}
29+
// snippet.end

‎Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.util.function.Predicate;
1818

19+
// snippet.helloClass
1920
public class HelloSwift {
2021
public double value;
2122
public static double initialValue = 3.14159;
@@ -71,3 +72,4 @@ public void throwMessage(String message) throws Exception {
7172
throw new Exception(message);
7273
}
7374
}
75+
// snippet.end

‎Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/ThreadSafe.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.lang.annotation.Retention;
1818
import java.lang.annotation.RetentionPolicy;
1919

20+
// snippet.threadSafe
2021
@Retention(RetentionPolicy.RUNTIME)
2122
public @interface ThreadSafe {
2223
}
24+
// snippet.end

‎Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/ThreadSafeHelperClass.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.OptionalInt;
2020
import java.util.OptionalDouble;
2121

22+
// snippet.threadSafeHelper
2223
@ThreadSafe
2324
public class ThreadSafeHelperClass {
2425
public ThreadSafeHelperClass() { }
@@ -53,3 +54,4 @@ public OptionalLong from(OptionalInt value) {
5354
return OptionalLong.of(value.getAsInt());
5455
}
5556
}
57+
// snippet.end

‎Samples/JavaKitSampleApp/Tests/JavaKitExampleTests/JavaKitArrayRuntimeTests.swift‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct JavaKitArrayRuntimeTests {
2121

2222
let jvm = try JavaKitSampleJVM.shared
2323

24+
// snippet.arraysUsage
2425
@Test
2526
func getFixedBytes() throws {
2627
let env = try jvm.environment()
@@ -31,30 +32,40 @@ struct JavaKitArrayRuntimeTests {
3132
}
3233

3334
@Test
34-
func getEmptyBytes() throws {
35+
func reverseBytes() throws {
3536
let env = try jvm.environment()
3637
let arrays = HelloJavaKitArrays(environment: env)
3738

38-
let bytes: [Int8] = arrays.getEmptyBytes()
39-
#expect(bytes.isEmpty)
39+
let reversed: [Int8] = arrays.reverseBytes([10, 20, 30])
40+
#expect(reversed == [30, 20, 10])
4041
}
4142

4243
@Test
43-
func filledBytes() throws {
44+
func getGreetings() throws {
4445
let env = try jvm.environment()
4546
let arrays = HelloJavaKitArrays(environment: env)
4647

47-
let bytes: [Int8] = arrays.filledBytes(4, 42)
48-
#expect(bytes == [42, 42, 42, 42])
48+
let greetings: [String] = arrays.getGreetings()
49+
#expect(greetings == ["hello", "world", "from", "java"])
4950
}
51+
// snippet.end
5052

5153
@Test
52-
func reverseBytes() throws {
54+
func getEmptyBytes() throws {
5355
let env = try jvm.environment()
5456
let arrays = HelloJavaKitArrays(environment: env)
5557

56-
let reversed: [Int8] = arrays.reverseBytes([10, 20, 30])
57-
#expect(reversed == [30, 20, 10])
58+
let bytes: [Int8] = arrays.getEmptyBytes()
59+
#expect(bytes.isEmpty)
60+
}
61+
62+
@Test
63+
func filledBytes() throws {
64+
let env = try jvm.environment()
65+
let arrays = HelloJavaKitArrays(environment: env)
66+
67+
let bytes: [Int8] = arrays.filledBytes(4, 42)
68+
#expect(bytes == [42, 42, 42, 42])
5869
}
5970

6071
@Test
@@ -84,13 +95,4 @@ struct JavaKitArrayRuntimeTests {
8495
// "Hi" in UTF-8 is [0x48, 0x69]
8596
#expect(bytes == [0x48, 0x69])
8697
}
87-
88-
@Test
89-
func getGreetings() throws {
90-
let env = try jvm.environment()
91-
let arrays = HelloJavaKitArrays(environment: env)
92-
93-
let greetings: [String] = arrays.getGreetings()
94-
#expect(greetings == ["hello", "world", "from", "java"])
95-
}
9698
}

‎Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/prime.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ struct ProbablyPrime: ParsableCommand {
2424
var certainty: Int32 = 10
2525

2626
func run() throws {
27+
// snippet.probablyPrime
2728
let bigInt = BigInteger(number)
2829
if bigInt.isProbablePrime(certainty) {
2930
print("\(number) is probably prime")
3031
} else {
3132
print("\(number) is definitely not prime")
3233
}
34+
// snippet.end
3335
}
3436
}

‎Samples/JavaSieve/Sources/JavaSieve/main.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import SwiftJava
1717

1818
let jvm = try JavaVirtualMachine.shared()
1919

20+
// snippet.sieveUsage
2021
do {
2122
let sieveClass = try JavaClass<SieveOfEratosthenes>(environment: jvm.environment())
2223
for prime in sieveClass.findPrimes(100)! {
@@ -27,3 +28,4 @@ do {
2728
} catch {
2829
print("Failure: \(error)")
2930
}
31+
// snippet.end

0 commit comments

Comments
 (0)