Skip to content

Add Support for Threadsafe and NotThreadSafe annotations #188

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 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -76,6 +76,9 @@ extension HelloSwift: HelloSwiftNativeMethods {
print("Caught Java error: \(error)")
}

// Make sure that the thread safe class is sendable
let threadSafe: Sendable = ThreadSafeHelperClass(environment: javaEnvironment)

return i * j
}

Expand Down
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
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface ThreadSafe {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.swift;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing license header?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops updated


@ThreadSafe
public class ThreadSafeHelperClass {
public ThreadSafeHelperClass() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"classes" : {
"com.example.swift.HelloSwift" : "HelloSwift",
"com.example.swift.HelloSubclass" : "HelloSubclass",
"com.example.swift.JavaKitSampleMain" : "JavaKitSampleMain"
"com.example.swift.JavaKitSampleMain" : "JavaKitSampleMain",
"com.example.swift.ThreadSafeHelperClass" : "ThreadSafeHelperClass"
}
}
30 changes: 30 additions & 0 deletions Sources/Java2SwiftLib/JavaClassTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ struct JavaClassTranslator {
/// The Swift names of the interfaces that this class implements.
let swiftInterfaces: [String]

/// The annotations of the Java class
let annotations: [Annotation]

/// The (instance) fields of the Java class.
var fields: [Field] = []

Expand Down Expand Up @@ -164,6 +167,8 @@ struct JavaClassTranslator {
}
}

self.annotations = javaClass.getAnnotations().compactMap(\.self)

// Collect all of the class members that we will need to translate.
// TODO: Switch over to "declared" versions of these whenever we don't need
// to see inherited members.
Expand Down Expand Up @@ -274,6 +279,7 @@ extension JavaClassTranslator {
if let nativeMethodsProtocol = renderNativeMethodsProtocol() {
allDecls.append(nativeMethodsProtocol)
}
allDecls.append(contentsOf: renderAnnotationExtensions())
return allDecls
}

Expand Down Expand Up @@ -483,6 +489,30 @@ extension JavaClassTranslator {
return protocolDecl.formatted(using: translator.format).cast(DeclSyntax.self)
}

func renderAnnotationExtensions() -> [DeclSyntax] {
var extensions: [DeclSyntax] = []

for annotation in annotations {
let annotationName = annotation.annotationType().getName().splitSwiftTypeName().name
if annotationName == "ThreadSafe" || annotationName == "Immutable" { // If we are threadsafe, mark as unchecked Sendable
extensions.append(
"""
extension \(raw: swiftTypeName): @unchecked Swift.Sendable { }
"""
)
} else if annotationName == "NotThreadSafe" { // If we are _not_ threadsafe, mark sendable unavailable
extensions.append(
"""
@available(unavailable, *)
extension \(raw: swiftTypeName): Swift.Sendable { }
"""
)
}
}

return extensions
}

/// Render the given Java constructor as a Swift initializer.
package func renderConstructor(
_ javaConstructor: Constructor<some AnyJavaObject>
Expand Down
3 changes: 3 additions & 0 deletions Sources/JavaKitReflection/JavaClass+Reflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ extension JavaClass {

@JavaMethod
public func getGenericInterfaces() -> [Type?]

@JavaMethod
public func getAnnotations() -> [Annotation?]
}
Loading