Skip to content

Commit 7a9ecb8

Browse files
authored
Add JavaIO and CSV example (#234)
* [javakit] Generate some java.io types * [javakit] exception description should include type, otherwise hard to act on * [sample] extend dependency example to consume commons csv as an example * add missing license in empty file * [javakit] correct the description/toString of Throwable * Discard changes to Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java * Discard changes to Samples/JavaKitSampleApp/Sources/JavaKitExample/JavaKitExample.swift
1 parent 00df828 commit 7a9ecb8

34 files changed

+897
-106
lines changed

Package.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ let package = Package(
7676
targets: ["JavaKitNetwork"]
7777
),
7878

79+
.library(
80+
name: "JavaKitIO",
81+
targets: ["JavaKitIO"]
82+
),
83+
7984
.library(
8085
name: "JavaKitReflection",
8186
targets: ["JavaKitReflection"]
@@ -241,6 +246,15 @@ let package = Package(
241246
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
242247
]
243248
),
249+
.target(
250+
name: "JavaKitIO",
251+
dependencies: ["JavaKit", "JavaKitCollection"],
252+
exclude: ["swift-java.config"],
253+
swiftSettings: [
254+
.swiftLanguageMode(.v5),
255+
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
256+
]
257+
),
244258
.target(
245259
name: "JavaKitReflection",
246260
dependencies: ["JavaKit", "JavaKitCollection"],
@@ -448,6 +462,6 @@ let package = Package(
448462
swiftSettings: [
449463
.swiftLanguageMode(.v5)
450464
]
451-
)
465+
),
452466
]
453467
)

Samples/JavaDependencySampleApp/Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ let package = Package(
8686
.product(name: "JavaKit", package: "swift-java"),
8787
.product(name: "JavaKitFunction", package: "swift-java"),
8888
.product(name: "JavaKitCollection", package: "swift-java"),
89+
.product(name: "JavaKitIO", package: "swift-java"),
90+
.product(name: "JavaKitNetwork", package: "swift-java"),
8991
],
9092
exclude: ["swift-java.config"],
9193
swiftSettings: [
@@ -98,5 +100,7 @@ let package = Package(
98100
]
99101
),
100102

103+
.target(name: "JavaExample"),
104+
101105
]
102106
)

Samples/JavaDependencySampleApp/Sources/JavaCommonsCSV/swift-java.config

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"classes" : {
33
"org.apache.commons.io.FilenameUtils" : "FilenameUtils",
4-
"org.apache.commons.io.IOCase" : "IOCase"
4+
"org.apache.commons.io.IOCase" : "IOCase",
5+
"org.apache.commons.csv.CSVFormat" : "CSVFormat",
6+
"org.apache.commons.csv.CSVParser" : "CSVParser",
7+
"org.apache.commons.csv.CSVRecord" : "CSVRecord"
58
},
69
"dependencies" : [
710
"org.apache.commons:commons-csv:1.12.0"

Samples/JavaDependencySampleApp/Sources/JavaDependencySample/main.swift

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

1515
import JavaKit
1616
import JavaKitFunction
17+
import JavaKitIO
1718
import JavaKitConfigurationShared
1819
import Foundation
1920

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

46+
let CSVFormatClass = try JavaClass<CSVFormat>()
47+
48+
let reader = StringReader("hello,example")
49+
for record in try CSVFormatClass.RFC4180.parse(reader)!.getRecords()! {
50+
for field in record.toList()! {
51+
print("Field: \(field)")
52+
}
53+
}
54+
4555
print("Done.")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) YEARS 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+
//===----------------------------------------------------------------------===//

Sources/Java2Swift/JavaToSwift+FetchDependencies.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ extension JavaToSwift {
4040
print("[info][swift-java] Resolved classpath for \(deps.count) dependencies of '\(moduleName)', classpath entries: \(classpathEntries.count), ", terminator: "")
4141
print("done.".green)
4242

43+
for entry in classpathEntries {
44+
print("[info][swift-java] Classpath entry: \(entry)")
45+
}
46+
4347
return ResolvedDependencyClasspath(for: dependencies, classpath: dependenciesClasspath)
4448
}
4549

@@ -133,6 +137,8 @@ extension JavaToSwift {
133137
// The file contents are just plain
134138
let contents = resolvedClasspath.classpath
135139

140+
print("[debug][swift-java] Resolved dependency: \(classpath)")
141+
136142
// Write the file
137143
try writeContents(
138144
contents,

Sources/JavaKit/Exceptions/Throwable+Error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// Translate all Java Throwable instances in a Swift error.
1616
extension Throwable: Error, CustomStringConvertible {
1717
public var description: String {
18-
return getMessage()
18+
return toString()
1919
}
2020
}
2121

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Auto-generated by Java-to-Swift wrapper generator.
2+
import JavaRuntime
3+
4+
@JavaInterface("java.lang.Appendable")
5+
public struct Appendable {
6+
@JavaMethod
7+
public func append(_ arg0: CharSequence?) throws -> Appendable!
8+
9+
@JavaMethod
10+
public func append(_ arg0: CharSequence?, _ arg1: Int32, _ arg2: Int32) throws -> Appendable!
11+
12+
@JavaMethod
13+
public func append(_ arg0: UInt16) throws -> Appendable!
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Auto-generated by Java-to-Swift wrapper generator.
2+
import JavaRuntime
3+
4+
@JavaInterface("java.lang.CharSequence")
5+
public struct CharSequence {
6+
@JavaMethod
7+
public func length() -> Int32
8+
9+
@JavaMethod
10+
public func toString() -> String
11+
12+
@JavaMethod
13+
public func charAt(_ arg0: Int32) -> UInt16
14+
15+
@JavaMethod
16+
public func isEmpty() -> Bool
17+
18+
@JavaMethod
19+
public func subSequence(_ arg0: Int32, _ arg1: Int32) -> CharSequence!
20+
}
21+
extension JavaClass<CharSequence> {
22+
@JavaStaticMethod
23+
public func compare(_ arg0: CharSequence?, _ arg1: CharSequence?) -> Int32
24+
}

Sources/JavaKit/generated/JavaInteger.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import JavaRuntime
33

44
@JavaClass("java.lang.Integer")
55
open class JavaInteger: JavaNumber {
6+
67
@JavaMethod
78
@_nonoverride public convenience init(_ arg0: Int32, environment: JNIEnvironment? = nil)
89

0 commit comments

Comments
 (0)