Skip to content

Commit e6acb16

Browse files
committed
prefix boolean variables with is to match Java conventions
1 parent 29f3d18 commit e6acb16

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

Sources/JExtractSwiftLib/Convenience/String+Extensions.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,32 @@ extension String {
2222

2323
return "\(f.uppercased())\(String(dropFirst()))"
2424
}
25-
}
25+
26+
/// Returns whether the string is of the format `isX`
27+
private var hasJavaBooleanNamingConvention: Bool {
28+
guard self.hasPrefix("is"), self.count > 2 else {
29+
return false
30+
}
31+
32+
let thirdCharacterIndex = self.index(self.startIndex, offsetBy: 2)
33+
return self[thirdCharacterIndex].isUppercase
34+
}
35+
36+
func javaGetterName(isBoolean: Bool) -> String {
37+
if !isBoolean {
38+
return "get\(self.toCamelCase)"
39+
} else if !hasJavaBooleanNamingConvention {
40+
return "is\(self.toCamelCase)"
41+
} else {
42+
return self.toCamelCase
43+
}
44+
}
45+
46+
func javaSetterName(isBoolean: Bool) -> String {
47+
if !isBoolean || !hasJavaBooleanNamingConvention {
48+
return "set\(self.toCamelCase)"
49+
} else {
50+
return "setIs\(self.toCamelCase)"
51+
}
52+
}
53+
}

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ extension FFMSwift2JavaGenerator {
126126
let loweredSignature = try lowering.lowerFunctionSignature(decl.functionSignature)
127127

128128
// Name.
129+
let returnsBoolean = decl.functionSignature.result.type.asNominalTypeDeclaration?.knownTypeKind == .bool
129130
let javaName = switch decl.apiKind {
130-
case .getter: "get\(decl.name.toCamelCase)"
131-
case .setter: "set\(decl.name.toCamelCase)"
131+
case .getter: decl.name.javaGetterName(isBoolean: returnsBoolean)
132+
case .setter: decl.name.javaSetterName(isBoolean: returnsBoolean)
132133
case .function, .initializer: decl.name
133134
}
134135

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
9191
}
9292
return false
9393
}
94-
94+
9595
/// If this function/method is member of a class/struct/protocol,
9696
/// this will contain that declaration's imported name.
9797
///

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaTranslation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ extension JNISwift2JavaGenerator {
3838
let parentName = decl.parentType?.asNominalType?.nominalTypeDecl.qualifiedName ?? swiftModuleName
3939

4040
// Name.
41+
let returnsBoolean = translatedFunctionSignature.resultType == .boolean
4142
let javaName = switch decl.apiKind {
42-
case .getter: "get\(decl.name.toCamelCase)"
43-
case .setter: "set\(decl.name.toCamelCase)"
43+
case .getter: decl.name.javaGetterName(isBoolean: returnsBoolean)
44+
case .setter: decl.name.javaSetterName(isBoolean: returnsBoolean)
4445
case .function, .initializer: decl.name
4546
}
4647

0 commit comments

Comments
 (0)