Skip to content

Commit 1ed22ce

Browse files
committed
fix duplicated fields from superclass
in case of duplicated properties, e.g. from a base class it was generating duplicate property definitions, e.g.: ``` extension JavaClass<PrivateKey> { @JavaStaticField(isFinal: true) public var serialVersionUID: Int64 @JavaStaticField(isFinal: true) public var serialVersionUID: Int64 } ``` (see e.g. https://docs.oracle.com/javase/8/docs/api/java/security/PrivateKey.html) which then fails the swift build. Analog to methods, this now skips the property in case it wasn't directly from the class itself.
1 parent 0685f55 commit 1ed22ce

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

Sources/SwiftJavaToolLib/JavaClassTranslator.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ extension JavaClassTranslator {
270270

271271
/// Add a field to the appropriate lists(s) for later translation.
272272
private mutating func addField(_ field: Field) {
273+
// Don't include inherited fields when translating to a class.
274+
// This applies to both instance and static fields to avoid duplicates
275+
if translateAsClass &&
276+
!field.getDeclaringClass()!.equals(javaClass.as(JavaObject.self)!) {
277+
return
278+
}
279+
273280
// Static fields go into a separate list.
274281
if field.isStatic {
275282
staticFields.append(field)
@@ -283,12 +290,6 @@ extension JavaClassTranslator {
283290
return
284291
}
285292

286-
// Don't include inherited fields when translating to a class.
287-
if translateAsClass &&
288-
!field.getDeclaringClass()!.equals(javaClass.as(JavaObject.self)!) {
289-
return
290-
}
291-
292293
fields.append(field)
293294
}
294295

Tests/SwiftJavaToolLibTests/WrapJavaTests/BasicWrapJavaTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,45 @@ final class BasicWrapJavaTests: XCTestCase {
8888
)
8989
}
9090

91+
92+
// Test that static fields from superclasses are not duplicated in generated code.
93+
// This prevents duplicate serialVersionUID declarations when both a class and its
94+
// superclass declare the field. The subclass field "hides" the superclass field,
95+
// similar to how static methods work in Java.
96+
func test_wrapJava_noDuplicateStaticFieldsFromSuperclass() async throws {
97+
let classpathURL = try await compileJava(
98+
"""
99+
package com.example;
100+
101+
class SuperClass {
102+
public static final long serialVersionUID = 1L;
103+
}
104+
105+
class SubClass extends SuperClass {
106+
public static final long serialVersionUID = 2L;
107+
}
108+
""")
109+
110+
try assertWrapJavaOutput(
111+
javaClassNames: [
112+
"com.example.SuperClass",
113+
"com.example.SubClass"
114+
],
115+
classpath: [classpathURL],
116+
expectedChunks: [
117+
// SuperClass should have its static field
118+
"""
119+
extension JavaClass<SuperClass> {
120+
@JavaStaticField(isFinal: true)
121+
public var serialVersionUID: Int64
122+
""",
123+
// SubClass should only have its own static field, not the superclass one
124+
"""
125+
extension JavaClass<SubClass> {
126+
@JavaStaticField(isFinal: true)
127+
public var serialVersionUID: Int64
128+
""",
129+
]
130+
)
131+
}
91132
}

0 commit comments

Comments
 (0)