Skip to content

Minor cleanups to the JavaKit module and samples #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"classes" : {
"java.util.ArrayList" : "ArrayList",
"com.example.swift.HelloSwift" : "HelloSwift",
"com.example.swift.HelloSubclass" : "HelloSubclass",
"com.example.swift.JavaKitSampleMain" : "JavaKitSampleMain"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,3 @@ extension HelloSwift: HelloSwiftNativeMethods {
throw SwiftWrappedError.message(message)
}
}

func removeLast(arrayList: ArrayList<JavaClass<HelloSwift>>) {
if let lastObject = arrayList.getLast() {
_ = arrayList.remove(lastObject)
}
}
1 change: 1 addition & 0 deletions Samples/JavaProbablyPrime/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let package = Package(
.executableTarget(
name: "JavaProbablyPrime",
dependencies: [
.product(name: "JavaKitCollection", package: "swift-java"),
.product(name: "JavaKit", package: "swift-java"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
Expand Down
1 change: 0 additions & 1 deletion Samples/JavaProbablyPrime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ swift run JavaProbablyPrime <very big number>
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`.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ struct ProbablyPrime: ParsableCommand {
var certainty: Int32 = 10

func run() throws {
let javaVirtualMachine = try JavaVirtualMachine.shared()
let jniEnvironment = try javaVirtualMachine.environment()

let bigInt = BigInteger(number, environment: jniEnvironment)
let bigInt = BigInteger(number)
if bigInt.isProbablePrime(certainty) {
print("\(number) is probably prime")
} else {
Expand Down
3 changes: 1 addition & 2 deletions Samples/JavaSieve/Sources/JavaMath/Java2Swift.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"java.math.BigDecimal" : "BigDecimal",
"java.math.BigInteger" : "BigInteger",
"java.math.MathContext" : "MathContext",
"java.math.RoundingMode" : "RoundingMode",
"java.lang.Integer" : "JavaInteger",
"java.math.RoundingMode" : "RoundingMode"
}
}
3 changes: 1 addition & 2 deletions Samples/JavaSieve/Sources/JavaSieve/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
//===----------------------------------------------------------------------===//

import JavaKit
import JavaKitVM

let jvm = try JavaVirtualMachine.shared(classPath: ["QuadraticSieve-1.0.jar"])
do {
let sieveClass = try JavaClass<SieveOfEratosthenes>(in: jvm.environment())
let sieveClass = try JavaClass<SieveOfEratosthenes>(environment: jvm.environment())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, abusing the in: as label is always a tad too much in Swift libs 😆

for prime in sieveClass.findPrimes(100)! {
print("Found prime: \(prime.intValue())")
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Java2Swift/JavaToSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ struct JavaToSwift: ParsableCommand {
environment: environment
)
#else
let classLoader = try JavaClass<ClassLoader>(in: environment)
let classLoader = try JavaClass<ClassLoader>(environment: environment)
.getSystemClassLoader()!
#endif
var javaClasses: [JavaClass<JavaObject>] = []
Expand Down
2 changes: 1 addition & 1 deletion Sources/Java2SwiftLib/JavaTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ extension JavaTranslator {
} else {
try! JavaVirtualMachine.shared().environment()
}
let classObj = try! JavaClass<Self>(in: _environment)
let classObj = try! JavaClass<Self>(environment: _environment)
switch enumValue {
\(raw: enumFields.map {
return """
Expand Down
13 changes: 12 additions & 1 deletion Sources/JavaKit/Java2Swift.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
{
"classes" : {
"java.lang.Boolean" : "JavaBoolean",
"java.lang.Byte" : "JavaByte",
"java.lang.Character" : "JavaCharacter",
"java.lang.Class" : "JavaClass",
"java.lang.Double" : "JavaDouble",
"java.lang.Error" : "JavaError",
"java.lang.Exception" : "Exception",
"java.lang.Float" : "JavaFloat",
"java.lang.Integer" : "JavaInteger",
"java.lang.Long" : "JavaLong",
"java.lang.Number" : "JavaNumber",
"java.lang.Object" : "JavaObject",
"java.lang.RuntimeException" : "RuntimeException",
"java.lang.Throwable" : "Throwable"
"java.lang.Short" : "JavaShort",
"java.lang.Throwable" : "Throwable",
"java.lang.Void" : "JavaVoid"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

import JavaRuntime

/// Wrapper around a Java class that provides access to the static members of
/// the class.
@JavaClass("java.lang.Class")
public struct JavaClass<ObjectType: AnyJavaObject> {
extension JavaClass {
public typealias ObjectType = T

/// Lookup this Java class within the given environment.
public init(in environment: JNIEnvironment) throws {
public init(environment: JNIEnvironment? = nil) throws {
let environment = try environment ?? JavaVirtualMachine.shared().environment()
self.init(
javaThis: try ObjectType.getJNIClass(in: environment),
environment: environment
Expand Down
87 changes: 87 additions & 0 deletions Sources/JavaKit/generated/JavaBoolean.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Auto-generated by Java-to-Swift wrapper generator.
import JavaRuntime

@JavaClass("java.lang.Boolean")
public struct JavaBoolean {
@JavaMethod
public init(_ arg0: Bool, environment: JNIEnvironment? = nil)

@JavaMethod
public init(_ arg0: String, environment: JNIEnvironment? = nil)

@JavaMethod
public func equals(_ arg0: JavaObject?) -> Bool

@JavaMethod
public func toString() -> String

@JavaMethod
public func hashCode() -> Int32

@JavaMethod
public func compareTo(_ arg0: JavaBoolean?) -> Int32

@JavaMethod
public func compareTo(_ arg0: JavaObject?) -> Int32

@JavaMethod
public func booleanValue() -> Bool

@JavaMethod
public func getClass() -> JavaClass<JavaObject>?

@JavaMethod
public func notify()

@JavaMethod
public func notifyAll()

@JavaMethod
public func wait(_ arg0: Int64) throws

@JavaMethod
public func wait(_ arg0: Int64, _ arg1: Int32) throws

@JavaMethod
public func wait() throws
}
extension JavaClass<JavaBoolean> {
@JavaStaticField
public var TRUE: JavaBoolean?

@JavaStaticField
public var FALSE: JavaBoolean?

@JavaStaticField
public var TYPE: JavaClass<JavaBoolean>?

@JavaStaticMethod
public func toString(_ arg0: Bool) -> String

@JavaStaticMethod
public func hashCode(_ arg0: Bool) -> Int32

@JavaStaticMethod
public func getBoolean(_ arg0: String) -> Bool

@JavaStaticMethod
public func compare(_ arg0: Bool, _ arg1: Bool) -> Int32

@JavaStaticMethod
public func valueOf(_ arg0: String) -> JavaBoolean?

@JavaStaticMethod
public func valueOf(_ arg0: Bool) -> JavaBoolean?

@JavaStaticMethod
public func parseBoolean(_ arg0: String) -> Bool

@JavaStaticMethod
public func logicalAnd(_ arg0: Bool, _ arg1: Bool) -> Bool

@JavaStaticMethod
public func logicalOr(_ arg0: Bool, _ arg1: Bool) -> Bool

@JavaStaticMethod
public func logicalXor(_ arg0: Bool, _ arg1: Bool) -> Bool
}
114 changes: 114 additions & 0 deletions Sources/JavaKit/generated/JavaByte.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Auto-generated by Java-to-Swift wrapper generator.
import JavaRuntime

@JavaClass("java.lang.Byte", extends: JavaNumber.self)
public struct JavaByte {
@JavaMethod
public init(_ arg0: Int8, environment: JNIEnvironment? = nil)

@JavaMethod
public init(_ arg0: String, environment: JNIEnvironment? = nil) throws

@JavaMethod
public func equals(_ arg0: JavaObject?) -> Bool

@JavaMethod
public func toString() -> String

@JavaMethod
public func hashCode() -> Int32

@JavaMethod
public func compareTo(_ arg0: JavaByte?) -> Int32

@JavaMethod
public func compareTo(_ arg0: JavaObject?) -> Int32

@JavaMethod
public func byteValue() -> Int8

@JavaMethod
public func shortValue() -> Int16

@JavaMethod
public func intValue() -> Int32

@JavaMethod
public func longValue() -> Int64

@JavaMethod
public func floatValue() -> Float

@JavaMethod
public func doubleValue() -> Double

@JavaMethod
public func getClass() -> JavaClass<JavaObject>?

@JavaMethod
public func notify()

@JavaMethod
public func notifyAll()

@JavaMethod
public func wait(_ arg0: Int64) throws

@JavaMethod
public func wait(_ arg0: Int64, _ arg1: Int32) throws

@JavaMethod
public func wait() throws
}
extension JavaClass<JavaByte> {
@JavaStaticField
public var MIN_VALUE: Int8

@JavaStaticField
public var MAX_VALUE: Int8

@JavaStaticField
public var TYPE: JavaClass<JavaByte>?

@JavaStaticField
public var SIZE: Int32

@JavaStaticField
public var BYTES: Int32

@JavaStaticMethod
public func toString(_ arg0: Int8) -> String

@JavaStaticMethod
public func hashCode(_ arg0: Int8) -> Int32

@JavaStaticMethod
public func compareUnsigned(_ arg0: Int8, _ arg1: Int8) -> Int32

@JavaStaticMethod
public func compare(_ arg0: Int8, _ arg1: Int8) -> Int32

@JavaStaticMethod
public func valueOf(_ arg0: String) throws -> JavaByte?

@JavaStaticMethod
public func valueOf(_ arg0: String, _ arg1: Int32) throws -> JavaByte?

@JavaStaticMethod
public func valueOf(_ arg0: Int8) -> JavaByte?

@JavaStaticMethod
public func decode(_ arg0: String) throws -> JavaByte?

@JavaStaticMethod
public func toUnsignedLong(_ arg0: Int8) -> Int64

@JavaStaticMethod
public func toUnsignedInt(_ arg0: Int8) -> Int32

@JavaStaticMethod
public func parseByte(_ arg0: String) throws -> Int8

@JavaStaticMethod
public func parseByte(_ arg0: String, _ arg1: Int32) throws -> Int8
}
Loading