File tree Expand file tree Collapse file tree 8 files changed +133
-16
lines changed
Sources/JavaKitExample/com/example/swift
Tests/JavaKitExampleTests Expand file tree Collapse file tree 8 files changed +133
-16
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,11 @@ public void greet(String name) {
4242 System .out .println ("Salutations, " + name );
4343 }
4444
45+ // method called 'init' to check we're able to handle these and not clash with swift 'init' keyword
46+ public long init (long value ) {
47+ return value ;
48+ }
49+
4550 public Predicate <Integer > lessThanTen () {
4651 Predicate <Integer > predicate = i -> (i < 10 );
4752 return predicate ;
Original file line number Diff line number Diff line change @@ -19,16 +19,13 @@ import JavaUtilFunction
1919import Testing
2020
2121@Suite
22- struct ManglingTests {
22+ struct JavaKitExampleRuntimeTests {
23+
24+ let jvm = try JavaKitSampleJVM . shared
2325
2426 @Test
2527 func methodMangling( ) throws {
26- let jvm = try ! JavaVirtualMachine . shared (
27- classpath: [
28- " .build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java "
29- ]
30- )
31- let env = try ! jvm. environment ( )
28+ let env = try jvm. environment ( )
3229
3330 let helper = ThreadSafeHelperClass ( environment: env)
3431
@@ -53,4 +50,14 @@ struct ManglingTests {
5350 #expect( #"Optional(21)"# == String ( describing: longOpt) )
5451 }
5552
56- }
53+ @Test
54+ func methodNamedInit( ) throws {
55+ let env = try jvm. environment ( )
56+
57+ let hello = HelloSwift ( environment: env)
58+
59+ let reply = hello. `init` ( 128 )
60+ #expect( reply == 128 )
61+ }
62+
63+ }
Original file line number Diff line number Diff line change 1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2024 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+ //===----------------------------------------------------------------------===//
14+
15+ import JavaKitExample
16+
17+ import SwiftJava
18+ import JavaUtilFunction
19+
20+ /// Utility to configure the classpath and native libraries paths for writing tests against
21+ /// classes defined in this JavaKitExample project
22+ struct JavaKitSampleJVM {
23+
24+ static var shared : JavaVirtualMachine = {
25+ try ! JavaVirtualMachine . shared (
26+ classpath: [
27+ " .build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java " ,
28+ ] ,
29+ vmOptions: [
30+ KnownJavaVMOptions . javaLibraryPath ( " .build/ \( SwiftPlatform . debugOrRelease) / " ) ,
31+ ]
32+ )
33+ } ( )
34+
35+ }
Original file line number Diff line number Diff line change 1010 DISABLE_EXPERIMENTAL_PREBUILTS=' --disable-experimental-prebuilts'
1111fi
1212
13- swift build $DISABLE_EXPERIMENTAL_PREBUILTS
13+ swift build --build-tests $DISABLE_EXPERIMENTAL_PREBUILTS
1414
15+ echo " java application run: ..."
1516" $JAVA_HOME /bin/java" \
1617 -cp .build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java \
1718 -Djava.library.path=.build/debug \
1819 " com.example.swift.JavaKitSampleMain"
20+ echo " java application run: OK"
21+
22+
23+ swift test $DISABLE_EXPERIMENTAL_PREBUILTS
Original file line number Diff line number Diff line change @@ -28,15 +28,28 @@ package final class JNI {
2828 package fileprivate( set) static var shared : JNI ?
2929
3030 /// The default application class loader
31- package let applicationClassLoader : JavaClassLoader
31+ package let applicationClassLoader : JavaClassLoader ?
3232
3333 init ( fromVM javaVM: JavaVirtualMachine ) {
3434 // Update the global JavaVM
3535 JavaVirtualMachine . sharedJVM. withLock {
3636 $0 = javaVM
3737 }
3838 let environment = try ! javaVM. environment ( )
39- self . applicationClassLoader = try ! JavaClass < JavaThread > ( environment: environment) . currentThread ( ) . getContextClassLoader ( )
39+ do {
40+ let clazz = try JavaClass < JavaThread > ( environment: environment)
41+ guard let thread: JavaThread = clazz. currentThread ( ) else {
42+ applicationClassLoader = nil
43+ return
44+ }
45+ guard let cl = thread. getContextClassLoader ( ) else {
46+ applicationClassLoader = nil
47+ return
48+ }
49+ self . applicationClassLoader = cl
50+ } catch {
51+ fatalError ( " Failed to get current thread's ContextClassLoader: \( error) " )
52+ }
4053 }
4154}
4255
Original file line number Diff line number Diff line change 1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2024 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+ //===----------------------------------------------------------------------===//
14+
15+ public struct KnownJavaVMOptions {
16+
17+ /// Helper for the option to configure where native libraries should be searched for: `-Djava.library.path`
18+ public static func javaLibraryPath( _ path: String ) -> String {
19+ return " -Djava.library.path= " + path
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2024 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+ //===----------------------------------------------------------------------===//
14+
15+ import CSwiftJavaJNI
16+
17+ /// Helpers for forming platform specific directory names and paths.
18+ public struct SwiftPlatform {
19+
20+ public static var debugOrRelease : String {
21+ #if DEBUG
22+ " debug "
23+ #else
24+ " release "
25+ #endif
26+ }
27+ }
Original file line number Diff line number Diff line change @@ -56,14 +56,17 @@ public final class _JNIMethodIDCache: Sendable {
5656 environment. interface. ExceptionClear ( environment)
5757
5858 // OK to force unwrap, we are in a jextract environment.
59- if let javaClass = try ? JNI . shared!. applicationClassLoader. loadClass (
59+ guard let jni = JNI . shared else {
60+ fatalError ( " Cannot get JNI.shared, it should have been initialized by JNI_OnLoad when loading the library " )
61+ }
62+ guard let javaClass = try ? jni. applicationClassLoader? . loadClass (
6063 className. replacingOccurrences ( of: " / " , with: " . " )
61- ) {
62- clazz = javaClass. javaThis
63- self . javaObjectHolder = javaClass. javaHolder
64- } else {
64+ ) else {
6565 fatalError ( " Class \( className) could not be found! " )
6666 }
67+
68+ clazz = javaClass. javaThis
69+ self . javaObjectHolder = javaClass. javaHolder
6770 }
6871
6972 self . _class = clazz
You can’t perform that action at this time.
0 commit comments