Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -1,7 +1,9 @@
package checks;

import java.time.Clock;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.chrono.JapaneseDate;
import java.util.Optional;

Expand All @@ -18,6 +20,15 @@ void nonCompliantIdentityChecks(LocalDateTime ldt1, LocalDateTime ldt2, Japanese
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
}

void compliantEnumIdentityChecks(Month month1, Month month2, DayOfWeek day1, DayOfWeek day2) {
boolean sameMonth = month1 == month2; // Compliant: enum types are an exception to the rule and can always be compared with '=='
boolean differentMonth = month1 != month2;
boolean sameDay = day1 == day2;
boolean differentDay = day1 != day2;
int monthHash = System.identityHashCode(month1);
int dayHash = System.identityHashCode(day1);
}

void compliantIdentityChecks(int i, int j, String s, String t, Integer k, Integer l, Clock c1, Clock c2) {
boolean b = i == j; // Compliant
boolean b1 = s == t; // Compliant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.chrono.HijrahDate;
import java.time.chrono.JapaneseDate;
import java.time.chrono.MinguoDate;
Expand Down Expand Up @@ -35,6 +37,10 @@ void foo() {

Clock clock = Clock.systemUTC();
synchronized (clock) { } // Compliant
Month month = Month.JANUARY;
synchronized (month) { } // Compliant: enum types are an exception and can be safely used here
DayOfWeek day = DayOfWeek.MONDAY;
synchronized (day) { }
Object object = new Object();
synchronized (object) { } // Compliant
DateTimeFormatterBuilder dateTimeFormatter = new DateTimeFormatterBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private TypeUtils() {

// Check if a type is a known value-based class.
public static boolean isValueBasedType(Type type) {
if (type.isUnknown() || type.is(JAVA_TIME_CLOCK)) {
if (type.isUnknown() || type.symbol().isEnum() || type.is(JAVA_TIME_CLOCK)) {
return false;
}
return type.isPrimitiveWrapper() || VALUE_BASED_TYPES.stream().anyMatch(type::is) || JAVA_TIME_PACKAGE_PATTERN.matcher(type.fullyQualifiedName()).matches();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ void isValueBasedType_returns_false_for_java_time_clock() {
assertThat(isValueBasedType("java.time.Clock c")).isFalse();
}

@Test
void isValueBasedType_returns_false_for_enum_types() {
assertThat(isValueBasedType("java.time.Month m")).isFalse();
assertThat(isValueBasedType("java.time.DayOfWeek d")).isFalse();
assertThat(isValueBasedType("java.util.concurrent.TimeUnit t")).isFalse();
}

@Test
void isValueBasedType_returns_false_for_non_value_based_types() {
assertThat(isValueBasedType("String s")).isFalse();
Expand Down
Loading