Skip to content

Commit

Permalink
minor code cleanups
Browse files Browse the repository at this point in the history
- more private final fields
- avoiding C-style array declarations
- few typos, notably JPlarformLogger -> JPlatformLogger
  • Loading branch information
dfa1 committed Aug 29, 2021
1 parent d0836be commit b9da2f7
Show file tree
Hide file tree
Showing 38 changed files with 73 additions and 70 deletions.
Expand Up @@ -43,7 +43,7 @@ public class SLF4JLocationAwareLog implements Log, Serializable {

// in both Log4jLogger and Jdk14Logger classes in the original JCL, the
// logger instance is transient
private transient LocationAwareLogger logger;
private final transient LocationAwareLogger logger;

private static final String FQCN = SLF4JLocationAwareLog.class.getName();

Expand Down
Expand Up @@ -42,7 +42,7 @@ public class SLF4JLog implements Log, Serializable {

// in both Log4jLogger and Jdk14Logger classes in the original JCL, the
// logger instance is transient
private transient Logger logger;
private final transient Logger logger;

public SLF4JLog(Logger logger) {
this.logger = logger;
Expand Down
Expand Up @@ -110,7 +110,7 @@ public String[] getAttributeNames() {
while (keys.hasMoreElements()) {
names.add((String) keys.nextElement());
}
String results[] = new String[names.size()];
String[] results = new String[names.size()];
for (int i = 0; i < results.length; i++) {
results[i] = (String) names.get(i);
}
Expand Down
Expand Up @@ -174,8 +174,8 @@ public static boolean isInstalled() {
public static void removeHandlersForRootLogger() {
java.util.logging.Logger rootLogger = getRootLogger();
java.util.logging.Handler[] handlers = rootLogger.getHandlers();
for (int i = 0; i < handlers.length; i++) {
rootLogger.removeHandler(handlers[i]);
for (Handler handler : handlers) {
rootLogger.removeHandler(handler);
}
}

Expand Down
4 changes: 2 additions & 2 deletions log4j-over-slf4j/src/main/java/org/apache/log4j/Category.java
Expand Up @@ -45,12 +45,12 @@ public class Category {

private static final String CATEGORY_FQCN = Category.class.getName();

private String name;
private final String name;

protected org.slf4j.Logger slf4jLogger;
private org.slf4j.spi.LocationAwareLogger locationAwareLogger;

private static Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL");
private static final Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL");

Category(String name) {
this.name = name;
Expand Down
Expand Up @@ -33,7 +33,7 @@
class Log4jLoggerFactory {

// String, Logger
private static ConcurrentMap<String, Logger> log4jLoggers = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Logger> log4jLoggers = new ConcurrentHashMap<>();

public static Logger getLogger(String name) {
org.apache.log4j.Logger instance = log4jLoggers.get(name);
Expand Down
2 changes: 1 addition & 1 deletion slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Expand Up @@ -182,7 +182,7 @@ private static void reportIgnoredStaticLoggerBinders(Set<URL> staticLoggerBinder

// We need to use the name of the StaticLoggerBinder class, but we can't
// reference the class itself.
private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";
private static final String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";

static Set<URL> findPossibleStaticLoggerBinderPathSet() {
// use Set instead of list in order to deal with bug #138
Expand Down
Expand Up @@ -43,7 +43,7 @@
*/
public class BasicMDCAdapter implements MDCAdapter {

private InheritableThreadLocal<Map<String, String>> inheritableThreadLocal = new InheritableThreadLocal<Map<String, String>>() {
private final InheritableThreadLocal<Map<String, String>> inheritableThreadLocal = new InheritableThreadLocal<Map<String, String>>() {
@Override
protected Map<String, String> childValue(Map<String, String> parentValue) {
if (parentValue == null) {
Expand Down
8 changes: 4 additions & 4 deletions slf4j-api/src/main/java/org/slf4j/helpers/BasicMarker.java
Expand Up @@ -40,7 +40,7 @@ public class BasicMarker implements Marker {

private static final long serialVersionUID = -2849567615646933777L;
private final String name;
private List<Marker> referenceList = new CopyOnWriteArrayList<>();
private final List<Marker> referenceList = new CopyOnWriteArrayList<>();

BasicMarker(String name) {
if (name == null) {
Expand Down Expand Up @@ -128,9 +128,9 @@ public boolean contains(String name) {
return false;
}

private static String OPEN = "[ ";
private static String CLOSE = " ]";
private static String SEP = ", ";
private static final String OPEN = "[ ";
private static final String CLOSE = " ]";
private static final String SEP = ", ";

public boolean equals(Object obj) {
if (this == obj)
Expand Down
Expand Up @@ -33,9 +33,9 @@ public class FormattingTuple {

static public FormattingTuple NULL = new FormattingTuple(null);

private String message;
private Throwable throwable;
private Object[] argArray;
private final String message;
private final Throwable throwable;
private final Object[] argArray;

public FormattingTuple(String message) {
this(message, null, null);
Expand Down
Expand Up @@ -14,9 +14,9 @@ public class NOP_FallbackServiceProvider implements SLF4JServiceProvider {
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "2.0.99"; // !final

private ILoggerFactory loggerFactory = new NOPLoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new NOPMDCAdapter();
private final ILoggerFactory loggerFactory = new NOPLoggerFactory();
private final IMarkerFactory markerFactory = new BasicMarkerFactory();
private final MDCAdapter mdcAdapter = new NOPMDCAdapter();


@Override
Expand Down
Expand Up @@ -53,7 +53,7 @@ public class SubstituteLogger implements Logger {
private Boolean delegateEventAware;
private Method logMethodCache;
private EventRecodingLogger eventRecodingLogger;
private Queue<SubstituteLoggingEvent> eventQueue;
private final Queue<SubstituteLoggingEvent> eventQueue;

public final boolean createdPostInitialization;

Expand Down
Expand Up @@ -6,9 +6,9 @@
import org.slf4j.spi.SLF4JServiceProvider;

public class SubstituteServiceProvider implements SLF4JServiceProvider {
private SubstituteLoggerFactory loggerFactory = new SubstituteLoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new BasicMDCAdapter();
private final SubstituteLoggerFactory loggerFactory = new SubstituteLoggerFactory();
private final IMarkerFactory markerFactory = new BasicMarkerFactory();
private final MDCAdapter mdcAdapter = new BasicMDCAdapter();

@Override
public ILoggerFactory getLoggerFactory() {
Expand Down
@@ -1,10 +1,12 @@
package org.slf4j.spi;

import org.slf4j.event.LoggingEvent;

/**
* Additional interface to {@link LoggingEventBuilder} and
* {@link qorg.slf4j.event.LoggingEvent LoggingEvent}.
* {@link org.slf4j.event.LoggingEvent LoggingEvent}.
*
* Implementations of {@link LoggingEventBuilder} and {@link LoggingEvent} may optionally
* Implementations of {@link LoggingEventBuilder} and {@link LoggingEvent} may optionally
* implement {@link CallerBoundaryAware} in order to support caller info extraction.
*
* This interface is intended for use by logging backends or logging bridges.
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/**
* This interface based on {@link java.util.ServiceLoader} paradigm.
Expand Down Expand Up @@ -31,7 +32,7 @@ public interface SLF4JServiceProvider {
public IMarkerFactory getMarkerFactory();

/**
* Return the instnace of {@link MDCAdapter} that
* Return the instance of {@link MDCAdapter} that
* {@link MDC} should bind to.
*
* @return instance of {@link MDCAdapter}
Expand Down
Expand Up @@ -29,7 +29,7 @@
import java.util.concurrent.atomic.AtomicLong;

public class LoggerAccessingThread extends Thread {
private static int LOOP_LEN = 32;
private static final int LOOP_LEN = 32;

final CyclicBarrier barrier;
final int count;
Expand Down
Expand Up @@ -36,7 +36,7 @@
import java.util.Set;

public class SubstituteLoggerFactoryTest {
private SubstituteLoggerFactory factory = new SubstituteLoggerFactory();
private final SubstituteLoggerFactory factory = new SubstituteLoggerFactory();

@Test
public void testFactory() {
Expand Down
Expand Up @@ -135,8 +135,8 @@ public Builder level(String level) {
}
}

private String level;
private String levelEnabled;
private final String level;
private final String levelEnabled;

private LogTransformer(Builder builder) {
String s = "WARNING: javassist not available on classpath for javaagent, log statements will not be added";
Expand All @@ -156,10 +156,10 @@ private LogTransformer(Builder builder) {
this.levelEnabled = "is" + builder.level.substring(0, 1).toUpperCase() + builder.level.substring(1) + "Enabled";
}

private boolean addEntryExit;
private final boolean addEntryExit;
// private boolean addVariableAssignment;
private boolean verbose;
private String[] ignore;
private final boolean verbose;
private final String[] ignore;

public byte[] transform(ClassLoader loader, String className, Class<?> clazz, ProtectionDomain domain, byte[] bytes) {

Expand Down
Expand Up @@ -50,7 +50,7 @@ public int[] sortAndPruneComposites() {
int[] sortedArray = sort();
// start a new stopwatch called PRUNE_COMPOSITES
sortProfiler.start("PRUNE_COMPOSITES");
int result[] = pruneComposites(sortedArray);
int[] result = pruneComposites(sortedArray);

return result;
}
Expand Down
Expand Up @@ -31,28 +31,28 @@
import org.slf4j.LoggerFactory;

/**
* Manages instances of {@link SLF4JPlarformLogger}.
* Manages instances of {@link SLF4JPlatformLogger}.
*
* @since 1.3.0
* @author Ceki
*
*/
public class SLF4JPlarformLoggerFactory {
ConcurrentMap<String, SLF4JPlarformLogger> loggerMap = new ConcurrentHashMap<>();
ConcurrentMap<String, SLF4JPlatformLogger> loggerMap = new ConcurrentHashMap<>();

/**
* Return an appropriate {@link SLF4JPlarformLogger} instance by name.
* Return an appropriate {@link SLF4JPlatformLogger} instance by name.
*/
public SLF4JPlarformLogger getLogger(String loggerName) {
public SLF4JPlatformLogger getLogger(String loggerName) {


SLF4JPlarformLogger spla = loggerMap.get(loggerName);
SLF4JPlatformLogger spla = loggerMap.get(loggerName);
if (spla != null) {
return spla;
} else {
Logger slf4jLogger = LoggerFactory.getLogger(loggerName);
SLF4JPlarformLogger newInstance = new SLF4JPlarformLogger(slf4jLogger);
SLF4JPlarformLogger oldInstance = loggerMap.putIfAbsent(loggerName, newInstance);
SLF4JPlatformLogger newInstance = new SLF4JPlatformLogger(slf4jLogger);
SLF4JPlatformLogger oldInstance = loggerMap.putIfAbsent(loggerName, newInstance);
return oldInstance == null ? newInstance : oldInstance;
}
}
Expand Down
Expand Up @@ -37,13 +37,13 @@
* Adapts {@link Logger} to {@link System.Logger}.
* @since 2.0.0
*/
class SLF4JPlarformLogger implements System.Logger {
class SLF4JPlatformLogger implements System.Logger {

static private String PRESUMED_CALLER_BOUNDARY = System.Logger.class.getName();
static private final String PRESUMED_CALLER_BOUNDARY = System.Logger.class.getName();

private final Logger slf4jLogger;

public SLF4JPlarformLogger(Logger logger) {
public SLF4JPlatformLogger(Logger logger) {
this.slf4jLogger = requireNonNull(logger);
}

Expand Down
Expand Up @@ -52,7 +52,7 @@ public System.Logger getLogger(String name, Module module) {
// is updated to forward a module, we should do that here.
//
// [1] https://openjdk.java.net/jeps/264
SLF4JPlarformLogger adapter = platformLoggerFactory.getLogger(name);
SLF4JPlatformLogger adapter = platformLoggerFactory.getLogger(name);
return adapter;
}

Expand Down
Expand Up @@ -216,7 +216,7 @@ final private void fillCallerData(String callerFQCN, LogRecord record) {
static String SUBSTITUE = SubstituteLogger.class.getName();
static String FLUENT = DefaultLoggingEventBuilder.class.getName();

static String BARRIER_CLASSES[] = new String[] { SUPER_OF_SUPER, SUPER, SELF, SUBSTITUE, FLUENT };
static String[] BARRIER_CLASSES = new String[] { SUPER_OF_SUPER, SUPER, SELF, SUBSTITUE, FLUENT };

private boolean barrierMatch(String callerFQCN, String candidateClassName) {
if (candidateClassName.equals(callerFQCN))
Expand Down
Expand Up @@ -47,7 +47,7 @@ public class LoggerSerializationTest {
static class LoggerHolder implements Serializable {
private static final long serialVersionUID = 1L;

private Logger log = LoggerFactory.getLogger(LoggerHolder.class);
private final Logger log = LoggerFactory.getLogger(LoggerHolder.class);

public String toString() {
return "log=" + getLog();
Expand Down
Expand Up @@ -11,7 +11,7 @@

public class MDCFriendTest {

private static Random random = new Random();
private static final Random random = new Random();
int diff = random.nextInt(1024 * 8);

@Test
Expand Down
Expand Up @@ -58,8 +58,8 @@ public class MigratorFrame extends JFrame implements ActionListener {
static final int X_SIZE = 700;
static final int Y_SIZE = 400;

private SpringLayout layoutManager = new SpringLayout();
private SpringLayoutHelper slh = new SpringLayoutHelper(layoutManager, BASIC_PADDING);
private final SpringLayout layoutManager = new SpringLayout();
private final SpringLayoutHelper slh = new SpringLayoutHelper(layoutManager, BASIC_PADDING);

private JLabel migrationLabel;

Expand Down
Expand Up @@ -36,7 +36,7 @@
*/
public class JCLRuleSet implements RuleSet {

private ArrayList<ConversionRule> conversionRuleList;
private final ArrayList<ConversionRule> conversionRuleList;

public JCLRuleSet() {
// matching : import org.apache.commons.logging.LogFactory;
Expand Down
Expand Up @@ -36,7 +36,7 @@
*/
public class JULRuleSet implements RuleSet {

private ArrayList<ConversionRule> conversionRuleList;
private final ArrayList<ConversionRule> conversionRuleList;

public JULRuleSet() {

Expand Down
Expand Up @@ -30,7 +30,7 @@

public class Log4jRuleSet implements RuleSet {

private ArrayList<ConversionRule> conversionRuleList;
private final ArrayList<ConversionRule> conversionRuleList;

public Log4jRuleSet() {

Expand Down
Expand Up @@ -40,8 +40,8 @@ public class MultiGroupConversionRule implements ConversionRule {
// our conversion reg-expressions
final private static int MAX_GROUPS = 10;

private Pattern pattern;
private String[] replacementTable = new String[MAX_GROUPS];
private final Pattern pattern;
private final String[] replacementTable = new String[MAX_GROUPS];

public MultiGroupConversionRule(Pattern pattern) {
this.pattern = pattern;
Expand Down
Expand Up @@ -28,7 +28,7 @@

public class RandomHelper {

private Random random = new Random(100);
private final Random random = new Random(100);
final char folderSeparator;

RandomHelper(char folderSeparator) {
Expand Down
Expand Up @@ -35,7 +35,7 @@

class TrivialMatcher implements RuleSet {

private ArrayList<ConversionRule> conversionRuleList;
private final ArrayList<ConversionRule> conversionRuleList;

public TrivialMatcher() {
// simple rule no capturing group is defined, we use default capturing group which is group zero
Expand Down

0 comments on commit b9da2f7

Please sign in to comment.