Skip to content

Commit e054f36

Browse files
committed
move JavaType equality extensions to separate file
1 parent 94c70c3 commit e054f36

File tree

2 files changed

+135
-115
lines changed

2 files changed

+135
-115
lines changed

Sources/SwiftJavaToolLib/JavaClassTranslator.swift

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -939,118 +939,3 @@ extension [Type?] {
939939
}
940940
}
941941

942-
extension Type {
943-
/// Adjust the given type to use its bounds, mirroring what we do in
944-
/// mapping Java types into Swift.
945-
func adjustToJavaBounds(adjusted: inout Bool) -> Type {
946-
if let typeVariable = self.as(TypeVariable<GenericDeclaration>.self),
947-
typeVariable.getBounds().count == 1,
948-
let bound = typeVariable.getBounds()[0] {
949-
adjusted = true
950-
return bound
951-
}
952-
953-
if let wildcardType = self.as(WildcardType.self),
954-
wildcardType.getUpperBounds().count == 1,
955-
let bound = wildcardType.getUpperBounds()[0] {
956-
adjusted = true
957-
return bound
958-
}
959-
960-
return self
961-
}
962-
963-
/// Determine whether this type is equivalent to or a subtype of the other
964-
/// type.
965-
func isEqualTo(_ other: Type, file: String = #file, line: Int = #line, function: String = #function) -> Bool {
966-
if self.javaHolder.object == other.javaHolder.object {
967-
return true
968-
}
969-
970-
// First, adjust types to their bounds, if we need to.
971-
var anyAdjusted: Bool = false
972-
let adjustedSelf = self.adjustToJavaBounds(adjusted: &anyAdjusted)
973-
let adjustedOther = other.adjustToJavaBounds(adjusted: &anyAdjusted)
974-
if anyAdjusted {
975-
return adjustedSelf.isEqualTo(adjustedOther)
976-
}
977-
978-
// If both are classes, check for equivalence.
979-
if let selfClass = self.as(JavaClass<JavaObject>.self),
980-
let otherClass = other.as(JavaClass<JavaObject>.self) {
981-
return selfClass.equals(otherClass.as(JavaObject.self))
982-
}
983-
984-
// If both are arrays, check that their component types are equivalent.
985-
if let selfArray = self.as(GenericArrayType.self),
986-
let otherArray = other.as(GenericArrayType.self) {
987-
return selfArray.getGenericComponentType().isEqualTo(otherArray.getGenericComponentType())
988-
}
989-
990-
// If both are parameterized types, check their raw type and type
991-
// arguments for equivalence.
992-
if let selfParameterizedType = self.as(ParameterizedType.self),
993-
let otherParameterizedType = other.as(ParameterizedType.self) {
994-
if !selfParameterizedType.getRawType().isEqualTo(otherParameterizedType.getRawType()) {
995-
return false
996-
}
997-
998-
return selfParameterizedType.getActualTypeArguments()
999-
.allTypesEqual(otherParameterizedType.getActualTypeArguments())
1000-
}
1001-
1002-
// If both are type variables, compare their bounds.
1003-
// FIXME: This is a hack.
1004-
if let selfTypeVariable = self.as(TypeVariable<GenericDeclaration>.self),
1005-
let otherTypeVariable = other.as(TypeVariable<GenericDeclaration>.self) {
1006-
return selfTypeVariable.getBounds().allTypesEqual(otherTypeVariable.getBounds())
1007-
}
1008-
1009-
// If both are wildcards, compare their upper and lower bounds.
1010-
if let selfWildcard = self.as(WildcardType.self),
1011-
let otherWildcard = other.as(WildcardType.self) {
1012-
return selfWildcard.getUpperBounds().allTypesEqual(otherWildcard.getUpperBounds())
1013-
&& selfWildcard.getLowerBounds().allTypesEqual(otherWildcard.getLowerBounds())
1014-
}
1015-
1016-
return false
1017-
}
1018-
1019-
/// Determine whether this type is equivalent to or a subtype of the
1020-
/// other type.
1021-
func isEqualToOrSubtypeOf(_ other: Type) -> Bool {
1022-
// First, adjust types to their bounds, if we need to.
1023-
var anyAdjusted: Bool = false
1024-
let adjustedSelf = self.adjustToJavaBounds(adjusted: &anyAdjusted)
1025-
let adjustedOther = other.adjustToJavaBounds(adjusted: &anyAdjusted)
1026-
if anyAdjusted {
1027-
return adjustedSelf.isEqualToOrSubtypeOf(adjustedOther)
1028-
}
1029-
1030-
if isEqualTo(other) {
1031-
return true
1032-
}
1033-
1034-
// If both are classes, check for subclassing.
1035-
if let selfClass = self.as(JavaClass<JavaObject>.self),
1036-
let otherClass = other.as(JavaClass<JavaObject>.self) {
1037-
// If either is a Java array, then this cannot be a subtype relationship
1038-
// in Swift.
1039-
if selfClass.isArray() || otherClass.isArray() {
1040-
return false
1041-
}
1042-
1043-
return selfClass.isSubclass(of: otherClass)
1044-
}
1045-
1046-
// Anything object-like is a subclass of java.lang.Object
1047-
if let otherClass = other.as(JavaClass<JavaObject>.self),
1048-
otherClass.getName() == "java.lang.Object" {
1049-
if self.is(GenericArrayType.self) || self.is(ParameterizedType.self) ||
1050-
self.is(WildcardType.self) || self.is(TypeVariable<GenericDeclaration>.self) {
1051-
return true
1052-
}
1053-
}
1054-
return false
1055-
}
1056-
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftJava
16+
import JavaLangReflect
17+
import SwiftSyntax
18+
import SwiftJavaConfigurationShared
19+
import Logging
20+
21+
extension Type {
22+
/// Adjust the given type to use its bounds, mirroring what we do in
23+
/// mapping Java types into Swift.
24+
func adjustToJavaBounds(adjusted: inout Bool) -> Type {
25+
if let typeVariable = self.as(TypeVariable<GenericDeclaration>.self),
26+
typeVariable.getBounds().count == 1,
27+
let bound = typeVariable.getBounds()[0] {
28+
adjusted = true
29+
return bound
30+
}
31+
32+
if let wildcardType = self.as(WildcardType.self),
33+
wildcardType.getUpperBounds().count == 1,
34+
let bound = wildcardType.getUpperBounds()[0] {
35+
adjusted = true
36+
return bound
37+
}
38+
39+
return self
40+
}
41+
42+
/// Determine whether this type is equivalent to or a subtype of the other
43+
/// type.
44+
func isEqualTo(_ other: Type, file: String = #file, line: Int = #line, function: String = #function) -> Bool {
45+
if self.javaHolder.object == other.javaHolder.object {
46+
return true
47+
}
48+
49+
// First, adjust types to their bounds, if we need to.
50+
var anyAdjusted: Bool = false
51+
let adjustedSelf = self.adjustToJavaBounds(adjusted: &anyAdjusted)
52+
let adjustedOther = other.adjustToJavaBounds(adjusted: &anyAdjusted)
53+
if anyAdjusted {
54+
return adjustedSelf.isEqualTo(adjustedOther)
55+
}
56+
57+
// If both are classes, check for equivalence.
58+
if let selfClass = self.as(JavaClass<JavaObject>.self),
59+
let otherClass = other.as(JavaClass<JavaObject>.self) {
60+
return selfClass.equals(otherClass.as(JavaObject.self))
61+
}
62+
63+
// If both are arrays, check that their component types are equivalent.
64+
if let selfArray = self.as(GenericArrayType.self),
65+
let otherArray = other.as(GenericArrayType.self) {
66+
return selfArray.getGenericComponentType().isEqualTo(otherArray.getGenericComponentType())
67+
}
68+
69+
// If both are parameterized types, check their raw type and type
70+
// arguments for equivalence.
71+
if let selfParameterizedType = self.as(ParameterizedType.self),
72+
let otherParameterizedType = other.as(ParameterizedType.self) {
73+
if !selfParameterizedType.getRawType().isEqualTo(otherParameterizedType.getRawType()) {
74+
return false
75+
}
76+
77+
return selfParameterizedType.getActualTypeArguments()
78+
.allTypesEqual(otherParameterizedType.getActualTypeArguments())
79+
}
80+
81+
// If both are type variables, compare their bounds.
82+
// FIXME: This is a hack.
83+
if let selfTypeVariable = self.as(TypeVariable<GenericDeclaration>.self),
84+
let otherTypeVariable = other.as(TypeVariable<GenericDeclaration>.self) {
85+
return selfTypeVariable.getBounds().allTypesEqual(otherTypeVariable.getBounds())
86+
}
87+
88+
// If both are wildcards, compare their upper and lower bounds.
89+
if let selfWildcard = self.as(WildcardType.self),
90+
let otherWildcard = other.as(WildcardType.self) {
91+
return selfWildcard.getUpperBounds().allTypesEqual(otherWildcard.getUpperBounds())
92+
&& selfWildcard.getLowerBounds().allTypesEqual(otherWildcard.getLowerBounds())
93+
}
94+
95+
return false
96+
}
97+
98+
/// Determine whether this type is equivalent to or a subtype of the
99+
/// other type.
100+
func isEqualToOrSubtypeOf(_ other: Type) -> Bool {
101+
// First, adjust types to their bounds, if we need to.
102+
var anyAdjusted: Bool = false
103+
let adjustedSelf = self.adjustToJavaBounds(adjusted: &anyAdjusted)
104+
let adjustedOther = other.adjustToJavaBounds(adjusted: &anyAdjusted)
105+
if anyAdjusted {
106+
return adjustedSelf.isEqualToOrSubtypeOf(adjustedOther)
107+
}
108+
109+
if isEqualTo(other) {
110+
return true
111+
}
112+
113+
// If both are classes, check for subclassing.
114+
if let selfClass = self.as(JavaClass<JavaObject>.self),
115+
let otherClass = other.as(JavaClass<JavaObject>.self) {
116+
// If either is a Java array, then this cannot be a subtype relationship
117+
// in Swift.
118+
if selfClass.isArray() || otherClass.isArray() {
119+
return false
120+
}
121+
122+
return selfClass.isSubclass(of: otherClass)
123+
}
124+
125+
// Anything object-like is a subclass of java.lang.Object
126+
if let otherClass = other.as(JavaClass<JavaObject>.self),
127+
otherClass.getName() == "java.lang.Object" {
128+
if self.is(GenericArrayType.self) || self.is(ParameterizedType.self) ||
129+
self.is(WildcardType.self) || self.is(TypeVariable<GenericDeclaration>.self) {
130+
return true
131+
}
132+
}
133+
return false
134+
}
135+
}

0 commit comments

Comments
 (0)