Skip to content
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
Expand Up @@ -42,6 +42,11 @@ public void greet(String name) {
System.out.println("Salutations, " + name);
}

// method called 'init' to check we're able to handle these and not clash with swift 'init' keyword
public long init(long value) {
return value;
}

public Predicate<Integer> lessThanTen() {
Predicate<Integer> predicate = i -> (i < 10);
return predicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ import JavaUtilFunction
import Testing

@Suite
struct ManglingTests {
struct JavaKitExampleRuntimeTests {

let jvm = try JavaKitSampleJVM.shared

@Test
func methodMangling() throws {
let jvm = try! JavaVirtualMachine.shared(
classpath: [
".build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java"
]
)
let env = try! jvm.environment()
let env = try jvm.environment()

let helper = ThreadSafeHelperClass(environment: env)

Expand All @@ -53,4 +50,14 @@ struct ManglingTests {
#expect(#"Optional(21)"# == String(describing: longOpt))
}

}
@Test
func methodNamedInit() throws {
let env = try jvm.environment()

let hello = HelloSwift(environment: env)

let reply = hello.`init`(128)
#expect(reply == 128)
}

}
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) 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
//
//===----------------------------------------------------------------------===//

import JavaKitExample

import SwiftJava
import JavaUtilFunction

/// Utility to configure the classpath and native libraries paths for writing tests against
/// classes defined in this JavaKitExample project
struct JavaKitSampleJVM {

static var shared: JavaVirtualMachine = {
try! JavaVirtualMachine.shared(
classpath: [
".build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java",
],
vmOptions: [
KnownJavaVMOptions.javaLibraryPath(".build/\(SwiftPlatform.debugOrRelease)/"),
]
)
}()

}
7 changes: 6 additions & 1 deletion Samples/JavaKitSampleApp/ci-validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ else
DISABLE_EXPERIMENTAL_PREBUILTS='--disable-experimental-prebuilts'
fi

swift build $DISABLE_EXPERIMENTAL_PREBUILTS
swift build --build-tests $DISABLE_EXPERIMENTAL_PREBUILTS

echo "java application run: ..."
"$JAVA_HOME/bin/java" \
-cp .build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java \
-Djava.library.path=.build/debug \
"com.example.swift.JavaKitSampleMain"
echo "java application run: OK"


swift test $DISABLE_EXPERIMENTAL_PREBUILTS
17 changes: 15 additions & 2 deletions Sources/SwiftJava/JNI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ package final class JNI {
package fileprivate(set) static var shared: JNI?

/// The default application class loader
package let applicationClassLoader: JavaClassLoader
package let applicationClassLoader: JavaClassLoader?

init(fromVM javaVM: JavaVirtualMachine) {
// Update the global JavaVM
JavaVirtualMachine.sharedJVM.withLock {
$0 = javaVM
}
let environment = try! javaVM.environment()
self.applicationClassLoader = try! JavaClass<JavaThread>(environment: environment).currentThread().getContextClassLoader()
do {
let clazz = try JavaClass<JavaThread>(environment: environment)
guard let thread: JavaThread = clazz.currentThread() else {
applicationClassLoader = nil
return
}
guard let cl = thread.getContextClassLoader() else {
applicationClassLoader = nil
return
}
self.applicationClassLoader = cl
} catch {
fatalError("Failed to get current thread's ContextClassLoader: \(error)")
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions Sources/SwiftJava/KnownJavaVMOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// 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 struct KnownJavaVMOptions {

/// Helper for the option to configure where native libraries should be searched for: `-Djava.library.path`
public static func javaLibraryPath(_ path: String) -> String {
return "-Djava.library.path=" + path
}

}
27 changes: 27 additions & 0 deletions Sources/SwiftJava/SwiftPlatform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

import CSwiftJavaJNI

/// Helpers for forming platform specific directory names and paths.
public struct SwiftPlatform {

public static var debugOrRelease: String {
#if DEBUG
"debug"
#else
"release"
#endif
}
}
13 changes: 8 additions & 5 deletions Sources/SwiftJavaRuntimeSupport/_JNIMethodIDCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ public final class _JNIMethodIDCache: Sendable {
environment.interface.ExceptionClear(environment)

// OK to force unwrap, we are in a jextract environment.
if let javaClass = try? JNI.shared!.applicationClassLoader.loadClass(
guard let jni = JNI.shared else {
fatalError("Cannot get JNI.shared, it should have been initialized by JNI_OnLoad when loading the library")
}
guard let javaClass = try? jni.applicationClassLoader?.loadClass(
className.replacingOccurrences(of: "/", with: ".")
) {
clazz = javaClass.javaThis
self.javaObjectHolder = javaClass.javaHolder
} else {
) else {
fatalError("Class \(className) could not be found!")
}

clazz = javaClass.javaThis
self.javaObjectHolder = javaClass.javaHolder
}

self._class = clazz
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftJavaToolLib/JavaClassTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ extension JavaClassTranslator {
}
// Do we need to record any generic information, in order to enable type-erasure for the upcalls?
var parameters: [String] = []
// If the method name is "init", we need to explicitly specify it in the annotation
// because "init" is a Swift keyword and will be escaped in the function name via `init`
if javaMethod.getName() == "init" {
parameters.append("\"init\"")
}
if hasTypeEraseGenericResultType {
parameters.append("typeErasedResult: \"\(resultType)\"")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ final class BasicWrapJavaTests: XCTestCase {

class SuperClass {
public static final long serialVersionUID = 1L;
public void init() throws Exception {}
}

class SubClass extends SuperClass {
Expand Down Expand Up @@ -128,4 +129,37 @@ final class BasicWrapJavaTests: XCTestCase {
]
)
}

// Test that Java methods named "init" get @JavaMethod("init") annotation.
// Since "init" is a Swift keyword and gets escaped with backticks in the function name,
// we explicitly specify the Java method name in the annotation.
// See KeyAgreement.init() methods as a real-world example.
func test_wrapJava_initMethodAnnotation() async throws {
let classpathURL = try await compileJava(
"""
package com.example;

class TestClass {
public void init(String arg) throws Exception {}
public void init() throws Exception {}
}
""")

try assertWrapJavaOutput(
javaClassNames: [
"com.example.TestClass"
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaMethod("init")
open func `init`(_ arg0: String) throws
""",
"""
@JavaMethod("init")
open func `init`() throws
""",
]
)
}
}
Loading