Class CalendarDetector

java.lang.Object
se.deversity.asynctest.diagnostics.CalendarDetector

public class CalendarDetector extends Object
Detects concurrent use of non-thread-safe 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 Calendar instance 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-safe
  • ThreadLocal<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");
 }
 
  • Constructor Details

    • CalendarDetector

      public CalendarDetector()
  • Method Details

    • registerCalendar

      public void registerCalendar(Calendar calendar, String name)
      Register a Calendar instance for monitoring.
      Parameters:
      calendar - the Calendar to monitor
      name - a descriptive label used in reports
    • recordGet

      public void recordGet(Calendar calendar, String name)
      Record a get() or getTime() call.
      Parameters:
      calendar - the Calendar instance
      name - the label (should match registration)
    • recordSet

      public void recordSet(Calendar calendar, String name)
      Record a set() call.

      The next recorded get then counts as a write, which needs an exclusive lock: after a set() it recomputes the fields into the instance. Record a setTime() or setTimeInMillis() with recordSetTime(java.util.Calendar, java.lang.String) instead; recorded here, the get after it counts as a write too.

      Parameters:
      calendar - the Calendar instance
      name - the label (should match registration)
    • recordSetTime

      public void recordSetTime(Calendar calendar, String name)
      Record a setTime() or setTimeInMillis() call.

      A write, counted with the sets, but it computes every field at once, including the ones an earlier set() left, so the next recorded get only reads (#820).

      Parameters:
      calendar - the Calendar instance
      name - the label (should match registration)
      Since:
      1.12.3
    • recordAdd

      public void recordAdd(Calendar calendar, String name)
      Record an add() or roll() call.

      As with recordSet(java.util.Calendar, java.lang.String), the next recorded get then counts as a write: an add to a date field and most rolls leave the fields to recompute. Record an add() with recordAdd(Calendar, String, int) where the field is known.

      Parameters:
      calendar - the Calendar instance
      name - the label (should match registration)
    • recordAdd

      public void recordAdd(Calendar calendar, String name, int field)
      Record an add(field, amount) call, saying which field it added to.

      An add to Calendar.ERA, Calendar.YEAR or Calendar.MONTH sets that field and leaves the rest to recompute, so the next recorded get counts as a write, as after recordSet(java.util.Calendar, java.lang.String). An add to any other field is carried out as a setTimeInMillis, which computes every field at once, so the next get only reads, as after recordSetTime(java.util.Calendar, java.lang.String) (#820). A roll() goes through recordAdd(Calendar, String).

      Parameters:
      calendar - the Calendar instance
      name - the label (should match registration)
      field - the field the add was made to, such as Calendar.HOUR
      Since:
      1.12.3
    • recordError

      public void recordError(Calendar calendar, String name, String errorType)
      Record a calendar operation error (e.g. corrupted return value).
      Parameters:
      calendar - the Calendar instance
      name - the label
      errorType - a short description of the error
    • analyze

      Analyse Calendar usage and return a report.
      Returns:
      the findings this detector collected during the run