Skip to content

Commit c9b9107

Browse files
author
Cyrille Le Clerc
committed
Use SpotBugs
1 parent 6a6137a commit c9b9107

File tree

7 files changed

+52
-20
lines changed

7 files changed

+52
-20
lines changed

pom.xml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
</scm>
5858
<dependencies>
5959
<dependency>
60-
<groupId>com.google.code.findbugs</groupId>
61-
<artifactId>jsr305</artifactId>
62-
<version>3.0.0</version>
60+
<groupId>com.github.spotbugs</groupId>
61+
<artifactId>spotbugs-annotations</artifactId>
62+
<version>3.1.10</version>
6363
<optional>true</optional>
6464
</dependency>
6565
<dependency>
@@ -92,6 +92,20 @@
9292
<version>4.0.0</version>
9393
<extensions>true</extensions>
9494
</plugin>
95+
<plugin>
96+
<groupId>com.github.spotbugs</groupId>
97+
<artifactId>spotbugs-maven-plugin</artifactId>
98+
<version>3.1.10</version>
99+
<executions>
100+
<execution>
101+
<id>analyze-compile</id>
102+
<phase>compile</phase>
103+
<goals>
104+
<goal>check</goal>
105+
</goals>
106+
</execution>
107+
</executions>
108+
</plugin>
95109
</plugins>
96110
</build>
97111
</project>

src/main/java/com/cloudbees/syslog/SDElement.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.Serializable;
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.Objects;
2324

@@ -33,11 +34,7 @@ public class SDElement implements Serializable {
3334
/**
3435
* Reserved SD-IDs as documented in <a href="https://www.rfc-editor.org/rfc/rfc5424.txt">RFC-5424</a>
3536
*/
36-
public static final String[] RESERVED_SDID = new String[]{
37-
"timeQuality",
38-
"origin",
39-
"meta"
40-
};
37+
private final List<String> RESERVED_SDID = Collections.unmodifiableList(Arrays.asList("timeQuality", "origin", "meta"));
4138

4239
public SDElement(String sdID) {
4340
validateSDID(sdID);

src/main/java/com/cloudbees/syslog/SyslogMessage.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected String newObject() {
9090

9191
private Facility facility;
9292
private Severity severity;
93-
private Date timestamp;
93+
private Long timestamp;
9494
private String hostname;
9595
private String appName;
9696
private String procId;
@@ -129,20 +129,20 @@ public SyslogMessage withSeverity(Severity severity) {
129129
}
130130

131131
public Date getTimestamp() {
132-
return timestamp;
132+
return timestamp == null ? null : new Date(timestamp);
133133
}
134134

135135
public void setTimestamp(Date timestamp) {
136-
this.timestamp = timestamp;
136+
this.timestamp = (timestamp == null ? null : timestamp.getTime());
137137
}
138138

139139
public SyslogMessage withTimestamp(long timestamp) {
140-
this.timestamp = new Date(timestamp);
140+
this.timestamp = timestamp;
141141
return this;
142142
}
143143

144144
public SyslogMessage withTimestamp(Date timestamp) {
145-
this.timestamp = timestamp;
145+
this.timestamp = (timestamp == null ? null : timestamp.getTime());
146146
return this;
147147
}
148148

@@ -336,7 +336,7 @@ public void toRfc5424SyslogMessage(Writer out) throws IOException {
336336
out.write('>');
337337
out.write('1'); // version
338338
out.write(SP);
339-
out.write(rfc3339DateFormat.format(timestamp == null ? new Date() : timestamp)); // message time
339+
out.write(rfc3339DateFormat.format(timestamp == null ? new Date() : new Date(timestamp))); // message time
340340
out.write(SP);
341341
out.write(hostname == null ? localhostNameReference.get() : hostname); // emitting server hostname
342342
out.write(SP);
@@ -379,7 +379,7 @@ public void toRfc3164SyslogMessage(Writer out) throws IOException {
379379
out.write('<');
380380
out.write(Integer.toString(pri));
381381
out.write('>');
382-
out.write(rfc3164DateFormat.format(timestamp == null ? new Date() : timestamp)); // message time
382+
out.write(rfc3164DateFormat.format(timestamp == null ? new Date() : new Date(timestamp))); // message time
383383
out.write(SP);
384384
out.write((hostname == null) ? localhostNameReference.get() : hostname); // emitting server hostname
385385
out.write(SP);

src/main/java/com/cloudbees/syslog/integration/jul/util/LogManagerHelper.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public class LogManagerHelper {
4242
*/
4343
@Nullable
4444
public static Level getLevelProperty(@Nonnull LogManager manager, @Nonnull String name, @Nullable Level defaultValue) {
45-
45+
if (name == null) {
46+
return defaultValue;
47+
}
4648
String val = manager.getProperty(name);
4749
if (val == null) {
4850
return defaultValue;
@@ -61,6 +63,9 @@ public static Level getLevelProperty(@Nonnull LogManager manager, @Nonnull Strin
6163
*/
6264
@Nullable
6365
public static Filter getFilterProperty(@Nonnull LogManager manager, @Nullable String name, @Nullable Filter defaultValue) {
66+
if (name == null) {
67+
return defaultValue;
68+
}
6469
String val = manager.getProperty(name);
6570
try {
6671
if (val != null) {
@@ -85,6 +90,9 @@ public static Filter getFilterProperty(@Nonnull LogManager manager, @Nullable St
8590
* If the property is not defined or has problems we return the defaultValue.
8691
*/
8792
public static Formatter getFormatterProperty(@Nonnull LogManager manager, @Nullable String name, @Nullable Formatter defaultValue) {
93+
if (name == null) {
94+
return defaultValue;
95+
}
8896
String val = manager.getProperty(name);
8997
try {
9098
if (val != null) {
@@ -107,6 +115,9 @@ public static Formatter getFormatterProperty(@Nonnull LogManager manager, @Nulla
107115
* If the property is not defined we return the given default value.
108116
*/
109117
public static String getStringProperty(@Nonnull LogManager manager, @Nullable String name, String defaultValue) {
118+
if (name == null) {
119+
return defaultValue;
120+
}
110121
String val = manager.getProperty(name);
111122
if (val == null) {
112123
return defaultValue;
@@ -123,6 +134,9 @@ public static String getStringProperty(@Nonnull LogManager manager, @Nullable St
123134
* If the property is not defined or cannot be parsed we return the given default value.
124135
*/
125136
public static int getIntProperty(@Nonnull LogManager manager, @Nullable String name, int defaultValue) {
137+
if (name == null) {
138+
return defaultValue;
139+
}
126140
String val = manager.getProperty(name);
127141
if (val == null) {
128142
return defaultValue;
@@ -143,6 +157,9 @@ public static int getIntProperty(@Nonnull LogManager manager, @Nullable String n
143157
* If the property is not defined or cannot be parsed we return the given default value.
144158
*/
145159
public static boolean getBooleanProperty(@Nonnull LogManager manager, @Nullable String name, boolean defaultValue) {
160+
if (name == null) {
161+
return defaultValue;
162+
}
146163
String val = manager.getProperty(name);
147164
if (val == null) {
148165
return defaultValue;

src/main/java/com/cloudbees/syslog/sender/TcpSyslogMessageSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void setMaxRetryCount(int maxRetryCount) {
246246
this.maxRetryCount = maxRetryCount;
247247
}
248248

249-
public void setPostfix(String postfix) {
249+
public synchronized void setPostfix(String postfix) {
250250
this.postfix = postfix;
251251
}
252252

src/main/java/com/cloudbees/syslog/util/ConcurrentDateFormat.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.cloudbees.syslog.util;
1717

18+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19+
1820
import javax.annotation.Nonnull;
1921
import java.text.SimpleDateFormat;
2022
import java.util.Date;
@@ -60,6 +62,7 @@ public ConcurrentDateFormat(String pattern, Locale locale, TimeZone timeZone, in
6062
* @return the formatted time string.
6163
*/
6264
@Nonnull
65+
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
6366
public String format(@Nonnull Date date) {
6467
SimpleDateFormat dateFormat = dateFormats.poll();
6568
if (dateFormat == null) {

src/main/java/com/cloudbees/syslog/util/InternalLogger.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.cloudbees.syslog.integration.jul.util.LevelHelper;
1919

20+
import javax.annotation.Nonnull;
2021
import javax.annotation.Nullable;
2122
import java.text.DateFormat;
2223
import java.text.SimpleDateFormat;
@@ -41,12 +42,12 @@ public class InternalLogger {
4142
}
4243
}
4344

44-
public static InternalLogger getLogger(@Nullable String name) {
45+
public static InternalLogger getLogger(@Nonnull String name) {
4546
return new InternalLogger(name);
4647
}
4748

48-
public static InternalLogger getLogger(@Nullable Class clazz) {
49-
return getLogger(clazz == null ? null : clazz.getName());
49+
public static InternalLogger getLogger(@Nonnull Class clazz) {
50+
return getLogger(clazz.getName());
5051
}
5152

5253
public static Level getLevel() {

0 commit comments

Comments
 (0)