Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

public class EquatableClass: Equatable {
public let value: Int
public init(value: Int) {
self.value = value
}

public static func == (lhs: EquatableClass, rhs: EquatableClass) -> Bool {
lhs.value == rhs.value
}
}

public class EquatableSubclass: EquatableClass {
public override init(value: Int) {
super.init(value: value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftArena;

import static org.junit.jupiter.api.Assertions.*;

@SuppressWarnings({"AssertBetweenInconvertibleTypes", "EqualsWithItself"})
public class EquatableTest {
@Test
void genericStructType() {
try (var arena = SwiftArena.ofConfined()) {
var a = MyIDs.makeIntID(42, arena);
var b = MyIDs.makeIntID(42, arena);
var c = MyIDs.makeIntID(0, arena);
var d = MyIDs.makeStringID("42", arena);
assertEquals(a, a);
assertEquals(a, b);
assertNotEquals(a, c);
assertNotEquals(a, d);
assertNotEquals("foo", a);
}
}

@Test
void classType() {
try (var arena = SwiftArena.ofConfined()) {
var a = EquatableClass.init(42, arena);
var b = EquatableSubclass.init(42, arena);
var c = EquatableSubclass.init(0, arena);
assertEquals(a, b);
assertEquals(b, a);
assertEquals(b, b);
assertNotEquals(a, c);
assertNotEquals(b, c);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ extension JNISwift2JavaGenerator {

printer.print(
"""
public boolean equals(Object obj) {
if (obj instanceof JNISwiftInstance rhs) {
return SwiftObjects.equals(this.$memoryAddress(), this.$typeMetadataAddress(), rhs.$memoryAddress(), rhs.$typeMetadataAddress());
}
return false;
}

public java.lang.String toString() {
return SwiftObjects.toString(this.$memoryAddress(), this.$typeMetadataAddress());
}
Expand Down
37 changes: 37 additions & 0 deletions Sources/SwiftJava/SwiftObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,41 @@ extension SwiftObjects {
let typeMetadata = unsafeBitCast(selfType$, to: Any.Type.self)
return String(describing: typeMetadata)
}

@JavaMethod
public static func equals(environment: UnsafeMutablePointer<JNIEnv?>!, lhsPointer: Int64, lhsTypePointer: Int64, rhsPointer: Int64, rhsTypePointer: Int64) -> Bool {
guard let lhsType$ = UnsafeRawPointer(bitPattern: Int(lhsTypePointer)) else {
fatalError("lhsType metadata address was null")
}
let lhsMetatype = unsafeBitCast(lhsType$, to: Any.Type.self)
guard let lhsMetatype = lhsMetatype as? (any Equatable.Type) else {
return false
}

guard let rhsType$ = UnsafeRawPointer(bitPattern: Int(rhsTypePointer)) else {
fatalError("rhsType metadata address was null")
}
let rhsMetatype = unsafeBitCast(rhsType$, to: Any.Type.self)
guard let rhsMetatype = rhsMetatype as? (any Equatable.Type) else {
return false
}

func perform<L: Equatable, R: Equatable>(lhsType: L.Type, rhsType: R.Type) -> Bool {
guard let lhs$ = UnsafeMutablePointer<L>(bitPattern: Int(lhsPointer)) else {
fatalError("lhs memory address was null")
}
guard let rhs$ = UnsafeMutablePointer<R>(bitPattern: Int(rhsPointer)) else {
fatalError("rhs memory address was null")
}
if lhsType == rhsType {
return lhs$.pointee == rhs$.pointee as! L
} else if let lhs = lhs$.pointee as? R {
return lhs == rhs$.pointee
} else if let rhs = rhs$.pointee as? L {
return lhs$.pointee == rhs
}
return false
}
return perform(lhsType: lhsMetatype, rhsType: rhsMetatype)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public static void requireNonZero(long number, String name) {
public static native String toDebugString(long selfPointer, long selfTypePointer);
public static native void destroy(long selfPointer, long selfTypePointer);
public static native String typeDescription(long selfTypePointer);
public static native boolean equals(long lhsPointer, long lhsTypePointer, long rhsPointer, long rhsTypePointer);
}
Loading