Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nullability annotations to LoggingEvent #927

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions logback-classic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.util.EnvUtil;
import ch.qos.logback.core.util.StringUtil;

import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.slf4j.Marker;
import org.slf4j.event.KeyValuePair;
import org.slf4j.helpers.MessageFormatter;
Expand Down Expand Up @@ -150,46 +153,46 @@ public LoggingEvent(String fqcn, Logger logger, Level level, String message, Thr
}
}

void initTmestampFields(Instant instant) {
void initTmestampFields(@NonNull Instant instant) {
this.instant = instant;
long epochSecond = instant.getEpochSecond();
this.nanoseconds = instant.getNano();
long milliseconds = nanoseconds / 1000_000;
this.timeStamp = (epochSecond * 1000) + (milliseconds);
}

private Throwable extractThrowableAnRearrangeArguments(Object[] argArray) {
private @Nullable Throwable extractThrowableAnRearrangeArguments(@Nullable Object[] argArray) {
Throwable extractedThrowable = EventArgUtil.extractThrowable(argArray);
if (EventArgUtil.successfulExtraction(extractedThrowable)) {
this.argumentArray = EventArgUtil.trimmedCopy(argArray);
}
return extractedThrowable;
}

public void setArgumentArray(Object[] argArray) {
public void setArgumentArray(@Nullable Object[] argArray) {
if (this.argumentArray != null) {
throw new IllegalStateException("argArray has been already set");
}
this.argumentArray = argArray;
}

public Object[] getArgumentArray() {
public @Nullable Object[] getArgumentArray() {
return this.argumentArray;
}

public void addKeyValuePair(KeyValuePair kvp) {
public void addKeyValuePair(@NonNull KeyValuePair kvp) {
if (keyValuePairs == null) {
keyValuePairs = new ArrayList<>(4);
}
keyValuePairs.add(kvp);
}

public void setKeyValuePairs(List<KeyValuePair> kvpList) {
public void setKeyValuePairs(@Nullable List<KeyValuePair> kvpList) {
this.keyValuePairs = kvpList;
}

@Override
public List<KeyValuePair> getKeyValuePairs() {
public @Nullable List<KeyValuePair> getKeyValuePairs() {
return this.keyValuePairs;
}

Expand All @@ -205,7 +208,7 @@ public void setLoggerName(String loggerName) {
this.loggerName = loggerName;
}

public String getThreadName() {
public @NonNull String getThreadName() {
if (threadName == null) {
threadName = extractThreadName(Thread.currentThread());
}
Expand All @@ -221,7 +224,7 @@ public String getThreadName() {
* @return
* @since 1.5.0
*/
private String extractThreadName(Thread aThread) {
private @NonNull String extractThreadName(@Nullable Thread aThread) {
if (aThread == null) {
return CoreConstants.NA;
}
Expand All @@ -243,7 +246,7 @@ private String extractThreadName(Thread aThread) {
* @param aThread
* @return Return the threadId if the thread is a virtual thread, return null otherwise.
*/
Long getVirtualThreadId(Thread aThread) {
Long getVirtualThreadId(@NonNull Thread aThread) {
if (EnvUtil.isJDK21OrHigher()) {
try {
Method isVirtualMethod = Thread.class.getMethod("isVirtual");
Expand Down Expand Up @@ -396,7 +399,7 @@ public void setLevel(Level level) {
* Note that after serialization it is impossible to correctly extract caller information.
* </p>
*/
public StackTraceElement[] getCallerData() {
public @NonNull StackTraceElement[] getCallerData() {
if (callerDataArray == null) {
callerDataArray = CallerData.extract(new Throwable(), fqnOfLoggerClass,
loggerContext.getMaxCallerDataDepth(), loggerContext.getFrameworkPackages());
Expand All @@ -408,15 +411,15 @@ public boolean hasCallerData() {
return (callerDataArray != null);
}

public void setCallerData(StackTraceElement[] callerDataArray) {
public void setCallerData(@Nullable StackTraceElement[] callerDataArray) {
this.callerDataArray = callerDataArray;
}

public List<Marker> getMarkerList() {
public @Nullable List<Marker> getMarkerList() {
return markerList;
}

public void addMarker(Marker marker) {
public void addMarker(@Nullable Marker marker) {
if (marker == null) {
return;
}
Expand Down Expand Up @@ -450,7 +453,7 @@ public String getFormattedMessage() {
return formattedMessage;
}

public Map<String, String> getMDCPropertyMap() {
public @NonNull Map<String, String> getMDCPropertyMap() {
// populate mdcPropertyMap if null
if (mdcPropertyMap == null) {
MDCAdapter mdcAdapter = loggerContext.getMDCAdapter();
Expand All @@ -472,7 +475,7 @@ public Map<String, String> getMDCPropertyMap() {
* @param map
* @since 1.0.8
*/
public void setMDCPropertyMap(Map<String, String> map) {
public void setMDCPropertyMap(@Nullable Map<String, String> map) {
if (mdcPropertyMap != null) {
throw new IllegalStateException("The MDCPropertyMap has been already set for this event.");
}
Expand All @@ -485,7 +488,7 @@ public void setMDCPropertyMap(Map<String, String> map) {
*
* @deprecated Replaced by [@link #getMDCPropertyMap}
*/
public Map<String, String> getMdc() {
public @NonNull Map<String, String> getMdc() {
return getMDCPropertyMap();
}

Expand Down
3 changes: 2 additions & 1 deletion logback-classic/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
requires org.slf4j;

requires ch.qos.logback.core;
provides org.slf4j.spi.SLF4JServiceProvider with ch.qos.logback.classic.spi.LogbackServiceProvider;
requires org.jspecify;
provides org.slf4j.spi.SLF4JServiceProvider with ch.qos.logback.classic.spi.LogbackServiceProvider;
uses ch.qos.logback.classic.spi.Configurator;

exports ch.qos.logback.classic;
Expand Down