@@ -52,6 +52,9 @@ struct JavaClassTranslator {
52
52
/// The Swift names of the interfaces that this class implements.
53
53
let swiftInterfaces : [ String ]
54
54
55
+ /// The annotations of the Java class
56
+ let annotations : [ Annotation ]
57
+
55
58
/// The (instance) fields of the Java class.
56
59
var fields : [ Field ] = [ ]
57
60
@@ -164,6 +167,8 @@ struct JavaClassTranslator {
164
167
}
165
168
}
166
169
170
+ self . annotations = javaClass. getAnnotations ( ) . compactMap ( \. self)
171
+
167
172
// Collect all of the class members that we will need to translate.
168
173
// TODO: Switch over to "declared" versions of these whenever we don't need
169
174
// to see inherited members.
@@ -274,6 +279,7 @@ extension JavaClassTranslator {
274
279
if let nativeMethodsProtocol = renderNativeMethodsProtocol ( ) {
275
280
allDecls. append ( nativeMethodsProtocol)
276
281
}
282
+ allDecls. append ( contentsOf: renderAnnotationExtensions ( ) )
277
283
return allDecls
278
284
}
279
285
@@ -483,6 +489,30 @@ extension JavaClassTranslator {
483
489
return protocolDecl. formatted ( using: translator. format) . cast ( DeclSyntax . self)
484
490
}
485
491
492
+ func renderAnnotationExtensions( ) -> [ DeclSyntax ] {
493
+ var extensions : [ DeclSyntax ] = [ ]
494
+
495
+ for annotation in annotations {
496
+ let annotationName = annotation. annotationType ( ) . getName ( ) . splitSwiftTypeName ( ) . name
497
+ if annotationName == " ThreadSafe " || annotationName == " Immutable " { // If we are threadsafe, mark as unchecked Sendable
498
+ extensions. append (
499
+ """
500
+ extension \( raw: swiftTypeName) : @unchecked Swift.Sendable { }
501
+ """
502
+ )
503
+ } else if annotationName == " NotThreadSafe " { // If we are _not_ threadsafe, mark sendable unavailable
504
+ extensions. append (
505
+ """
506
+ @available(unavailable, *)
507
+ extension \( raw: swiftTypeName) : Swift.Sendable { }
508
+ """
509
+ )
510
+ }
511
+ }
512
+
513
+ return extensions
514
+ }
515
+
486
516
/// Render the given Java constructor as a Swift initializer.
487
517
package func renderConstructor(
488
518
_ javaConstructor: Constructor < some AnyJavaObject >
0 commit comments