1
0
forked from phoedos/pmd

[java] Use slf4j

This commit is contained in:
Andreas Dangel
2022-02-18 17:08:41 +01:00
parent 744817a389
commit 138296be1e
8 changed files with 54 additions and 58 deletions
pmd-java
pom.xml
src
main/java/net/sourceforge/pmd/lang/java
ast/internal
internal
rule
symbols/internal/asm
test/java/net/sourceforge/pmd/lang/java

@ -248,11 +248,6 @@
</dependency>
<!-- the following dependencies are there to support typeresolution in test cases -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>

@ -12,8 +12,9 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
@ -21,7 +22,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
* Helper class to analyze {@link ASTImportDeclaration}s.
*/
public final class ImportWrapper {
private static final Logger LOG = Logger.getLogger(ImportWrapper.class.getName());
private static final Logger LOG = LoggerFactory.getLogger(ImportWrapper.class);
private final ASTImportDeclaration node;
private final String name;
@ -78,7 +79,7 @@ public final class ImportWrapper {
return names;
} catch (LinkageError e) {
// This is an incomplete classpath, report the missing class
LOG.log(Level.FINE, "Possible incomplete auxclasspath: Error while processing imports", e);
LOG.debug("Possible incomplete auxclasspath: Error while processing imports", e);
return Collections.emptySet();
}
}

@ -5,10 +5,13 @@
package net.sourceforge.pmd.lang.java.internal;
import java.util.IdentityHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import net.sourceforge.pmd.benchmark.TimeTracker;
import net.sourceforge.pmd.benchmark.TimedOperation;
@ -40,7 +43,7 @@ import net.sourceforge.pmd.lang.java.types.internal.infer.TypeInferenceLogger.Ve
*/
public final class JavaAstProcessor {
private static final Logger DEFAULT_LOG = Logger.getLogger(JavaAstProcessor.class.getName());
private static final Logger DEFAULT_LOG = LoggerFactory.getLogger(JavaAstProcessor.class);
private static final Map<ClassLoader, TypeSystem> TYPE_SYSTEMS = new IdentityHashMap<>();
private static final Level INFERENCE_LOG_LEVEL;
@ -49,9 +52,32 @@ public final class JavaAstProcessor {
static {
Level level;
try {
level = Level.parse(System.getenv("PMD_DEBUG_LEVEL"));
switch (System.getenv("PMD_DEBUG_LEVEL").toLowerCase(Locale.ROOT)) {
case "error":
level = Level.ERROR;
break;
case "warn":
case "warning":
level = Level.WARN;
break;
case "info":
level = Level.INFO;
break;
case "debug":
case "fine":
case "finer":
level = Level.DEBUG;
break;
case "trace":
case "finest":
level = Level.TRACE;
break;
default:
level = null;
break;
}
} catch (IllegalArgumentException | NullPointerException ignored) {
level = Level.OFF;
level = null;
}
INFERENCE_LOG_LEVEL = level;
}
@ -86,11 +112,10 @@ public final class JavaAstProcessor {
return unresolvedTypes;
}
@SuppressWarnings("PMD.LiteralsFirstInComparisons") // see #3315
static TypeInferenceLogger defaultTypeInfLogger() {
if (Level.FINEST.equals(INFERENCE_LOG_LEVEL)) {
if (INFERENCE_LOG_LEVEL == Level.TRACE) {
return new VerboseLogger(System.err);
} else if (Level.FINE.equals(INFERENCE_LOG_LEVEL)) {
} else if (INFERENCE_LOG_LEVEL == Level.DEBUG) {
return new SimpleLogger(System.err);
} else {
return TypeInferenceLogger.noop();

@ -11,7 +11,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -227,7 +226,7 @@ public class GuardLogStatementRule extends AbstractJavaRulechainRule {
private @Nullable String getJutilLogLevelInFirstArg(ASTMethodCall methodCall) {
ASTExpression firstArg = methodCall.getArguments().toStream().get(0);
if (TypeTestUtil.isA(Level.class, firstArg) && firstArg instanceof ASTNamedReferenceExpr) {
if (TypeTestUtil.isA("java.util.logging.Level", firstArg) && firstArg instanceof ASTNamedReferenceExpr) {
return ((ASTNamedReferenceExpr) firstArg).getName().toLowerCase(Locale.ROOT);
}
return null;

@ -11,7 +11,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration;
@ -35,7 +37,7 @@ import net.sourceforge.pmd.properties.PropertyFactory;
* @author Brian Remedios
*/
public class CommentRequiredRule extends AbstractJavaRulechainRule {
private static final Logger LOG = Logger.getLogger(CommentRequiredRule.class.getName());
private static final Logger LOG = LoggerFactory.getLogger(CommentRequiredRule.class);
// Used to pretty print a message
private static final Map<String, String> DESCRIPTOR_NAME_TO_COMMENT_TYPE = new HashMap<>();
@ -101,7 +103,7 @@ public class CommentRequiredRule extends AbstractJavaRulechainRule {
boolean classCommentRequirementValueOverridden = classCommentRequirementValue != CommentRequirement.Required;
if (headerCommentRequirementValueOverridden && !classCommentRequirementValueOverridden) {
LOG.warning("Rule CommentRequired uses deprecated property 'headerCommentRequirement'. "
LOG.warn("Rule CommentRequired uses deprecated property 'headerCommentRequirement'. "
+ "Future versions of PMD will remove support for this property. "
+ "Please use 'classCommentRequirement' instead!");
propertyValues.put(CLASS_CMT_REQUIREMENT_DESCRIPTOR, headerCommentRequirementValue);

@ -4,7 +4,8 @@
package net.sourceforge.pmd.lang.java.symbols.internal.asm;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A simple double-checked initializer, that parses something (a class,
@ -13,7 +14,7 @@ import java.util.logging.Logger;
@SuppressWarnings({"PMD.AvoidUsingVolatile", "PMD.AvoidCatchingThrowable"})
abstract class ParseLock {
private static final Logger LOG = Logger.getLogger(ParseLock.class.getName());
private static final Logger LOG = LoggerFactory.getLogger(ParseLock.class);
private volatile ParseStatus status = ParseStatus.NOT_PARSED;
@ -36,8 +37,7 @@ abstract class ParseLock {
} catch (Throwable t) {
status = ParseStatus.FAILED;
this.status = status;
LOG.severe(t.toString());
t.printStackTrace();
LOG.error(t.toString(), t);
finishParse(true);
}
assert status.isFinished : "Inconsistent status " + status;

@ -9,12 +9,13 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.internal.Slf4jSimpleConfiguration;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.ast.Node;
@ -99,10 +100,10 @@ public class JavaParsingHelper extends BaseParsingHelper<JavaParsingHelper, ASTC
}
public TestCheckLogger(boolean doLogOnConsole) {
Logger consoleLogger = Logger.getAnonymousLogger();
if (!doLogOnConsole) {
consoleLogger.setLevel(Level.OFF);
Slf4jSimpleConfiguration.disableLogging(TestCheckLogger.class);
}
Logger consoleLogger = LoggerFactory.getLogger(TestCheckLogger.class);
baseLogger = SemanticErrorReporter.reportToLogger(consoleLogger);
}

@ -4,11 +4,6 @@
package net.sourceforge.pmd.lang.java;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@ -22,33 +17,11 @@ public class QuickstartRulesetTest {
@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();
@After
public void cleanup() {
Handler[] handlers = Logger.getLogger(RuleSetLoader.class.getName()).getHandlers();
for (Handler handler : handlers) {
Logger.getLogger(RuleSetLoader.class.getName()).removeHandler(handler);
}
}
@Test
public void noDeprecations() {
Logger.getLogger(RuleSetLoader.class.getName()).addHandler(new Handler() {
@Override
public void publish(LogRecord record) {
Assert.fail("No Logging expected: " + record.getMessage());
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
});
RuleSetLoader ruleSetLoader = new RuleSetLoader().enableCompatibility(false);
RuleSet quickstart = ruleSetLoader.loadFromResource("rulesets/java/quickstart.xml");
Assert.assertFalse(quickstart.getRules().isEmpty());
Assert.assertTrue(systemErrRule.getLog().isEmpty());
}
}