Skip to content

[JExtract] Generate JNI code for classes (static methods and initializers) #298

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 9 commits into from
Jul 3, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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 {
let x: Int64
let y: Int64

public static func method() {
p("Hello from static method in a class!")
}

public init(x: Int64, y: Int64) {
self.x = x
self.y = y
p("\(self)")
}

public init() {
self.x = 10
self.y = 5
}

deinit {
p("deinit called!")
}
}
5 changes: 5 additions & 0 deletions Samples/JExtractJNISampleApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ dependencies {

tasks.named('test', Test) {
useJUnitPlatform()

testLogging {
events "failed"
exceptionFormat "full"
}
}

application {
Expand Down
3 changes: 2 additions & 1 deletion Samples/JExtractJNISampleApp/ci-validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
set -x
set -e

./gradlew run
./gradlew run
./gradlew test
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ static void examples() {
long i = MySwiftLibrary.globalMakeInt();
SwiftKit.trace("globalMakeInt() = " + i);

MySwiftClass.method();

MySwiftClass myClass = MySwiftClass.init(10, 5);
MySwiftClass myClass2 = MySwiftClass.init();

System.out.println("DONE.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// 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;

import com.example.swift.MySwiftLibrary;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class MySwiftClassTest {
@Test
void init_noParameters() {
MySwiftClass c = MySwiftClass.init();
assertNotNull(c);
}

@Test
void init_withParameters() {
MySwiftClass c = MySwiftClass.init(1337, 42);
assertNotNull(c);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// 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;

import com.example.swift.MySwiftLibrary;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

public class MySwiftLibraryTest {
@Test
void call_helloWorld() {
MySwiftLibrary.helloWorld();
}

@Test
void call_globalTakeInt() {
MySwiftLibrary.globalTakeInt(12);
}

@Test
void call_globalMakeInt() {
long i = MySwiftLibrary.globalMakeInt();
assertEquals(42, i);
}

@Test
void call_globalTakeIntInt() {
MySwiftLibrary.globalTakeIntInt(1337, 42);
}

@Test
void call_writeString_jextract() {
var string = "Hello Swift!";
long reply = MySwiftLibrary.globalWriteString(string);

assertEquals(string.length(), reply);
}
}
38 changes: 38 additions & 0 deletions Sources/JExtractSwiftLib/Convenience/JavaType+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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 JavaTypes

extension JavaType {
var jniTypeSignature: String {
switch self {
case .boolean: "Z"
case .byte: "B"
case .char: "C"
case .short: "S"
case .int: "I"
case .long: "J"
case .float: "F"
case .double: "D"
case .class(let package, let name):
if let package {
"L\(package.replacingOccurrences(of: ".", with: "/"))/\(name);"
} else {
"L\(name);"
}
case .array(let javaType): "[\(javaType.jniTypeSignature)"
case .void: fatalError("There is no type signature for 'void'")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ extension FFMSwift2JavaGenerator {
}
}

extension JavaConversionStep {
extension FFMSwift2JavaGenerator.JavaConversionStep {
/// Whether the conversion uses SwiftArena.
var requiresSwiftArena: Bool {
switch self {
Expand Down
Loading
Loading