Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8159616

java.time.Instant falls through switch statement

XMLWordPrintable

    • 8
    • b126
    • Not verified

      In this code in java.time.Instant:

          public int get(TemporalField field) {
              if (field instanceof ChronoField) {
                  switch ((ChronoField) field) {
                      case NANO_OF_SECOND: return nanos;
                      case MICRO_OF_SECOND: return nanos / 1000;
                      case MILLI_OF_SECOND: return nanos / 1000_000;
                      case INSTANT_SECONDS: INSTANT_SECONDS.checkValidIntValue(seconds);
                  }
                  throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
              }
              return range(field).checkValidIntValue(field.getFrom(this), field);
          }

      the INSTANT_SECONDS block falls through. The result is that passing INSTANT_SECONDS to the method sometimes throws DateTimeException and sometimes throws UnsupportedTemporalTypeException.

      The correct behaviour (as per the spec) is to throw UnsupportedTemporalTypeException. As such the whole "case INSTANT_SECONDS" line should simply be removed.

      See also https://github.com/ThreeTen/threetenbp/issues/41 for context.

            bgopularam Bhanu Prakash Gopularam (Inactive)
            scolebourne Stephen Colebourne
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: