Skip to content

Commit 43a91c2

Browse files
authored
Add a JavaKit sample that produces a Swift command-line app using Java APIs (#79)
* Remove stale JavaKitExample sources * Add a JavaKit sample that produces a Swift command-line app using Java APIs This demonstrates how to pull in just one Java type (java.math.BigInteger) and use it from a Swift program (a command line app).
1 parent f041f10 commit 43a91c2

File tree

8 files changed

+101
-231
lines changed

8 files changed

+101
-231
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// swift-tools-version: 6.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import CompilerPluginSupport
5+
import PackageDescription
6+
7+
let package = Package(
8+
name: "JavaProbablyPrime",
9+
platforms: [
10+
.macOS(.v13),
11+
.iOS(.v13),
12+
.tvOS(.v13),
13+
.watchOS(.v6),
14+
.macCatalyst(.v13),
15+
],
16+
17+
products: [
18+
.executable(
19+
name: "JavaProbablyPrime",
20+
targets: ["JavaProbablyPrime"]
21+
),
22+
],
23+
24+
dependencies: [
25+
.package(name: "swift-java", path: "../../"),
26+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
27+
],
28+
29+
targets: [
30+
.executableTarget(
31+
name: "JavaProbablyPrime",
32+
dependencies: [
33+
.product(name: "JavaKit", package: "swift-java"),
34+
.product(name: "JavaKitVM", package: "swift-java"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
36+
],
37+
swiftSettings: [
38+
.swiftLanguageMode(.v5)
39+
],
40+
plugins: [
41+
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
42+
]
43+
),
44+
]
45+
)

Samples/JavaProbablyPrime/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# JavaKit Example: Using Java APIs from Swift
2+
3+
This package contains an example program that uses Java's [`java.math.BigInteger`](https://docs.oracle.com/javase/8/docs/api/?java/math/BigInteger.html) from Swift to determine whether a given number is probably prime. You can try it out with your own very big number:
4+
5+
```
6+
swift run JavaProbablyPrime <very big number>
7+
```
8+
9+
The package itself demonstrates how to:
10+
11+
* Use the Java2Swift build tool plugin to wrap the `java.math.BigInteger` type in Swift.
12+
* Create a `JavaVirtualMachine` instance in a Swift command-line program.
13+
* Create an instance of `BigInteger` in Swift and use its `isProbablyPrime`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"classes" : {
3+
"java.math.BigInteger" : "BigInteger"
4+
}
5+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 ArgumentParser
16+
import JavaKit
17+
import JavaKitVM
18+
19+
@main
20+
struct ProbablyPrime: ParsableCommand {
21+
@Argument(help: "The number to check for primality")
22+
var number: String
23+
24+
@Option(help: "The certainty to require in the prime check")
25+
var certainty: Int32 = 10
26+
27+
func run() throws {
28+
let javaVirtualMachine = try JavaVirtualMachine.shared()
29+
let jniEnvironment = try javaVirtualMachine.environment()
30+
31+
let bigInt = BigInteger(number, environment: jniEnvironment)
32+
if bigInt.isProbablePrime(certainty) {
33+
print("\(number) is probably prime")
34+
} else {
35+
print("\(number) is definitely not prime")
36+
}
37+
}
38+
}

Sources/JavaKitExample/JavaKitExample.swift

Lines changed: 0 additions & 120 deletions
This file was deleted.

Sources/JavaKitExample/com/example/swift/HelloSubclass.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

Sources/JavaKitExample/com/example/swift/HelloSwift.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

Sources/JavaKitExample/com/example/swift/JavaKitSampleMain.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)