diff --git a/Samples/JavaProbablyPrime/Package.swift b/Samples/JavaProbablyPrime/Package.swift new file mode 100644 index 00000000..0420942b --- /dev/null +++ b/Samples/JavaProbablyPrime/Package.swift @@ -0,0 +1,45 @@ +// swift-tools-version: 6.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import CompilerPluginSupport +import PackageDescription + +let package = Package( + name: "JavaProbablyPrime", + platforms: [ + .macOS(.v13), + .iOS(.v13), + .tvOS(.v13), + .watchOS(.v6), + .macCatalyst(.v13), + ], + + products: [ + .executable( + name: "JavaProbablyPrime", + targets: ["JavaProbablyPrime"] + ), + ], + + dependencies: [ + .package(name: "swift-java", path: "../../"), + .package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"), + ], + + targets: [ + .executableTarget( + name: "JavaProbablyPrime", + dependencies: [ + .product(name: "JavaKit", package: "swift-java"), + .product(name: "JavaKitVM", package: "swift-java"), + .product(name: "ArgumentParser", package: "swift-argument-parser"), + ], + swiftSettings: [ + .swiftLanguageMode(.v5) + ], + plugins: [ + .plugin(name: "Java2SwiftPlugin", package: "swift-java"), + ] + ), + ] +) diff --git a/Samples/JavaProbablyPrime/README.md b/Samples/JavaProbablyPrime/README.md new file mode 100644 index 00000000..f9fe318f --- /dev/null +++ b/Samples/JavaProbablyPrime/README.md @@ -0,0 +1,13 @@ +# JavaKit Example: Using Java APIs from Swift + +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: + +``` +swift run JavaProbablyPrime +``` + +The package itself demonstrates how to: + +* Use the Java2Swift build tool plugin to wrap the `java.math.BigInteger` type in Swift. +* Create a `JavaVirtualMachine` instance in a Swift command-line program. +* Create an instance of `BigInteger` in Swift and use its `isProbablyPrime`. diff --git a/Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/Java2Swift.config b/Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/Java2Swift.config new file mode 100644 index 00000000..f73d01df --- /dev/null +++ b/Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/Java2Swift.config @@ -0,0 +1,5 @@ +{ + "classes" : { + "java.math.BigInteger" : "BigInteger" + } +} diff --git a/Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/prime.swift b/Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/prime.swift new file mode 100644 index 00000000..bd83c1ee --- /dev/null +++ b/Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/prime.swift @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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 ArgumentParser +import JavaKit +import JavaKitVM + +@main +struct ProbablyPrime: ParsableCommand { + @Argument(help: "The number to check for primality") + var number: String + + @Option(help: "The certainty to require in the prime check") + var certainty: Int32 = 10 + + func run() throws { + let javaVirtualMachine = try JavaVirtualMachine.shared() + let jniEnvironment = try javaVirtualMachine.environment() + + let bigInt = BigInteger(number, environment: jniEnvironment) + if bigInt.isProbablePrime(certainty) { + print("\(number) is probably prime") + } else { + print("\(number) is definitely not prime") + } + } +} diff --git a/Sources/JavaKitExample/JavaKitExample.swift b/Sources/JavaKitExample/JavaKitExample.swift deleted file mode 100644 index d6eba19d..00000000 --- a/Sources/JavaKitExample/JavaKitExample.swift +++ /dev/null @@ -1,120 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 JavaKit -import JavaRuntime - -enum SwiftWrappedError: Error { - case message(String) -} - -@JavaClass("com.example.swift.HelloSwift") -struct HelloSwift { - @JavaMethod - init(environment: JNIEnvironment) - - @JavaMethod - func sayHelloBack(_ i: Int32) -> Double - - @JavaMethod - func greet(_ name: String) - - @JavaMethod - func doublesToStrings(doubles: [Double]) -> [String] - - @JavaMethod - func throwMessage(message: String) throws - - @JavaField - var value: Double - - @JavaField - var name: String - - @ImplementsJava - 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 strings = doublesToStrings(doubles: [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.super.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(greeting: "Hello from Swift", environment: javaEnvironment) - helloSubFromSwift.greetMe() - - do { - try throwMessage(message: "I am an error") - } catch { - print("Caught Java error: \(error)") - } - - return i * j - } - - @ImplementsJava - func throwMessageFromSwift(message: String) throws -> String { - throw SwiftWrappedError.message(message) - } -} - -extension JavaClass { - @JavaStaticField - var initialValue: Double -} - -@JavaClass("com.example.swift.HelloSubclass", extends: HelloSwift.self) -struct HelloSubclass { - @JavaField - var greeting: String - - @JavaMethod - func greetMe() - - @JavaMethod - init(greeting: String, environment: JNIEnvironment) -} diff --git a/Sources/JavaKitExample/com/example/swift/HelloSubclass.java b/Sources/JavaKitExample/com/example/swift/HelloSubclass.java deleted file mode 100644 index eb3cba32..00000000 --- a/Sources/JavaKitExample/com/example/swift/HelloSubclass.java +++ /dev/null @@ -1,27 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -package com.example.swift; - -public class HelloSubclass extends HelloSwift { - private String greeting; - - public HelloSubclass(String greeting) { - this.greeting = greeting; - } - - private void greetMe() { - super.greet(greeting); - } -} diff --git a/Sources/JavaKitExample/com/example/swift/HelloSwift.java b/Sources/JavaKitExample/com/example/swift/HelloSwift.java deleted file mode 100644 index b4c87f71..00000000 --- a/Sources/JavaKitExample/com/example/swift/HelloSwift.java +++ /dev/null @@ -1,57 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -package com.example.swift; - -public class HelloSwift { - private double value; - private static double initialValue = 3.14159; - private String name = "Java"; - - static { - System.loadLibrary("JavaKitExample"); - } - - public HelloSwift() { - this.value = initialValue; - } - - public native int sayHello(int x, int y); - public native String throwMessageFromSwift(String message) throws Exception; - - // To be called back by the native code - private double sayHelloBack(int i) { - System.out.println("And hello back from " + name + "! You passed me " + i); - return value; - } - - public void greet(String name) { - System.out.println("Salutations, " + name); - } - - String[] doublesToStrings(double[] doubles) { - int size = doubles.length; - String[] strings = new String[size]; - - for(int i = 0; i < size; i++) { - strings[i] = "" + doubles[i]; - } - - return strings; - } - - public void throwMessage(String message) throws Exception { - throw new Exception(message); - } -} diff --git a/Sources/JavaKitExample/com/example/swift/JavaKitSampleMain.java b/Sources/JavaKitExample/com/example/swift/JavaKitSampleMain.java deleted file mode 100644 index 2b565608..00000000 --- a/Sources/JavaKitExample/com/example/swift/JavaKitSampleMain.java +++ /dev/null @@ -1,27 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -package com.example.swift; - -/** - * This sample shows off a {@link HelloSwift} type which is partially implemented in Swift. - * For the Swift implementation refer to - */ -public class JavaKitSampleMain { - - public static void main(String[] args) { - int result = new HelloSubclass("Swift").sayHello(17, 25); - System.out.println("sayHello(17, 25) = " + result); - } -}