Skip to content

Commit 101e062

Browse files
committed
remove vendored guava entirely, and instead allow sourcegen to use Guava
types
1 parent 819981c commit 101e062

File tree

19 files changed

+73
-4056
lines changed

19 files changed

+73
-4056
lines changed

NOTICE.txt

Lines changed: 0 additions & 38 deletions
This file was deleted.

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -549,30 +549,32 @@ extension FFMSwift2JavaGenerator {
549549

550550
func unsignedResultConversion(_ from: SwiftType, to javaType: JavaType,
551551
mode: JExtractUnsignedIntegerMode) -> JavaConversionStep {
552-
guard mode == .wrap else {
553-
return .placeholder
554-
}
552+
switch mode {
553+
case .annotate:
554+
return .placeholder // no conversions
555555

556-
guard let className = javaType.className else {
557-
fatalError("Missing target class name for result conversion step from \(from) to \(javaType)")
558-
}
556+
case .wrapGuava:
557+
guard let typeName = javaType.fullyQualifiedClassName else {
558+
fatalError("Missing target class name for result conversion step from \(from) to \(javaType)")
559+
}
559560

560-
switch from {
561-
case .nominal(let nominal):
562-
switch nominal.nominalTypeDecl.knownTypeKind {
563-
case .uint8:
564-
return .call(.placeholder, function: "\(className).fromIntBits", withArena: false)
565-
case .uint16:
566-
return .placeholder // no conversion, UInt16 can be returned as-is and will be seen as char by Java
567-
case .uint32:
568-
return .call(.placeholder, function: "\(className).fromIntBits", withArena: false)
569-
case .uint64:
570-
return .call(.placeholder, function: "\(className).fromLongBits", withArena: false)
571-
default:
572-
fatalError("unsignedResultConversion: Unsupported conversion from \(from) to \(javaType)")
573-
}
574-
default:
575-
fatalError("unsignedResultConversion: Unsupported conversion from \(from) to \(javaType)")
561+
switch from {
562+
case .nominal(let nominal):
563+
switch nominal.nominalTypeDecl.knownTypeKind {
564+
case .uint8:
565+
return .call(.placeholder, function: "\(typeName).fromIntBits", withArena: false)
566+
case .uint16:
567+
return .placeholder // no conversion, UInt16 can be returned as-is and will be seen as char by Java
568+
case .uint32:
569+
return .call(.placeholder, function: "\(typeName).fromIntBits", withArena: false)
570+
case .uint64:
571+
return .call(.placeholder, function: "\(typeName).fromLongBits", withArena: false)
572+
default:
573+
fatalError("unsignedResultConversion: Unsupported conversion from \(from) to \(javaType)")
574+
}
575+
default:
576+
fatalError("unsignedResultConversion: Unsupported conversion from \(from) to \(javaType)")
577+
}
576578
}
577579
}
578580

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ extension FFMSwift2JavaGenerator {
104104

105105
// NonNull, Unsigned and friends
106106
"org.swift.swiftkit.core.annotations.*",
107-
// Unsigned numerics support
108-
"org.swift.swiftkit.core.primitives.*",
109107

110108
// Necessary for native calls and type mapping
111109
"java.lang.foreign.*",
@@ -187,7 +185,7 @@ extension FFMSwift2JavaGenerator {
187185
func printImportedNominal(_ printer: inout CodePrinter, _ decl: ImportedNominalType) {
188186
printHeader(&printer)
189187
printPackage(&printer)
190-
printImports(&printer)
188+
printImports(&printer) // TODO: we could have some imports be driven from types used in the generated decl
191189

192190
printNominal(&printer, decl) { printer in
193191
// We use a static field to abuse the initialization order such that by the time we get type metadata,

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
7878
}
7979
}
8080

81+
/// If this function type uses types that require any additional `import` statements,
82+
/// these would be exported here.
83+
var additionalJavaImports: Set<String> {
84+
var imports: Set<String> = []
85+
// imports += self.functionSignature.parameters.flatMap { $0.additionalJavaImports }
86+
// imports += self.functionSignature.result.additionalJavaImports
87+
return imports
88+
}
89+
8190
var isStatic: Bool {
8291
if case .staticMethod = functionSignature.selfParameter {
8392
return true

Sources/JExtractSwiftLib/JavaTypes/JavaType+SwiftKit.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension JavaType {
2626
case "UInt8":
2727
self = switch unsigned {
2828
case .ignoreSign: .char
29-
case .wrapAsUnsignedNumbers: JavaType.swiftkit.primitives.UnsignedByte
29+
case .wrapUnsignedGuava: JavaType.guava.primitives.UnsignedInteger
3030
}
3131

3232
case "Int16": self = .short
@@ -36,14 +36,14 @@ extension JavaType {
3636
case "UInt32":
3737
self = switch unsigned {
3838
case .ignoreSign: .int
39-
case .wrapAsUnsignedNumbers: JavaType.swiftkit.primitives.UnsignedInteger
39+
case .wrapUnsignedGuava: JavaType.guava.primitives.UnsignedInteger
4040
}
4141

4242
case "Int64": self = .long
4343
case "UInt64":
4444
self = switch unsigned {
4545
case .ignoreSign: .long
46-
case .wrapAsUnsignedNumbers: JavaType.swiftkit.primitives.UnsignedLong
46+
case .wrapUnsignedGuava: JavaType.guava.primitives.UnsignedLong
4747
}
4848

4949
case "Float": self = .float
@@ -60,25 +60,20 @@ extension JavaType {
6060
switch swiftType {
6161
case .nominal(let nominal):
6262
switch nominal.nominalTypeDecl.knownTypeKind {
63-
case .uint8: return swiftkit.primitives.UnsignedByte
63+
case .uint8: return guava.primitives.UnsignedInteger
6464
case .uint16: return .char // no wrapper necessary, we can express it as 'char' natively in Java
65-
case .uint32: return swiftkit.primitives.UnsignedInteger
66-
case .uint64: return swiftkit.primitives.UnsignedLong
65+
case .uint32: return guava.primitives.UnsignedInteger
66+
case .uint64: return guava.primitives.UnsignedLong
6767
default: return nil
6868
}
6969
default: return nil
7070
}
7171
}
7272

73-
enum swiftkit {
73+
/// Known types from the Google Guava library
74+
enum guava {
7475
enum primitives {
75-
static let package = "org.swift.swiftkit.core.primitives"
76-
77-
static var UnsignedByte: JavaType {
78-
.class(package: primitives.package, name: "UnsignedByte")
79-
}
80-
81-
// UnsignedShort is not necessary because UInt16 is directly expressible as Java's unsigned 'char'.
76+
static let package = "com.google.common.primitives"
8277

8378
static var UnsignedInteger: JavaType {
8479
.class(package: primitives.package, name: "UnsignedInteger")

Sources/JavaKitConfigurationShared/GenerationMode.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public enum JExtractUnsignedIntegerMode: String, Codable {
3838
/// This mode trades off performance, due to needing to allocate the type-safe wrapper objects around
3939
/// primitive values, however allows to retain static type information about the unsignedness of
4040
/// unsigned number types in the Java side of generated bindings.
41-
case wrap
41+
case wrapGuava
4242

4343
// /// If possible, use a wider Java signed integer type to represent an Unsigned Swift integer type.
4444
// /// For example, represent a Swift `UInt32` (width equivalent to Java `int`) as a Java signed `long`,
@@ -56,7 +56,7 @@ extension JExtractUnsignedIntegerMode {
5656
public var needsConversion: Bool {
5757
switch self {
5858
case .annotate: false
59-
case .wrap: true
59+
case .wrapGuava: true
6060
}
6161
}
6262

Sources/JavaTypes/JavaType+JavaSource.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extension JavaType: CustomStringConvertible {
6060
}
6161
}
6262

63-
/// Returns the a class name if this java type was a class,
63+
/// Returns the class name if this java type was a class,
6464
/// and nil otherwise.
6565
public var className: String? {
6666
switch self {
@@ -70,4 +70,17 @@ extension JavaType: CustomStringConvertible {
7070
return nil
7171
}
7272
}
73+
74+
/// Returns the fully qualified class name if this java type was a class,
75+
/// and nil otherwise.
76+
public var fullyQualifiedClassName: String? {
77+
switch self {
78+
case .class(.some(let package), let name):
79+
return "\(package).\(name)"
80+
case .class(nil, let name):
81+
return name
82+
default:
83+
return nil
84+
}
85+
}
7386
}

Sources/JavaTypes/JavaType+SwiftNames.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,21 @@ extension JavaType {
9696
/// - `UInt32` is imported as `int`
9797
/// - `UInt64` is imported as `long`
9898
///
99-
/// When `wrapAsUnsignedNumbers` is used, unsigned Swift types are imported as safe "wrapper" types on the Java side.
99+
/// When `wrapUnsignedGuava` is used, unsigned Swift types are imported as safe "wrapper" types from the popular Guava
100+
/// library on the Java side. SwiftJava does not include these types, so you would have to make sure your project depends
101+
/// on Guava for such generated code to be able to compile.
102+
///
100103
/// These make the Unsigned nature of the types explicit in Java, however they come at a cost of allocating the wrapper
101104
/// object, and indirection when accessing the underlying numeric value. These are often useful as a signal to watch out
102105
/// when dealing with a specific API, however in high performance use-cases, one may want to choose using the primitive
103106
/// values directly, and interact with them using {@code UnsignedIntegers} SwiftKit helper classes on the Java side.
104107
///
105108
/// The type mappings in this mode are as follows:
106-
/// - `UInt8` is imported as `org.swift.swiftkit.core.primitives.UnsignedByte`
109+
/// - `UInt8` is imported as `com.google.common.primitives.UnsignedInteger`
107110
/// - `UInt16` is imported as `char` (this is always correct, since `char` is unsigned in Java)
108-
/// - `UInt32` is imported as `org.swift.swiftkit.core.primitives.UnsignedInteger`
109-
/// - `UInt64` is imported as `org.swift.swiftkit.core.primitives.UnsignedLong`
111+
/// - `UInt32` is imported as `com.google.common.primitives.UnsignedInteger`
112+
/// - `UInt64` is imported as `com.google.common.primitives.UnsignedLong`
110113
public enum UnsignedNumericsMode {
111114
case ignoreSign
112-
case wrapAsUnsignedNumbers
113-
}
115+
case wrapUnsignedGuava
116+
}

0 commit comments

Comments
 (0)