Skip to content
Closed
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
42 changes: 42 additions & 0 deletions Samples/SwiftJavaExtractKotlinSampleApp/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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: "SwiftJavaExtractKotlinSampleApp",
platforms: [
.macOS(.v15),
.iOS(.v18),
.watchOS(.v11),
.tvOS(.v18),
],
products: [
.library(
name: "MySwiftLibrary",
type: .dynamic,
targets: ["MySwiftLibrary"]
)
],
dependencies: [
.package(name: "swift-java", path: "../../")
],
targets: [
.target(
name: "MySwiftLibrary",
dependencies: [
.product(name: "SwiftJava", package: "swift-java")
],
exclude: [
"swift-java.config"
],
swiftSettings: [
.swiftLanguageMode(.v5)
],
plugins: [
.plugin(name: "JExtractSwiftPlugin", package: "swift-java")
]
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 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
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

public func echoData(_ data: Data) -> Data {
data
}

public func makeData() -> Data {
Data([0x01, 0x02, 0x03, 0x04])
}

public func getDataCount(_ data: Data) -> Int {
data.count
}

public func compareData(_ data1: Data, _ data2: Data) -> Bool {
data1 == data2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

public final class SwiftTypeInSubDirectory {
public init() {}

public func hello() -> Int {
12
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// This file exists to exercise the swiftpm plugin generating separate output Java files
// for the public types; because Java public types must be in a file with the same name as the type.

public struct PublicTypeOne {
public init() {}
public func test() {}
}

public struct PublicTypeTwo {
public init() {}
public func test() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

public class MySwiftClass {

public let byte: UInt8 = 0
public var len: Int
public var cap: Int

public init(len: Int, cap: Int) {
self.len = len
self.cap = cap
}

deinit {
}

public var counter: Int32 = 0

public static func factory(len: Int, cap: Int) -> MySwiftClass {
MySwiftClass(len: len, cap: cap)
}

public func voidMethod() {
}

public func takeIntMethod(i: Int) {
}

public func echoIntMethod(i: Int) -> Int {
i
}

public func makeIntMethod() -> Int {
12
}

public func makeRandomIntMethod() -> Int {
Int.random(in: 1..<256)
}

public func takeUnsignedChar(arg: UInt16) {
}

public func takeUnsignedInt(arg: UInt32) {
}

public func takeUnsignedLong(arg: UInt64) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// This is a "plain Swift" file containing various types of declarations,
// that is exported to Java by using the `jextract-swift` tool.
//
// No annotations are necessary on the Swift side to perform the export.

import Foundation

#if os(Linux)
import Glibc
#else
import Darwin.C
#endif

public func helloWorld() {
}

public func globalTakeInt(i: Int) {
}

public func globalMakeInt() -> Int {
42
}

public func globalWriteString(string: String) -> Int {
string.count
}

public func globalTakeIntInt(i: Int, j: Int) {
}

public func globalCallMeRunnable(run: () -> Void) {
run()
}

public func globalReceiveRawBuffer(buf: UnsafeRawBufferPointer) -> Int {
buf.count
}

public var globalBuffer: UnsafeRawBufferPointer = UnsafeRawBufferPointer(
UnsafeMutableRawBufferPointer.allocate(byteCount: 124, alignment: 1)
)

public func globalReceiveReturnData(data: Data) -> Data {
Data(data)
}

public func withBuffer(body: (UnsafeRawBufferPointer) -> Void) {
body(globalBuffer)
}

public func getArray() -> [UInt8] {
[1, 2, 3]
}

// Tuple round-trips for jextract FFM (see `FFMTupleTest` in the sample app).
public func ffmTupleReturnPair() -> (Int32, Int64) {
(42, 43)
}

public func ffmTupleSumPair(_ arg: (Int32, Int64)) -> Int64 {
Int64(arg.0) + arg.1
}

public func ffmTupleLabeledPair() -> (x: Int32, y: Int32) {
(x: 10, y: 20)
}

public func sumAllByteArrayElements(actuallyAnArray: UnsafeRawPointer, count: Int) -> Int {
let bufferPointer = UnsafeRawBufferPointer(start: actuallyAnArray, count: count)
let array = Array(bufferPointer)
return Int(array.reduce(0, { partialResult, element in partialResult + element }))
}

public func sumAllByteArrayElements(array: [UInt8]) -> Int {
Int(array.reduce(0, { partialResult, element in partialResult + element }))
}
public func returnSwiftArray() -> [UInt8] {
[1, 2, 3, 4]
}

public func withArray(body: ([UInt8]) -> Void) {
body([1, 2, 3])
}

public func globalReceiveSomeDataProtocol(data: some DataProtocol) -> Int {
p(Array(data).description)
return data.count
}

public func globalReceiveOptional(o1: Int?, o2: (some DataProtocol)?) -> Int {
switch (o1, o2) {
case (nil, nil):
return 0
case (let v1?, nil):
return 1
case (nil, let v2?):
return 2
case (let v1?, let v2?):
return 3
}
}

// ==== -----------------------------------------------------------------------
// MARK: Overloaded functions

public func globalOverloaded(a: Int) {
p("globalOverloaded(a: \(a))")
}

public func globalOverloaded(b: Int) {
p("globalOverloaded(b: \(b))")
}

// ==== Internal helpers

func p(_ msg: String, file: String = #fileID, line: UInt = #line, function: String = #function) {
print("[swift][\(file):\(line)](\(function)) \(msg)")
fflush(stdout)
}

#if os(Linux)
// FIXME: why do we need this workaround?
@_silgen_name("_objc_autoreleaseReturnValue")
public func _objc_autoreleaseReturnValue(a: Any) {}

@_silgen_name("objc_autoreleaseReturnValue")
public func objc_autoreleaseReturnValue(a: Any) {}
#endif
Loading