compareTo() doesn't throw ClassCastException with non-comparible object
- Dominant language
- Java
- Stars
- 6k
- Forks
- 999
- Avg merge
- 19h 20m
- Merged PRs (30d)
- 14
Description
Comparable.compareTo(o) is supposed to throw a ClassCastException if o isn't an instance of a Comparable type. The following test passes on the JVM, but fails with j2objc due to NSObject not declaring a compareTo: method:
```
import java.time.LocalDateTime;
class CompareTime {
private static LocalDateTime TEST_2007_07_15_12_30_40_987654321 =
LocalDateTime.of(2007, 7, 15, 12, 30, 40, 987654321);
public static void main(String... args) {
try {
Comparable c = TEST_2007_07_15_12_30_40_987654321;
c.compareTo(new Object());
System.out.println("failed");
} catch (ClassCastException e) {
System.out.println("passed");
} catch (Throwable t) {
System.out.println("failed");
}
}
}
```
The translator needs to add the equivalent of a Comparable cast, so this test passes:
```
c.compareTo((Comparable) new Object());
```
The correct code for the above is:
```
[((id) nil_chk(c)) compareToWithId:(id)
cast_check(create_NSObject_init(), JavaLangComparable_class_())];
```
Contributor guide
Assessment
This issue has not been assessed yet.