From 138296be1e0f9cf2df87af5a88254b8ae83bf435 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 18 Feb 2022 17:08:41 +0100 Subject: [PATCH] [java] Use slf4j --- pmd-java/pom.xml | 5 --- .../lang/java/ast/internal/ImportWrapper.java | 9 ++-- .../lang/java/internal/JavaAstProcessor.java | 41 +++++++++++++++---- .../bestpractices/GuardLogStatementRule.java | 3 +- .../documentation/CommentRequiredRule.java | 8 ++-- .../java/symbols/internal/asm/ParseLock.java | 8 ++-- .../pmd/lang/java/JavaParsingHelper.java | 9 ++-- .../pmd/lang/java/QuickstartRulesetTest.java | 29 +------------ 8 files changed, 54 insertions(+), 58 deletions(-) diff --git a/pmd-java/pom.xml b/pmd-java/pom.xml index 44275b453b..1b25ad4b65 100644 --- a/pmd-java/pom.xml +++ b/pmd-java/pom.xml @@ -248,11 +248,6 @@ - - org.slf4j - slf4j-api - test - org.apache.logging.log4j log4j-api diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ImportWrapper.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ImportWrapper.java index a16fbae33e..a3b15bd4fe 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ImportWrapper.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/ImportWrapper.java @@ -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(); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaAstProcessor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaAstProcessor.java index b9c7d32353..f0b87004cf 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaAstProcessor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaAstProcessor.java @@ -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 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(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/GuardLogStatementRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/GuardLogStatementRule.java index ac31f9017d..c102432ec3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/GuardLogStatementRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/GuardLogStatementRule.java @@ -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; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java index dfb7f2d8eb..318c97c7c2 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java @@ -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 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); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/ParseLock.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/ParseLock.java index adf2cd8a92..7869406868 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/ParseLock.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/ParseLock.java @@ -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; diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/JavaParsingHelper.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/JavaParsingHelper.java index 2ce3b7e540..cdc29ad493 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/JavaParsingHelper.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/JavaParsingHelper.java @@ -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