Class CalendarDetector
Calendar instances.
java.util.Calendar is not thread-safe. When multiple
threads share the same instance, operations like get(), set(),
add(), and getTime() can interleave, producing silently wrong
results — corrupted dates without any exception being thrown.
Common issues detected:
- Shared
Calendarinstance accessed by multiple threads - Calendar mutation (
set()/add()) during reads - Mixed-operation contention producing corrupted date values
Preferred thread-safe alternatives:
java.time.*(Java 8+) — all classes are immutable and thread-safeThreadLocal<Calendar>— one instance per thread- Synchronized access blocks (last resort)
Usage:
@AsyncTest(threads = 4, includes = DetectorType.CALENDAR)
void testCalendarUsage() {
Calendar cal = Calendar.getInstance();
AsyncTestContext.calendarDetector()
.registerCalendar(cal, "shared-calendar");
// This will be flagged — multiple threads sharing one Calendar
cal.set(Calendar.YEAR, 2024);
AsyncTestContext.calendarDetector()
.recordSet(cal, "shared-calendar");
}
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionanalyze()Analyse Calendar usage and return a report.voidRecord anadd()orroll()call.voidRecord anadd(field, amount)call, saying which field it added to.voidrecordError(Calendar calendar, String name, String errorType) Record a calendar operation error (e.g. corrupted return value).voidRecord aget()orgetTime()call.voidRecord aset()call.voidrecordSetTime(Calendar calendar, String name) Record asetTime()orsetTimeInMillis()call.voidregisterCalendar(Calendar calendar, String name) Register aCalendarinstance for monitoring.
-
Constructor Details
-
CalendarDetector
public CalendarDetector()
-
-
Method Details
-
registerCalendar
Register aCalendarinstance for monitoring.- Parameters:
calendar- the Calendar to monitorname- a descriptive label used in reports
-
recordGet
Record aget()orgetTime()call.- Parameters:
calendar- the Calendar instancename- the label (should match registration)
-
recordSet
Record aset()call.The next recorded
getthen counts as a write, which needs an exclusive lock: after aset()it recomputes the fields into the instance. Record asetTime()orsetTimeInMillis()withrecordSetTime(java.util.Calendar, java.lang.String)instead; recorded here, thegetafter it counts as a write too.- Parameters:
calendar- the Calendar instancename- the label (should match registration)
-
recordSetTime
Record asetTime()orsetTimeInMillis()call.A write, counted with the sets, but it computes every field at once, including the ones an earlier
set()left, so the next recordedgetonly reads (#820).- Parameters:
calendar- the Calendar instancename- the label (should match registration)- Since:
- 1.12.3
-
recordAdd
Record anadd()orroll()call.As with
recordSet(java.util.Calendar, java.lang.String), the next recordedgetthen counts as a write: anaddto a date field and most rolls leave the fields to recompute. Record anadd()withrecordAdd(Calendar, String, int)where the field is known.- Parameters:
calendar- the Calendar instancename- the label (should match registration)
-
recordAdd
Record anadd(field, amount)call, saying which field it added to.An
addtoCalendar.ERA,Calendar.YEARorCalendar.MONTHsets that field and leaves the rest to recompute, so the next recordedgetcounts as a write, as afterrecordSet(java.util.Calendar, java.lang.String). Anaddto any other field is carried out as asetTimeInMillis, which computes every field at once, so the nextgetonly reads, as afterrecordSetTime(java.util.Calendar, java.lang.String)(#820). Aroll()goes throughrecordAdd(Calendar, String).- Parameters:
calendar- the Calendar instancename- the label (should match registration)field- the field theaddwas made to, such asCalendar.HOUR- Since:
- 1.12.3
-
recordError
Record a calendar operation error (e.g. corrupted return value).- Parameters:
calendar- the Calendar instancename- the labelerrorType- a short description of the error
-
analyze
Analyse Calendar usage and return a report.- Returns:
- the findings this detector collected during the run
-