diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index fe3d2aa4c7..0b3f50ccdc 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -27,6 +27,7 @@ Being based on a proper Antlr grammar, CPD can: * doc * [#1896](https://github.com/pmd/pmd/issues/1896): \[doc] Error in changelog 6.16.0 due to not properly closed rule tag + * [#1898](https://github.com/pmd/pmd/issues/1898): \[doc] Incorrect code example for DoubleBraceInitialization in documentation on website * [#1906](https://github.com/pmd/pmd/issues/1906): \[doc] Broken link for adding own CPD languages * [#1909](https://github.com/pmd/pmd/issues/1909): \[doc] Sample usage example refers to deprecated ruleset "basic.xml" instead of "quickstart.xml" * xml diff --git a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java index acf13282e6..8b6f68f16a 100644 --- a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java +++ b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java @@ -422,7 +422,7 @@ public class RuleDocGenerator { lines.add(""); for (String example : rule.getExamples()) { lines.add("``` " + mapLanguageForHighlighting(languageTersename)); - lines.addAll(toLines(StringUtils.stripToEmpty(example))); + lines.addAll(toLines("{%raw%}" + StringUtils.stripToEmpty(example) + "{%endraw%}")); lines.add("```"); lines.add(""); } diff --git a/pmd-doc/src/test/resources/expected/sample.md b/pmd-doc/src/test/resources/expected/sample.md index 866bb6815c..79b91c61a8 100644 --- a/pmd-doc/src/test/resources/expected/sample.md +++ b/pmd-doc/src/test/resources/expected/sample.md @@ -50,7 +50,7 @@ Avoid jumbled loop incrementers - its usually a mistake, and is confusing even i **Example(s):** ``` java -public class JumbledIncrementerRule1 { +{%raw%}public class JumbledIncrementerRule1 { public void foo() { for (int i = 0; i < 10; i++) { // only references 'i' for (int k = 0; k < 20; i++) { // references both 'i' and 'k' @@ -58,7 +58,7 @@ public class JumbledIncrementerRule1 { } } } -} +}{%endraw%} ``` **This rule has the following properties:** @@ -123,7 +123,7 @@ Avoid jumbled loop incrementers - its usually a mistake, and is confusing even i **Example(s):** ``` java -public class JumbledIncrementerRule1 { +{%raw%}public class JumbledIncrementerRule1 { public void foo() { for (int i = 0; i < 10; i++) { // only references 'i' for (int k = 0; k < 20; i++) { // references both 'i' and 'k' @@ -131,7 +131,7 @@ public class JumbledIncrementerRule1 { } } } -} +}{%endraw%} ``` **Use this rule by referencing it:** @@ -162,7 +162,7 @@ Third paragraph. **Example(s):** ``` java -public class Bar { // poor, missing a hashcode() method +{%raw%}public class Bar { // poor, missing a hashcode() method public boolean equals(Object o) { // do some comparison } @@ -182,6 +182,13 @@ public class Foo { // perfect, both methods provided // return some hash value } } + +// A sample with double braces (#1898) +public class Foo { + public List bar() { + return new ArrayList(){{ addAll("a","b","c"); }}; + } +}{%endraw%} ``` **Use this rule by referencing it:** @@ -214,7 +221,7 @@ Avoid jumbled loop incrementers - its usually a mistake, and is confusing even i **Example(s):** ``` java -public class JumbledIncrementerRule1 { +{%raw%}public class JumbledIncrementerRule1 { public void foo() { for (int i = 0; i < 10; i++) { // only references 'i' for (int k = 0; k < 20; i++) { // references both 'i' and 'k' @@ -222,7 +229,7 @@ public class JumbledIncrementerRule1 { } } } -} +}{%endraw%} ``` **This rule has the following properties:** @@ -312,14 +319,14 @@ if (0 > 1 && 0 < 1) { **Example(s):** ``` java -public class Bar { +{%raw%}public class Bar { public boolean foo() { if (0 < 1) { // less-than should not be escaped in markdown String s = "abc"; // the quotes should not be escaped in markdown. } } // -} +}{%endraw%} ``` **This rule has the following properties:** diff --git a/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml b/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml index aa49c9a6f2..44e2bb76fb 100644 --- a/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml +++ b/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml @@ -123,6 +123,13 @@ public class Foo { // perfect, both methods provided // return some hash value } } + +// A sample with double braces (#1898) +public class Foo { + public List bar() { + return new ArrayList(){{ addAll("a","b","c"); }}; + } +} ]]> diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 1cbc1fe9ab..3ab442e456 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -426,7 +426,7 @@ public class Foo { (){%raw%}{{ addAll("a","b","c"); }};{%endraw%} +return new ArrayList(){{ addAll("a","b","c"); }}; // the better way is to not create an anonymous class: List a = new ArrayList<>();