Skip to content

Add JavaIO and CSV example #234

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 7 commits into from
May 28, 2025
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
16 changes: 15 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ let package = Package(
targets: ["JavaKitNetwork"]
),

.library(
name: "JavaKitIO",
targets: ["JavaKitIO"]
),

.library(
name: "JavaKitReflection",
targets: ["JavaKitReflection"]
Expand Down Expand Up @@ -241,6 +246,15 @@ let package = Package(
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
]
),
.target(
name: "JavaKitIO",
dependencies: ["JavaKit", "JavaKitCollection"],
exclude: ["swift-java.config"],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
]
),
.target(
name: "JavaKitReflection",
dependencies: ["JavaKit", "JavaKitCollection"],
Expand Down Expand Up @@ -448,6 +462,6 @@ let package = Package(
swiftSettings: [
.swiftLanguageMode(.v5)
]
)
),
]
)
4 changes: 4 additions & 0 deletions Samples/JavaDependencySampleApp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ let package = Package(
.product(name: "JavaKit", package: "swift-java"),
.product(name: "JavaKitFunction", package: "swift-java"),
.product(name: "JavaKitCollection", package: "swift-java"),
.product(name: "JavaKitIO", package: "swift-java"),
.product(name: "JavaKitNetwork", package: "swift-java"),
],
exclude: ["swift-java.config"],
swiftSettings: [
Expand All @@ -98,5 +100,7 @@ let package = Package(
]
),

.target(name: "JavaExample"),

]
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"classes" : {
"org.apache.commons.io.FilenameUtils" : "FilenameUtils",
"org.apache.commons.io.IOCase" : "IOCase"
"org.apache.commons.io.IOCase" : "IOCase",
"org.apache.commons.csv.CSVFormat" : "CSVFormat",
"org.apache.commons.csv.CSVParser" : "CSVParser",
"org.apache.commons.csv.CSVRecord" : "CSVRecord"
},
"dependencies" : [
"org.apache.commons:commons-csv:1.12.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import JavaKit
import JavaKitFunction
import JavaKitIO
import JavaKitConfigurationShared
import Foundation

Expand Down Expand Up @@ -42,4 +43,13 @@ let ext = try! FilenameUtilsClass.getExtension(path)
print("org.apache.commons.io.FilenameUtils.getExtension = \(ext)")
precondition(ext == "exe")

let CSVFormatClass = try JavaClass<CSVFormat>()

let reader = StringReader("hello,example")
for record in try CSVFormatClass.RFC4180.parse(reader)!.getRecords()! {
for field in record.toList()! {
print("Field: \(field)")
}
}

print("Done.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) YEARS 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
//
//===----------------------------------------------------------------------===//
6 changes: 6 additions & 0 deletions Sources/Java2Swift/JavaToSwift+FetchDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ extension JavaToSwift {
print("[info][swift-java] Resolved classpath for \(deps.count) dependencies of '\(moduleName)', classpath entries: \(classpathEntries.count), ", terminator: "")
print("done.".green)

for entry in classpathEntries {
print("[info][swift-java] Classpath entry: \(entry)")
}

return ResolvedDependencyClasspath(for: dependencies, classpath: dependenciesClasspath)
}

Expand Down Expand Up @@ -133,6 +137,8 @@ extension JavaToSwift {
// The file contents are just plain
let contents = resolvedClasspath.classpath

print("[debug][swift-java] Resolved dependency: \(classpath)")

// Write the file
try writeContents(
contents,
Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaKit/Exceptions/Throwable+Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Translate all Java Throwable instances in a Swift error.
extension Throwable: Error, CustomStringConvertible {
public var description: String {
return getMessage()
return toString()
}
}

Expand Down
14 changes: 14 additions & 0 deletions Sources/JavaKit/generated/Appendable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Auto-generated by Java-to-Swift wrapper generator.
import JavaRuntime

@JavaInterface("java.lang.Appendable")
public struct Appendable {
@JavaMethod
public func append(_ arg0: CharSequence?) throws -> Appendable!

@JavaMethod
public func append(_ arg0: CharSequence?, _ arg1: Int32, _ arg2: Int32) throws -> Appendable!

@JavaMethod
public func append(_ arg0: UInt16) throws -> Appendable!
}
24 changes: 24 additions & 0 deletions Sources/JavaKit/generated/CharSequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Auto-generated by Java-to-Swift wrapper generator.
import JavaRuntime

@JavaInterface("java.lang.CharSequence")
public struct CharSequence {
@JavaMethod
public func length() -> Int32

@JavaMethod
public func toString() -> String

@JavaMethod
public func charAt(_ arg0: Int32) -> UInt16

@JavaMethod
public func isEmpty() -> Bool

@JavaMethod
public func subSequence(_ arg0: Int32, _ arg1: Int32) -> CharSequence!
}
extension JavaClass<CharSequence> {
@JavaStaticMethod
public func compare(_ arg0: CharSequence?, _ arg1: CharSequence?) -> Int32
}
1 change: 1 addition & 0 deletions Sources/JavaKit/generated/JavaInteger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import JavaRuntime

@JavaClass("java.lang.Integer")
open class JavaInteger: JavaNumber {

@JavaMethod
@_nonoverride public convenience init(_ arg0: Int32, environment: JNIEnvironment? = nil)

Expand Down
6 changes: 6 additions & 0 deletions Sources/JavaKit/generated/JavaLong.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,18 @@ extension JavaClass<JavaLong> {
@JavaStaticMethod
public func rotateRight(_ arg0: Int64, _ arg1: Int32) -> Int64

@JavaStaticMethod
public func parseLong(_ arg0: CharSequence?, _ arg1: Int32, _ arg2: Int32, _ arg3: Int32) throws -> Int64

@JavaStaticMethod
public func parseLong(_ arg0: String, _ arg1: Int32) throws -> Int64

@JavaStaticMethod
public func parseLong(_ arg0: String) throws -> Int64

@JavaStaticMethod
public func parseUnsignedLong(_ arg0: CharSequence?, _ arg1: Int32, _ arg2: Int32, _ arg3: Int32) throws -> Int64

@JavaStaticMethod
public func parseUnsignedLong(_ arg0: String, _ arg1: Int32) throws -> Int64

Expand Down
61 changes: 38 additions & 23 deletions Sources/JavaKit/generated/JavaString.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Auto-generated by Java-to-Swift wrapper generator.
import JavaRuntime

@JavaClass("java.lang.String")
@JavaClass("java.lang.String", implements: CharSequence.self)
open class JavaString: JavaObject {
@JavaMethod
@_nonoverride public convenience init(_ arg0: [Int8], _ arg1: String, environment: JNIEnvironment? = nil) throws
Expand Down Expand Up @@ -52,16 +52,16 @@ open class JavaString: JavaObject {
open func getChars(_ arg0: Int32, _ arg1: Int32, _ arg2: [UInt16], _ arg3: Int32)

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

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

@JavaMethod
open func indexOf(_ arg0: String, _ arg1: Int32, _ arg2: Int32) -> Int32
open func indexOf(_ arg0: String, _ arg1: Int32) -> Int32

@JavaMethod
open func indexOf(_ arg0: String) -> Int32
open func indexOf(_ arg0: String, _ arg1: Int32, _ arg2: Int32) -> Int32

@JavaMethod
open func indexOf(_ arg0: Int32) -> Int32
Expand All @@ -73,7 +73,7 @@ open class JavaString: JavaObject {
open func indexOf(_ arg0: Int32, _ arg1: Int32, _ arg2: Int32) -> Int32

@JavaMethod
open func indexOf(_ arg0: String, _ arg1: Int32) -> Int32
open func indexOf(_ arg0: String) -> Int32

@JavaMethod
open func charAt(_ arg0: Int32) -> UInt16
Expand All @@ -90,48 +90,54 @@ open class JavaString: JavaObject {
@JavaMethod
open func offsetByCodePoints(_ arg0: Int32, _ arg1: Int32) -> Int32

@JavaMethod
open func getBytes() -> [Int8]

@JavaMethod
open func getBytes(_ arg0: String) throws -> [Int8]

@JavaMethod
open func getBytes(_ arg0: Int32, _ arg1: Int32, _ arg2: [Int8], _ arg3: Int32)

@JavaMethod
open func regionMatches(_ arg0: Bool, _ arg1: Int32, _ arg2: String, _ arg3: Int32, _ arg4: Int32) -> Bool
open func getBytes() -> [Int8]

@JavaMethod
open func contentEquals(_ arg0: CharSequence?) -> Bool

@JavaMethod
open func regionMatches(_ arg0: Int32, _ arg1: String, _ arg2: Int32, _ arg3: Int32) -> Bool

@JavaMethod
open func regionMatches(_ arg0: Bool, _ arg1: Int32, _ arg2: String, _ arg3: Int32, _ arg4: Int32) -> Bool

@JavaMethod
open func startsWith(_ arg0: String) -> Bool

@JavaMethod
open func startsWith(_ arg0: String, _ arg1: Int32) -> Bool

@JavaMethod
open func lastIndexOf(_ arg0: String) -> Int32
open func lastIndexOf(_ arg0: Int32) -> Int32

@JavaMethod
open func lastIndexOf(_ arg0: Int32, _ arg1: Int32) -> Int32
open func lastIndexOf(_ arg0: String) -> Int32

@JavaMethod
open func lastIndexOf(_ arg0: String, _ arg1: Int32) -> Int32

@JavaMethod
open func lastIndexOf(_ arg0: Int32) -> Int32
open func lastIndexOf(_ arg0: Int32, _ arg1: Int32) -> Int32

@JavaMethod
open func substring(_ arg0: Int32) -> String
open func substring(_ arg0: Int32, _ arg1: Int32) -> String

@JavaMethod
open func substring(_ arg0: Int32, _ arg1: Int32) -> String
open func substring(_ arg0: Int32) -> String

@JavaMethod
open func isEmpty() -> Bool

@JavaMethod
open func replace(_ arg0: CharSequence?, _ arg1: CharSequence?) -> String

@JavaMethod
open func replace(_ arg0: UInt16, _ arg1: UInt16) -> String

Expand Down Expand Up @@ -189,9 +195,15 @@ open class JavaString: JavaObject {
@JavaMethod
open func endsWith(_ arg0: String) -> Bool

@JavaMethod
open func subSequence(_ arg0: Int32, _ arg1: Int32) -> CharSequence!

@JavaMethod
open func concat(_ arg0: String) -> String

@JavaMethod
open func contains(_ arg0: CharSequence?) -> Bool

@JavaMethod
open func indent(_ arg0: Int32) -> String

Expand All @@ -215,39 +227,42 @@ open class JavaString: JavaObject {
}
}
extension JavaClass<JavaString> {
@JavaStaticMethod
public func valueOf(_ arg0: JavaObject?) -> String

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

@JavaStaticMethod
public func valueOf(_ arg0: [UInt16]) -> String
public func valueOf(_ arg0: Int32) -> String

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

@JavaStaticMethod
public func valueOf(_ arg0: [UInt16], _ arg1: Int32, _ arg2: Int32) -> String

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

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

@JavaStaticMethod
public func valueOf(_ arg0: UInt16) -> String
public func valueOf(_ arg0: [UInt16]) -> String

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

@JavaStaticMethod
public func valueOf(_ arg0: Int32) -> String
public func join(_ arg0: CharSequence?, _ arg1: [CharSequence?]) -> String

@JavaStaticMethod
public func format(_ arg0: String, _ arg1: [JavaObject?]) -> String

@JavaStaticMethod
public func copyValueOf(_ arg0: [UInt16]) -> String
public func copyValueOf(_ arg0: [UInt16], _ arg1: Int32, _ arg2: Int32) -> String

@JavaStaticMethod
public func copyValueOf(_ arg0: [UInt16], _ arg1: Int32, _ arg2: Int32) -> String
public func copyValueOf(_ arg0: [UInt16]) -> String
}
2 changes: 2 additions & 0 deletions Sources/JavaKit/swift-java.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"java.lang.String" : "JavaString",
"java.lang.Throwable" : "Throwable",
"java.lang.Void" : "JavaVoid",
"java.lang.CharSequence": "CharSequence",
"java.lang.Appendable": "Appendable",
"java.util.Optional": "JavaOptional",
"java.util.OptionalDouble": "JavaOptionalDouble",
"java.util.OptionalInt": "JavaOptionalInt",
Expand Down
Loading