Skip to content

Commit 1b544d3

Browse files
authored
Fix SEI MET07-J violations (apache#3602)
* Fix `SEI MET07-J` violations This change: - Deprecates public/protected hiding methods. - Removes package-private hiding methods. - For legacy modules that will disappear in 3.x, just suppresses the warning. Closes apache#3601 * Try skipping required checks skip-checks: true
1 parent d91d6e2 commit 1b544d3

File tree

15 files changed

+61
-22
lines changed

15 files changed

+61
-22
lines changed

log4j-1.2-api/src/main/java/org/apache/log4j/Hierarchy.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.log4j;
1818

19+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1920
import java.util.Enumeration;
2021
import java.util.Hashtable;
2122
import java.util.Vector;
@@ -79,10 +80,16 @@ protected Logger newLogger(final String name, final org.apache.logging.log4j.spi
7980
private static class PrivateLogManager extends org.apache.logging.log4j.LogManager {
8081
private static final String FQCN = Hierarchy.class.getName();
8182

83+
@SuppressFBWarnings(
84+
value = "HSM_HIDING_METHOD",
85+
justification = "The class is private, no confusion can arise.")
8286
public static LoggerContext getContext() {
8387
return getContext(FQCN, false);
8488
}
8589

90+
@SuppressFBWarnings(
91+
value = "HSM_HIDING_METHOD",
92+
justification = "The class is private, no confusion can arise.")
8693
public static org.apache.logging.log4j.Logger getLogger(final String name) {
8794
return getLogger(FQCN, name);
8895
}

log4j-1.2-api/src/main/java/org/apache/log4j/bridge/LogEventAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
public class LogEventAdapter extends LoggingEvent {
3939

40-
private static final long JVM_START_TIME = initStartTime();
40+
public static final long JVM_START_TIME = initStartTime();
4141

4242
private final LogEvent event;
4343

@@ -50,7 +50,7 @@ public LogEventAdapter(final LogEvent event) {
5050
* elapsed since 01.01.1970.
5151
* @return the time when the JVM started.
5252
*/
53-
public static long getStartTime() {
53+
public static long getJvmStartTime() {
5454
return JVM_START_TIME;
5555
}
5656

log4j-1.2-api/src/main/java/org/apache/log4j/helpers/UtilLoggingLevel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.apache.logging.log4j.util.Strings.toRootUpperCase;
2020

21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2122
import java.util.ArrayList;
2223
import java.util.List;
2324
import org.apache.log4j.Level;
@@ -157,6 +158,7 @@ public static UtilLoggingLevel toLevel(final int val, final UtilLoggingLevel def
157158
* @param val numeric value.
158159
* @return matching level or UtilLoggerLevel.FINEST if no match.
159160
*/
161+
@SuppressFBWarnings(value = "HSM_HIDING_METHOD", justification = "Legacy code")
160162
public static Level toLevel(final int val) {
161163
return toLevel(val, FINEST);
162164
}
@@ -184,6 +186,7 @@ public static List getAllPossibleLevels() {
184186
* @param s symbolic name.
185187
* @return matching level or Level.DEBUG if no match.
186188
*/
189+
@SuppressFBWarnings(value = "HSM_HIDING_METHOD", justification = "Legacy code")
187190
public static Level toLevel(final String s) {
188191
return toLevel(s, Level.DEBUG);
189192
}
@@ -195,6 +198,7 @@ public static Level toLevel(final String s) {
195198
* @param defaultLevel level to return if no match.
196199
* @return matching level or defaultLevel if no match.
197200
*/
201+
@SuppressFBWarnings(value = "HSM_HIDING_METHOD", justification = "Legacy code")
198202
public static Level toLevel(final String sArg, final Level defaultLevel) {
199203
if (sArg == null) {
200204
return defaultLevel;

log4j-1.2-api/src/main/java/org/apache/log4j/helpers/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Log4j 1.x compatibility layer.
1919
*/
2020
@Export
21-
@Version("2.20.2")
21+
@Version("2.20.3")
2222
package org.apache.log4j.helpers;
2323

2424
import org.osgi.annotation.bundle.Export;

log4j-1.2-api/src/main/java/org/apache/log4j/spi/LoggingEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class LoggingEvent {
3535
* @return the JVM start time.
3636
*/
3737
public static long getStartTime() {
38-
return LogEventAdapter.getStartTime();
38+
return LogEventAdapter.getJvmStartTime();
3939
}
4040

4141
/**

log4j-appserver/src/main/java/org/apache/logging/log4j/appserver/jetty/Log4j2Logger.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.appserver.jetty;
1818

19+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1920
import org.apache.logging.log4j.Level;
2021
import org.apache.logging.log4j.LogManager;
2122
import org.apache.logging.log4j.spi.ExtendedLogger;
@@ -51,6 +52,7 @@ public class Log4j2Logger extends AbstractLogger {
5152
*/
5253
private static class PrivateManager extends LogManager {
5354

55+
@SuppressFBWarnings("HSM_HIDING_METHOD")
5456
public static LoggerContext getContext() {
5557
final ClassLoader cl = AbstractLogger.class.getClassLoader();
5658
return getContext(PARENT_FQCN, cl, false);

log4j-appserver/src/main/java/org/apache/logging/log4j/appserver/tomcat/TomcatLogger.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import aQute.bnd.annotation.Resolution;
2020
import aQute.bnd.annotation.spi.ServiceProvider;
21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2122
import java.net.URI;
2223
import java.net.URISyntaxException;
2324
import java.net.URL;
@@ -163,6 +164,7 @@ public void fatal(final Object o, final Throwable throwable) {
163164
*/
164165
private static class PrivateManager extends LogManager {
165166

167+
@SuppressFBWarnings("HSM_HIDING_METHOD")
166168
public static LoggerContext getContext() {
167169
final ClassLoader cl = TomcatLogger.class.getClassLoader();
168170
URI uri = null;

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfig.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1920
import java.util.Arrays;
2021
import java.util.List;
2122
import java.util.concurrent.TimeUnit;
@@ -90,7 +91,7 @@ public LoggerConfig build() {
9091
isAdditivity(),
9192
getProperties(),
9293
getConfig(),
93-
includeLocation(getIncludeLocation()));
94+
shouldIncludeLocation(getIncludeLocation()));
9495
}
9596
}
9697

@@ -273,7 +274,14 @@ public static LoggerConfig createLogger(
273274
final boolean additive = Booleans.parseBoolean(additivity, true);
274275

275276
return new AsyncLoggerConfig(
276-
name, appenderRefs, filter, level, additive, properties, config, includeLocation(includeLocation));
277+
name,
278+
appenderRefs,
279+
filter,
280+
level,
281+
additive,
282+
properties,
283+
config,
284+
shouldIncludeLocation(includeLocation));
277285
}
278286

279287
/**
@@ -291,6 +299,7 @@ public static LoggerConfig createLogger(
291299
* @since 3.0
292300
*/
293301
@Deprecated
302+
@SuppressFBWarnings("HSM_HIDING_METHOD")
294303
public static LoggerConfig createLogger(
295304
@PluginAttribute(value = "additivity", defaultBoolean = true) final boolean additivity,
296305
@PluginAttribute("level") final Level level,
@@ -310,11 +319,20 @@ public static LoggerConfig createLogger(
310319
additivity,
311320
properties,
312321
config,
313-
includeLocation(includeLocation));
322+
shouldIncludeLocation(includeLocation));
314323
}
315324

316-
// Note: for asynchronous loggers, includeLocation default is FALSE
325+
/**
326+
* @deprecated since 2.25.0. The method will become private in version 3.0.
327+
*/
328+
@Deprecated
329+
@SuppressFBWarnings(value = "HSM_HIDING_METHOD", justification = "Should be private.")
317330
protected static boolean includeLocation(final String includeLocationConfigValue) {
331+
return shouldIncludeLocation(includeLocationConfigValue);
332+
}
333+
334+
// Note: for asynchronous loggers, includeLocation default is FALSE
335+
private static boolean shouldIncludeLocation(final String includeLocationConfigValue) {
318336
return Boolean.parseBoolean(includeLocationConfigValue);
319337
}
320338

@@ -343,7 +361,7 @@ public LoggerConfig build() {
343361
isAdditivity(),
344362
getProperties(),
345363
getConfig(),
346-
AsyncLoggerConfig.includeLocation(getIncludeLocation()));
364+
shouldIncludeLocation(getIncludeLocation()));
347365
}
348366
}
349367

@@ -376,7 +394,7 @@ public static LoggerConfig createLogger(
376394
additive,
377395
properties,
378396
config,
379-
AsyncLoggerConfig.includeLocation(includeLocation));
397+
shouldIncludeLocation(includeLocation));
380398
}
381399

382400
/**
@@ -402,7 +420,7 @@ public static LoggerConfig createLogger(
402420
additive,
403421
properties,
404422
config,
405-
AsyncLoggerConfig.includeLocation(includeLocation));
423+
shouldIncludeLocation(includeLocation));
406424
}
407425
}
408426
}

log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MainMapLookup.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.core.lookup;
1818

19+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1920
import java.util.Map;
2021
import org.apache.logging.log4j.core.LogEvent;
2122
import org.apache.logging.log4j.core.config.plugins.Plugin;
@@ -74,6 +75,9 @@ public MainMapLookup(final Map<String, String> map) {
7475
* @param args
7576
* An application's {@code public static main(String[])} arguments.
7677
*/
78+
@SuppressFBWarnings(
79+
value = "HSM_HIDING_METHOD",
80+
justification = "The MapLookup.setMainArguments() method hidden by this one is deprecated.")
7781
public static void setMainArguments(final String... args) {
7882
if (args == null) {
7983
return;

log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* {@link org.apache.logging.log4j.core.lookup.StrLookup#CATEGORY Lookup}.
2222
*/
2323
@Export
24-
@Version("2.24.0")
24+
@Version("2.24.1")
2525
package org.apache.logging.log4j.core.lookup;
2626

2727
import org.osgi.annotation.bundle.Export;

log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionResolver.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,6 @@ Throwable extractThrowable(final LogEvent logEvent) {
387387
return logEvent.getThrown();
388388
}
389389

390-
static String getName() {
391-
return "exception";
392-
}
393-
394390
@Override
395391
public boolean isResolvable() {
396392
return stackTraceEnabled;

log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionResolverFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static ExceptionResolverFactory getInstance() {
3636

3737
@Override
3838
public String getName() {
39-
return ExceptionResolver.getName();
39+
return "exception";
4040
}
4141

4242
@Override

log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionRootCauseResolver.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ public final class ExceptionRootCauseResolver extends ExceptionResolver {
3636
super(context, config);
3737
}
3838

39-
static String getName() {
40-
return "exceptionRootCause";
41-
}
42-
4339
@Override
4440
Throwable extractThrowable(final LogEvent logEvent) {
4541
final Throwable thrown = logEvent.getThrown();

log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionRootCauseResolverFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static ExceptionRootCauseResolverFactory getInstance() {
3636

3737
@Override
3838
public String getName() {
39-
return ExceptionRootCauseResolver.getName();
39+
return "exceptionRootCause";
4040
}
4141

4242
@Override
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="3601" link="https://github.com/apache/logging-log4j2/issues/3601"/>
7+
<description format="asciidoc">
8+
Deprecate or remove static hiding methods.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)