2

The following code is from Oracle's JDK java.util.logging.LogRecord

public LogRecord(Level level, String msg) {
    // Make sure level isn't null, by calling random method.
    level.getClass();
    this.level = level;
    message = msg;
    // Assign a thread ID and a unique sequence number.
    sequenceNumber = globalSequenceNumber.getAndIncrement();
    threadID = defaultThreadID();
    millis = System.currentTimeMillis();
    needToInferCaller = true; }

Does someone knows if there are any good reason to do this

// Make sure level isn't null, by calling random method.
    level.getClass();

Instead of this?

if (level == null) throw new NullPointerException();
sstan
  • 35,425
  • 6
  • 48
  • 66
Davide Rossetto
  • 475
  • 3
  • 11
  • 2
    This is a really strange design solution. – Yassin Hajaj May 26 '16 at 23:02
  • I wouldn't even use your approach. Instead either [this](https://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#requireNonNull%28T%29) or [that](http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/base/Preconditions.html#checkNotNull%28T%29). This also throws the same exception, yes, but this also makes it perfectly clear (for the one who reads the code), that `null` is not a valid argument. You could also annotate your argument with `@NonNull`. – Tom May 26 '16 at 23:04
  • 3
    Honestly, there is no reason. Java Logging should practically never be used. This "pattern" is only used in JULI and nowhere else in the JDK. No-one could explain why that line of code is there like this, except the person that wrote it. This makes the question opinion-based. – Tunaki May 26 '16 at 23:32
  • why dont u switch to `SLF4J logging`(just asking) – bananas May 27 '16 at 07:33

0 Answers0