Skip to content

Commit 59878b7

Browse files
committed
Add Support for Final Fields
1 parent c5a3d4d commit 59878b7

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

Sources/Java2SwiftLib/JavaTranslator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ extension JavaTranslator {
546546
let fieldAttribute: AttributeSyntax = javaField.isStatic ? "@JavaStaticField" : "@JavaField";
547547
let swiftFieldName = javaField.getName().escapedSwiftName
548548
return """
549-
\(fieldAttribute)
549+
\(fieldAttribute)(isFinal: \(raw: javaField.isFinal))
550550
public var \(raw: swiftFieldName): \(raw: typeName)
551551
"""
552552
}

Sources/JavaKit/Macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public macro JavaInterface(_ fullClassName: String, extends: (any AnyJavaObject.
9292
/// }
9393
/// ```
9494
@attached(accessor)
95-
public macro JavaField(_ javaFieldName: String? = nil) = #externalMacro(module: "JavaKitMacros", type: "JavaFieldMacro")
95+
public macro JavaField(_ javaFieldName: String? = nil, isFinal: Bool = false) = #externalMacro(module: "JavaKitMacros", type: "JavaFieldMacro")
9696

9797

9898
/// Attached macro that turns a Swift property into one that accesses a Java static field on the underlying Java object.
@@ -106,7 +106,7 @@ public macro JavaField(_ javaFieldName: String? = nil) = #externalMacro(module:
106106
/// }
107107
/// ```
108108
@attached(accessor)
109-
public macro JavaStaticField(_ javaFieldName: String? = nil) = #externalMacro(module: "JavaKitMacros", type: "JavaFieldMacro")
109+
public macro JavaStaticField(_ javaFieldName: String? = nil, isFinal: Bool = false) = #externalMacro(module: "JavaKitMacros", type: "JavaFieldMacro")
110110

111111
/// Attached macro that turns a Swift method into one that wraps a Java method on the underlying Java object.
112112
///

Sources/JavaKitMacros/JavaFieldMacro.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,31 @@ extension JavaFieldMacro: AccessorMacro {
4646
fieldNameAsWritten
4747
}
4848

49+
let createSetter =
50+
if case .argumentList(let arguments) = node.arguments,
51+
let wrapperIsBoolean = arguments.first(where: { $0.label?.text == "isFinal" })?.expression,
52+
let booleanLiteral = wrapperIsBoolean.as(BooleanLiteralExprSyntax.self)
53+
{
54+
booleanLiteral.literal.text == "false" // Create the setter if we are not final
55+
} else {
56+
true
57+
}
58+
4959
let getter: AccessorDeclSyntax = """
5060
get { self[javaFieldName: \(literal: fieldName), fieldType: \(fieldType).self] }
5161
"""
5262

53-
let setter: AccessorDeclSyntax = """
54-
nonmutating set { self[javaFieldName: \(literal: fieldName), fieldType: \(fieldType).self] = newValue }
55-
"""
63+
var accessors: [AccessorDeclSyntax] = [
64+
getter
65+
]
66+
67+
if createSetter {
68+
let setter: AccessorDeclSyntax = """
69+
nonmutating set { self[javaFieldName: \(literal: fieldName), fieldType: \(fieldType).self] = newValue }
70+
"""
71+
accessors.append(setter)
72+
}
5673

57-
return [getter, setter]
74+
return accessors
5875
}
5976
}

Sources/JavaKitReflection/Field+Utilities.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ extension Field {
1717
public var isStatic: Bool {
1818
return (getModifiers() & 0x08) != 0
1919
}
20+
21+
/// Whether this is a 'final' field.
22+
public var isFinal: Bool {
23+
return (getModifiers() & 16) != 0
24+
}
2025
}

Tests/Java2SwiftTests/Java2SwiftTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Java2SwiftTests: XCTestCase {
7575
}
7676
""",
7777
"""
78-
@JavaStaticField
78+
@JavaStaticField(isFinal: true)
7979
public var APRIL: Month?
8080
"""
8181
])

Tests/JavaKitMacroTests/JavaClassMacroTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class JavaKitMacroTests: XCTestCase {
4141
4242
@JavaField
4343
public var myField: Int64
44+
45+
@JavaField(isFinal: true)
46+
public var myFinalField: Int64
4447
}
4548
""",
4649
expandedSource: """
@@ -73,6 +76,11 @@ class JavaKitMacroTests: XCTestCase {
7376
self[javaFieldName: "myField", fieldType: Int64.self] = newValue
7477
}
7578
}
79+
public var myFinalField: Int64 {
80+
get {
81+
self[javaFieldName: "myFinalField", fieldType: Int64.self]
82+
}
83+
}
7684
7785
/// The full Java class name for this Swift type.
7886
public static var fullJavaClassName: String {

0 commit comments

Comments
 (0)