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

Fix Error Prone warnings in log4j-api #1961

Merged
merged 10 commits into from
Nov 15, 2023
11 changes: 4 additions & 7 deletions log4j-1.2-api/src/main/java/org/apache/log4j/jmx/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.apache.logging.log4j.util.LoaderUtil;

/**
* Manages an instance of com.sun.jdmk.comm.HtmlAdapterServer which was provided for demonstration purposes in the Java
Expand Down Expand Up @@ -53,13 +54,9 @@ public class Agent {
private static Object createServer() {
Object newInstance = null;
try {
newInstance = Class.forName("com.sun.jdmk.comm.HtmlAdapterServer").newInstance();
} catch (final ClassNotFoundException ex) {
throw new RuntimeException(ex.toString());
} catch (final InstantiationException ex) {
throw new RuntimeException(ex.toString());
} catch (final IllegalAccessException ex) {
throw new RuntimeException(ex.toString());
newInstance = LoaderUtil.newInstanceOf("com.sun.jdmk.comm.HtmlAdapterServer");
} catch (final ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
return newInstance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.util;

import java.nio.charset.Charset;
import java.util.Base64;


Expand All @@ -31,6 +32,6 @@ private Base64Util() {
}

public static String encode(final String str) {
return str != null ? encoder.encodeToString(str.getBytes()) : null;
return str != null ? encoder.encodeToString(str.getBytes(Charset.defaultCharset())) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static Object[][] data() {

@ParameterizedTest
@MethodSource("data")
public void testTokenize(final CharSequence value, final List<CharSequence> expectedTokens) {
public void testTokenize(final String value, final List<CharSequence> expectedTokens) {
final List<CharSequence> tokens = PropertySource.Util.tokenize(value);
assertEquals(expectedTokens, tokens);
}
Expand Down
3 changes: 3 additions & 0 deletions log4j-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</dependencies>
<build>
<plugins>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand All @@ -75,6 +76,7 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down Expand Up @@ -104,6 +106,7 @@
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
6 changes: 5 additions & 1 deletion log4j-api/src/main/java/org/apache/logging/log4j/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import aQute.bnd.annotation.baseline.BaselineIgnore;
import org.apache.logging.log4j.spi.StandardLevel;
import org.apache.logging.log4j.util.Strings;

Expand Down Expand Up @@ -75,6 +76,7 @@
* used in logging configurations.
* </p>
*/
@BaselineIgnore("2.22.0")
public final class Level implements Comparable<Level>, Serializable {

private static final Level[] EMPTY_ARRAY = {};
Expand Down Expand Up @@ -122,6 +124,8 @@ public final class Level implements Comparable<Level>, Serializable {
public static final Level ALL = new Level("ALL", StandardLevel.ALL.intLevel());

/**
* Category to be used by custom levels.
*
* @since 2.1
*/
public static final String CATEGORY = "Level";
Expand Down Expand Up @@ -365,7 +369,7 @@ public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final Strin
}

// for deserialization
protected Object readResolve() {
private Object readResolve() {
return Level.valueOf(this.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ default void log(String message, Object... params) {
* @param message The message.
* @param params Parameters to the message.
*/
@SuppressWarnings("deprecation")
default void log(String message, Supplier<?>... params) {
}

Expand All @@ -109,6 +110,7 @@ default void log(Message message) {
* Causes all the data collected to be logged along with the message. Interface default method does nothing.
* @param messageSupplier The supplier of the message to log.
*/
@SuppressWarnings("deprecation")
default void log(Supplier<Message> messageSupplier) {
}

Expand All @@ -119,6 +121,7 @@ default void log(Supplier<Message> messageSupplier) {
* @return the message logger or {@code null} if no logging occurred.
* @since 2.20
*/
@SuppressWarnings("deprecation")
default Message logAndGet(final Supplier<Message> messageSupplier) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class LogManager {

private static volatile LoggerContextFactory factory;

/**
/*
* Scans the classpath to find all logging implementation. Currently, only one will be used but this could be
* extended to allow multiple implementations to be used.
*/
Expand All @@ -92,7 +92,7 @@ public class LogManager {
final Class<? extends LoggerContextFactory> factoryClass = provider.loadLoggerContextFactory();
if (factoryClass != null) {
try {
factories.put(provider.getPriority(), factoryClass.newInstance());
factories.put(provider.getPriority(), LoaderUtil.newInstanceOf(factoryClass));
} catch (final Exception e) {
LOGGER.error("Unable to create class {} specified in provider URL {}",
factoryClass.getName(), provider.getUrl(), e);
Expand Down
Loading