Skip to content

Commit 95a11b1

Browse files
committed
Address pmd findings.
1 parent 15efff4 commit 95a11b1

File tree

6 files changed

+43
-47
lines changed
  • .github
  • powertools-e2e-tests/handlers
    • logging-log4j/src/main/java/software/amazon/lambda/powertools/e2e
    • logging-logback/src/main/java/software/amazon/lambda/powertools/e2e
    • metrics/src/main/java/software/amazon/lambda/powertools/e2e
    • tracing/src/main/java/software/amazon/lambda/powertools/e2e
  • powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/internal

6 files changed

+43
-47
lines changed

.github/pmd-ruleset.xml

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?xml version="1.0"?>
22
<ruleset name="dogfood7"
3-
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
3+
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
66
<description>Rules to check Powertools for Lambda</description>
77
<!--
8-
Originally copied from: https://github.com/pmd/build-tools/blob/main/src/main/resources/net/sourceforge/pmd/pmd-dogfood-config.xml
8+
Originally copied from:
9+
https://github.com/pmd/build-tools/blob/main/src/main/resources/net/sourceforge/pmd/pmd-dogfood-config.xml
910
-->
10-
11+
1112
<!--
1213
Note: Eventually, this ruleset should include all rules and exclude those,
1314
which we know are explicitly decided as not applicable to PMD itself.
@@ -211,9 +212,10 @@
211212
<!--
212213
<rule ref="category/java/design.xml/LoosePackageCoupling">
213214
<properties>
214-
<property name="packages"><value>net.sourceforge.pmd.lang,net.sourceforge.pmd.lang.java,net.sourceforge.pmd.lang.jsp,net.sourceforge.pmd.lang.ecmascript,net.sourceforge.pmd.lang.cpp</value></property>
215+
<property
216+
name="packages"><value>net.sourceforge.pmd.lang,net.sourceforge.pmd.lang.java,net.sourceforge.pmd.lang.jsp,net.sourceforge.pmd.lang.ecmascript,net.sourceforge.pmd.lang.cpp</value></property>
215217
<property name="classes">
216-
<value>net.sourceforge.pmd.lang.Language,net.sourceforge.pmd.lang.LanguageVersion,net.sourceforge.pmd.lang.LanguageVersionDiscoverer,net.sourceforge.pmd.lang.LanguageVersionHandler,net.sourceforge.pmd.lang.Parser,net.sourceforge.pmd.lang.ast.Node</value>
218+
<value>net.sourceforge.pmd.lang.Language,net.sourceforge.pmd.lang.LanguageVersion,net.sourceforge.pmd.lang.LanguageVersionDiscoverer,net.sourceforge.pmd.lang.LanguageVersionHandler,net.sourceforge.pmd.lang.Parser,net.sourceforge.pmd.lang.ast.Node</value>
217219
</property>
218220
</properties>
219221
</rule>
@@ -263,7 +265,7 @@
263265
<rule ref="category/java/errorprone.xml/AssignmentInOperand">
264266
<priority>1</priority>
265267
<properties>
266-
<property name="allowWhile" value="true"/>
268+
<property name="allowWhile" value="true" />
267269
</properties>
268270
</rule>
269271
<rule ref="category/java/errorprone.xml/AssignmentToNonFinalStatic">
@@ -327,7 +329,8 @@
327329
<rule ref="category/java/errorprone.xml/CompareObjectsWithEquals">
328330
<priority>1</priority>
329331
<properties>
330-
<property name="typesThatCompareByReference" value="java.lang.Enum,java.lang.Class,net.sourceforge.pmd.lang.ast.Node,net.sourceforge.pmd.lang.ast.GenericToken"/>
332+
<property name="typesThatCompareByReference"
333+
value="java.lang.Enum,java.lang.Class,net.sourceforge.pmd.lang.ast.Node,net.sourceforge.pmd.lang.ast.GenericToken" />
331334
</properties>
332335
</rule>
333336
<rule ref="category/java/errorprone.xml/ComparisonWithNaN">
@@ -350,6 +353,9 @@
350353
</rule>
351354
<rule ref="category/java/errorprone.xml/EmptyCatchBlock">
352355
<priority>1</priority>
356+
<properties>
357+
<property name="allowCommentedBlocks" value="false" />
358+
</properties>
353359
</rule>
354360
<!-- <rule ref="category/java/errorprone.xml/EmptyFinalizer" /> -->
355361
<!-- <rule ref="category/java/errorprone.xml/EmptyFinallyBlock" /> -->
@@ -516,10 +522,10 @@
516522

517523
<!-- PMD specific custom rules -->
518524
<rule name="UseInstanceofToCompareClasses"
519-
language="java"
520-
since="5.0"
521-
message="replace o.getClass().equals(MyClass.class) with o instanceof MyClass"
522-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
525+
language="java"
526+
since="5.0"
527+
message="replace o.getClass().equals(MyClass.class) with o instanceof MyClass"
528+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
523529
<description>replace o.getClass().equals(MyClass.class) with o instanceof MyClass. Make sure MyClass doesn't have descendants</description>
524530
<priority>1</priority>
525531
<properties>
@@ -536,10 +542,10 @@
536542
</rule>
537543

538544
<rule name="ReversedUseInstanceofToCompareClasses"
539-
language="java"
540-
since="5.0"
541-
message="replace MyClass.class.equals(o.getClass()) with o instanceof MyClass"
542-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
545+
language="java"
546+
since="5.0"
547+
message="replace MyClass.class.equals(o.getClass()) with o instanceof MyClass"
548+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
543549
<description>replace MyClass.class.equals(o.getClass()) with o instanceof MyClass. Make sure MyClass doesn't have descendants</description>
544550
<priority>3</priority>
545551
<properties>
@@ -556,10 +562,10 @@
556562
</rule>
557563

558564
<rule name="DontCallSuperVisitWhenUsingRuleChain"
559-
language="java"
560-
message="Don't call super.visit() when using the rulechain"
561-
typeResolution="true"
562-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
565+
language="java"
566+
message="Don't call super.visit() when using the rulechain"
567+
typeResolution="true"
568+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
563569
<description>Calling super.visit breaks the rulechain, by starting a full visitor run from the passed node downwards. Add all needed nodes to the rulechain instead.</description>
564570
<priority>1</priority>
565571
<properties>
@@ -583,10 +589,10 @@
583589
</rule>
584590

585591
<rule name="AlwaysCallSuperWhenNotUsingRuleChain"
586-
language="java"
587-
message="Always call super.visit() when not using rulechain"
588-
typeResolution="true"
589-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
592+
language="java"
593+
message="Always call super.visit() when not using rulechain"
594+
typeResolution="true"
595+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
590596
<description>Just returning without calling super stops visiting of nested nodes like inner classes.</description>
591597
<priority>3</priority>
592598
<properties>
@@ -606,10 +612,10 @@
606612

607613
<!-- Idea from https://github.com/pmd/pmd/pull/3609#discussion_r748292071 -->
608614
<rule name="ReuseInvocationMatcher"
609-
language="java"
610-
message="Reuse InvocationMatcher"
611-
typeResolution="true"
612-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
615+
language="java"
616+
message="Reuse InvocationMatcher"
617+
typeResolution="true"
618+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
613619
<description>Share the invocation matcher and not create a new one every time</description>
614620
<priority>1</priority>
615621
<properties>
@@ -625,10 +631,10 @@
625631
</rule>
626632

627633
<rule name="DoNotUseJavaUtilLogging"
628-
language="java"
629-
since="7.0.0"
630-
message="Use slf4j: LoggerFactory.getLogger(MyClass.class)"
631-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
634+
language="java"
635+
since="7.0.0"
636+
message="Use slf4j: LoggerFactory.getLogger(MyClass.class)"
637+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
632638
<description>Use slf4j: LoggerFactory.getLogger(MyClass.class)</description>
633639
<priority>1</priority>
634640
<properties>
@@ -641,4 +647,4 @@
641647
</property>
642648
</properties>
643649
</rule>
644-
</ruleset>
650+
</ruleset>

powertools-e2e-tests/handlers/logging-log4j/src/main/java/software/amazon/lambda/powertools/e2e/Input.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public class Input {
2020
private String message;
2121
private Map<String, String> keys;
2222

23-
public Input() {
24-
}
25-
2623
public String getMessage() {
2724
return message;
2825
}

powertools-e2e-tests/handlers/logging-logback/src/main/java/software/amazon/lambda/powertools/e2e/Input.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public class Input {
2020
private String message;
2121
private Map<String, String> keys;
2222

23-
public Input() {
24-
}
25-
2623
public String getMessage() {
2724
return message;
2825
}

powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Input.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ public class Input {
2323

2424
private String highResolution;
2525

26-
public Input() {
27-
}
28-
2926
public Map<String, Double> getMetrics() {
3027
return metrics;
3128
}

powertools-e2e-tests/handlers/tracing/src/main/java/software/amazon/lambda/powertools/e2e/Input.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
public class Input {
1818
private String message;
1919

20-
public Input() {
21-
}
22-
2320
public String getMessage() {
2421
return message;
2522
}

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspect.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.io.OutputStream;
4444
import java.io.OutputStreamWriter;
4545
import java.util.Arrays;
46+
import java.util.Locale;
4647
import java.util.Random;
4748

4849
import org.aspectj.lang.ProceedingJoinPoint;
@@ -99,7 +100,7 @@ static void setLogLevel() {
99100

100101
private static Level getLevelFromString(String level) {
101102
if (Arrays.stream(Level.values()).anyMatch(slf4jLevel -> slf4jLevel.name().equalsIgnoreCase(level))) {
102-
return Level.valueOf(level.toUpperCase());
103+
return Level.valueOf(level.toUpperCase(Locale.ROOT));
103104
} else {
104105
// FATAL does not exist in slf4j
105106
if ("FATAL".equalsIgnoreCase(level)) {
@@ -117,6 +118,7 @@ private static void setLogLevels(Level logLevel) {
117118
@SuppressWarnings({ "EmptyMethod" })
118119
@Pointcut("@annotation(logging)")
119120
public void callAt(Logging logging) {
121+
// Pointcut method - body intentionally empty
120122
}
121123

122124
/**

0 commit comments

Comments
 (0)