File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -429,20 +429,21 @@ extension JavaTranslator {
429
429
}
430
430
431
431
let throwsStr = javaMethod. throwsCheckedException ? " throws " : " "
432
-
432
+ let swiftMethodName = javaMethod . getName ( ) . escapedSwiftName
433
433
let methodAttribute : AttributeSyntax = javaMethod. isStatic ? " @JavaStaticMethod " : " @JavaMethod " ;
434
434
return """
435
435
\( methodAttribute)
436
- public func \( raw: javaMethod . getName ( ) ) \( raw: genericParameterClause) ( \( raw: parametersStr) ) \( raw: throwsStr) \( raw: resultTypeStr) \( raw: whereClause)
436
+ public func \( raw: swiftMethodName ) \( raw: genericParameterClause) ( \( raw: parametersStr) ) \( raw: throwsStr) \( raw: resultTypeStr) \( raw: whereClause)
437
437
"""
438
438
}
439
439
440
440
package func translateField( _ javaField: Field ) throws -> DeclSyntax {
441
441
let typeName = try getSwiftTypeNameAsString ( javaField. getGenericType ( ) !, outerOptional: true )
442
442
let fieldAttribute : AttributeSyntax = javaField. isStatic ? " @JavaStaticField " : " @JavaField " ;
443
+ let swiftFieldName = javaField. getName ( ) . escapedSwiftName
443
444
return """
444
445
\( fieldAttribute)
445
- public var \( raw: javaField . getName ( ) ) : \( raw: typeName)
446
+ public var \( raw: swiftFieldName ) : \( raw: typeName)
446
447
"""
447
448
}
448
449
Original file line number Diff line number Diff line change 11
11
// SPDX-License-Identifier: Apache-2.0
12
12
//
13
13
//===----------------------------------------------------------------------===//
14
+ import SwiftParser
14
15
15
16
extension String {
16
17
/// Split the Swift type name into parent type + innermost type name.
@@ -24,4 +25,13 @@ extension String {
24
25
name: String ( suffix ( from: index ( after: lastDot) ) )
25
26
)
26
27
}
28
+
29
+ /// Escape a name with backticks if it's a Swift keyword.
30
+ var escapedSwiftName : String {
31
+ if isValidSwiftIdentifier ( for: . variableName) {
32
+ return self
33
+ }
34
+
35
+ return " ` \( self ) ` "
36
+ }
27
37
}
You can’t perform that action at this time.
0 commit comments