---
title: Sample
summary: Sample ruleset to test rule doc generation.  Here might be &lt;script&gt;alert('XSS');&lt;/script&gt; as well. And &quot;quotes&quot;.  Inside CDATA &lt;script&gt;alert('XSS');&lt;/script&gt;.
permalink: pmd_rules_java_sample.html
folder: pmd/rules/java
sidebaractiveurl: /pmd_rules_java.html
editmepath: ../rulesets/ruledoctest/sample.xml
keywords: Sample, XSSInDocumentation, OverrideBothEqualsAndHashcode, JumbledIncrementer, DeprecatedSample, RenamedRule1, RenamedRule2, RenamedRule3, RenamedRule4, MovedRule
language: Java
---
<!-- DO NOT EDIT THIS FILE. This file is generated from file ../rulesets/ruledoctest/sample.xml. -->
## DeprecatedSample

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

**Since:** PMD 1.0

**Priority:** Medium (3)

Just some description of a deprecated rule.

RuleTag with quotes: Use {% rule "RenamedRule" %} instead.

RuleTag without quotes: Use {% rule RenamedRule %} instead.

RuleTag with full category and quotes: Use {% rule "java/sample/RenamedRule" %} instead.

RuleTag with full category and without quotes: Use {% rule java/sample/RenamedRule %} instead.

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
```

**Use this rule by referencing it:**
``` xml
<rule ref="category/java/sample.xml/DeprecatedSample" />
```

## JumbledIncrementer

**Since:** PMD 1.0

**Priority:** Medium (3)

Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
 [
  ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
  =
  ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
 ]
```

**Example(s):**

``` java
{%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'
                System.out.println("Hello");
            }
        }
    }
}{%endraw%}
```

**This rule has the following properties:**

|Name|Default Value|Description|
|----|-------------|-----------|
|sampleAdditionalProperty|the value|This is a additional property for tests|
|sampleMultiStringProperty|Value1 , Value2|Test property with multiple strings|
|sampleDeprecatedProperty|test|<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f; font-size: 75%;">Deprecated</span>  This is a sample deprecated property for tests|
|sampleRegexProperty1|\\/\\\*\\s+(default\|package)\\s+\\\*\\/|The property is of type regex|
|sampleRegexProperty2|\[a-z\]\*|The property is of type regex|
|sampleRegexProperty3|\\s+|The property is of type regex|
|sampleRegexProperty4|\_dd\_|The property is of type regex|
|sampleRegexProperty5|\[0-9\]{1,3}|The property is of type regex|
|sampleRegexProperty6|\\b|The property is of type regex|
|sampleRegexProperty7|\\n|The property is of type regex|

**Use this rule with the default properties by just referencing it:**
``` xml
<rule ref="category/java/sample.xml/JumbledIncrementer" />
```

**Use this rule and customize it:**
``` xml
<rule ref="category/java/sample.xml/JumbledIncrementer">
    <properties>
        <property name="sampleAdditionalProperty" value="the value" />
        <property name="sampleMultiStringProperty" value="Value1,Value2" />
        <property name="sampleRegexProperty1" value="\/\*\s+(default|package)\s+\*\/" />
        <property name="sampleRegexProperty2" value="[a-z]*" />
        <property name="sampleRegexProperty3" value="\s+" />
        <property name="sampleRegexProperty4" value="_dd_" />
        <property name="sampleRegexProperty5" value="[0-9]{1,3}" />
        <property name="sampleRegexProperty6" value="\b" />
        <property name="sampleRegexProperty7" value="\n" />
    </properties>
</rule>
```

## MovedRule

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

The rule has been moved to another ruleset. Use instead: [JumbledIncrementer](pmd_rules_java_sample2.html#jumbledincrementer)

**Since:** PMD 1.0

**Priority:** Medium (3)

Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
  [
    ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
    =
    ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
  ]
```

**Example(s):**

``` java
{%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'
                System.out.println("Hello");
            }
        }
    }
}{%endraw%}
```

**Use this rule by referencing it:**
``` xml
<rule ref="category/java/sample.xml/MovedRule" />
```

## OverrideBothEqualsAndHashcode

**Since:** PMD 0.4

**Priority:** Medium (3)

**Minimum Language Version:** Java 1.5

**Maximum Language Version:** Java 11

Override both `public boolean Object.equals(Object other)`, and `public int Object.hashCode()`, or override neither.
Even if you are inheriting a `hashCode()` from a parent class, consider implementing hashCode and explicitly
delegating to your superclass.

Second paragraph.

    Code sample

Third paragraph.

**This rule is defined by the following Java class:** [net.sourceforge.pmd.lang.java.rule.errorprone.OverrideBothEqualsAndHashcodeRule](https://github.com/pmd/pmd/blob/master/net/sourceforge/pmd/lang/java/rule/errorprone/OverrideBothEqualsAndHashcodeRule.java)

**Example(s):**

``` java
{%raw%}public class Bar {      // poor, missing a hashcode() method
    public boolean equals(Object o) {
      // do some comparison
    }
}

public class Baz {      // poor, missing an equals() method
    public int hashCode() {
      // return some hash value
    }
}

public class Foo {      // perfect, both methods provided
    public boolean equals(Object other) {
      // do some comparison
    }
    public int hashCode() {
      // return some hash value
    }
}

// A sample with double braces (#1898)
public class Foo {
    public List<String> bar() {
        return new ArrayList<String>(){{ addAll("a","b","c"); }};
    }
}{%endraw%}
```

**Use this rule by referencing it:**
``` xml
<rule ref="category/java/sample.xml/OverrideBothEqualsAndHashcode" />
```

## RenamedRule1

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

This rule has been renamed. Use instead: [JumbledIncrementer](#jumbledincrementer)

**Since:** PMD 1.0

**Priority:** Medium (3)

Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
 [
  ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
  =
  ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
 ]
```

**Example(s):**

``` java
{%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'
                System.out.println("Hello");
            }
        }
    }
}{%endraw%}
```

**This rule has the following properties:**

|Name|Default Value|Description|
|----|-------------|-----------|
|sampleAdditionalProperty|the value|This is a additional property for tests|
|sampleMultiStringProperty|Value1 , Value2|Test property with multiple strings|
|sampleDeprecatedProperty|test|<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f; font-size: 75%;">Deprecated</span>  This is a sample deprecated property for tests|
|sampleRegexProperty1|\\/\\\*\\s+(default\|package)\\s+\\\*\\/|The property is of type regex|
|sampleRegexProperty2|\[a-z\]\*|The property is of type regex|
|sampleRegexProperty3|\\s+|The property is of type regex|
|sampleRegexProperty4|\_dd\_|The property is of type regex|
|sampleRegexProperty5|\[0-9\]{1,3}|The property is of type regex|
|sampleRegexProperty6|\\b|The property is of type regex|
|sampleRegexProperty7|\\n|The property is of type regex|

**Use this rule with the default properties by just referencing it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule1" />
```

**Use this rule and customize it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule1">
    <properties>
        <property name="sampleAdditionalProperty" value="the value" />
        <property name="sampleMultiStringProperty" value="Value1,Value2" />
        <property name="sampleRegexProperty1" value="\/\*\s+(default|package)\s+\*\/" />
        <property name="sampleRegexProperty2" value="[a-z]*" />
        <property name="sampleRegexProperty3" value="\s+" />
        <property name="sampleRegexProperty4" value="_dd_" />
        <property name="sampleRegexProperty5" value="[0-9]{1,3}" />
        <property name="sampleRegexProperty6" value="\b" />
        <property name="sampleRegexProperty7" value="\n" />
    </properties>
</rule>
```

## RenamedRule2

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

This rule has been renamed. Use instead: [JumbledIncrementer](#jumbledincrementer)

**Since:** PMD 1.0

**Priority:** Medium (3)

Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
 [
  ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
  =
  ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
 ]
```

**Example(s):**

``` java
{%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'
                System.out.println("Hello");
            }
        }
    }
}{%endraw%}
```

**This rule has the following properties:**

|Name|Default Value|Description|
|----|-------------|-----------|
|sampleAdditionalProperty|the value|This is a additional property for tests|
|sampleMultiStringProperty|Value1 , Value2|Test property with multiple strings|
|sampleDeprecatedProperty|test|<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f; font-size: 75%;">Deprecated</span>  This is a sample deprecated property for tests|
|sampleRegexProperty1|\\/\\\*\\s+(default\|package)\\s+\\\*\\/|The property is of type regex|
|sampleRegexProperty2|\[a-z\]\*|The property is of type regex|
|sampleRegexProperty3|\\s+|The property is of type regex|
|sampleRegexProperty4|\_dd\_|The property is of type regex|
|sampleRegexProperty5|\[0-9\]{1,3}|The property is of type regex|
|sampleRegexProperty6|\\b|The property is of type regex|
|sampleRegexProperty7|\\n|The property is of type regex|

**Use this rule with the default properties by just referencing it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule2" />
```

**Use this rule and customize it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule2">
    <properties>
        <property name="sampleAdditionalProperty" value="the value" />
        <property name="sampleMultiStringProperty" value="Value1,Value2" />
        <property name="sampleRegexProperty1" value="\/\*\s+(default|package)\s+\*\/" />
        <property name="sampleRegexProperty2" value="[a-z]*" />
        <property name="sampleRegexProperty3" value="\s+" />
        <property name="sampleRegexProperty4" value="_dd_" />
        <property name="sampleRegexProperty5" value="[0-9]{1,3}" />
        <property name="sampleRegexProperty6" value="\b" />
        <property name="sampleRegexProperty7" value="\n" />
    </properties>
</rule>
```

## RenamedRule3

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

This rule has been renamed. Use instead: [JumbledIncrementer](#jumbledincrementer)

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

**Since:** PMD 1.0

**Priority:** Medium (3)

Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
 [
  ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
  =
  ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
 ]
```

**Example(s):**

``` java
{%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'
                System.out.println("Hello");
            }
        }
    }
}{%endraw%}
```

**This rule has the following properties:**

|Name|Default Value|Description|
|----|-------------|-----------|
|sampleAdditionalProperty|the value|This is a additional property for tests|
|sampleMultiStringProperty|Value1 , Value2|Test property with multiple strings|
|sampleDeprecatedProperty|test|<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f; font-size: 75%;">Deprecated</span>  This is a sample deprecated property for tests|
|sampleRegexProperty1|\\/\\\*\\s+(default\|package)\\s+\\\*\\/|The property is of type regex|
|sampleRegexProperty2|\[a-z\]\*|The property is of type regex|
|sampleRegexProperty3|\\s+|The property is of type regex|
|sampleRegexProperty4|\_dd\_|The property is of type regex|
|sampleRegexProperty5|\[0-9\]{1,3}|The property is of type regex|
|sampleRegexProperty6|\\b|The property is of type regex|
|sampleRegexProperty7|\\n|The property is of type regex|

**Use this rule with the default properties by just referencing it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule3" />
```

**Use this rule and customize it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule3">
    <properties>
        <property name="sampleAdditionalProperty" value="the value" />
        <property name="sampleMultiStringProperty" value="Value1,Value2" />
        <property name="sampleRegexProperty1" value="\/\*\s+(default|package)\s+\*\/" />
        <property name="sampleRegexProperty2" value="[a-z]*" />
        <property name="sampleRegexProperty3" value="\s+" />
        <property name="sampleRegexProperty4" value="_dd_" />
        <property name="sampleRegexProperty5" value="[0-9]{1,3}" />
        <property name="sampleRegexProperty6" value="\b" />
        <property name="sampleRegexProperty7" value="\n" />
    </properties>
</rule>
```

## RenamedRule4

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

This rule has been renamed. Use instead: [JumbledIncrementer](#jumbledincrementer)

<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span>

**Since:** PMD 1.0

**Priority:** Medium (3)

Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
 [
  ForUpdate/StatementExpressionList/StatementExpression/PostfixExpression/PrimaryExpression/PrimaryPrefix/Name/@Image
  =
  ancestor::ForStatement/ForInit//VariableDeclaratorId/@Image
 ]
```

**Example(s):**

``` java
{%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'
                System.out.println("Hello");
            }
        }
    }
}{%endraw%}
```

**This rule has the following properties:**

|Name|Default Value|Description|
|----|-------------|-----------|
|sampleAdditionalProperty|the value|This is a additional property for tests|
|sampleMultiStringProperty|Value1 , Value2|Test property with multiple strings|
|sampleDeprecatedProperty|test|<span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f; font-size: 75%;">Deprecated</span>  This is a sample deprecated property for tests|
|sampleRegexProperty1|\\/\\\*\\s+(default\|package)\\s+\\\*\\/|The property is of type regex|
|sampleRegexProperty2|\[a-z\]\*|The property is of type regex|
|sampleRegexProperty3|\\s+|The property is of type regex|
|sampleRegexProperty4|\_dd\_|The property is of type regex|
|sampleRegexProperty5|\[0-9\]{1,3}|The property is of type regex|
|sampleRegexProperty6|\\b|The property is of type regex|
|sampleRegexProperty7|\\n|The property is of type regex|

**Use this rule with the default properties by just referencing it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule4" />
```

**Use this rule and customize it:**
``` xml
<rule ref="category/java/sample.xml/RenamedRule4">
    <properties>
        <property name="sampleAdditionalProperty" value="the value" />
        <property name="sampleMultiStringProperty" value="Value1,Value2" />
        <property name="sampleRegexProperty1" value="\/\*\s+(default|package)\s+\*\/" />
        <property name="sampleRegexProperty2" value="[a-z]*" />
        <property name="sampleRegexProperty3" value="\s+" />
        <property name="sampleRegexProperty4" value="_dd_" />
        <property name="sampleRegexProperty5" value="[0-9]{1,3}" />
        <property name="sampleRegexProperty6" value="\b" />
        <property name="sampleRegexProperty7" value="\n" />
    </properties>
</rule>
```

## XSSInDocumentation

**Since:** PMD 0.1

**Priority:** Medium (3)

&lt;script&gt;alert('XSS at the beginning');&lt;/script&gt; HTML tags might appear at various places.
Sometimes they should be escaped, sometimes not:

    For example, in a code sample, it should <strong>not</strong> be escaped.
    This means, <script>alert('XSS');</script> is no problem here.

Also in backticks `<script>alert('XSS');</script>` like here it is no problem.

The tags are already escaped in the text, e.g. &lt;script&gt;alert('XSS');&lt;/script&gt;.

A valid case in markdown is a <https://en.wikipedia.org/wiki/Hyperlink>


If using CDATA you don't need to escape, but this &lt;script&gt;alert('XSS');&lt;/script&gt;
should still not be executed.


> this is a
> block quote
> in markdown

The paragraph after the quotation.


> block quote
> in cdata


If the description contains a code example, then e.g. &quot;quotes&quot; should not be escaped:

```
if (0 > 1 && 0 < 1) {
    System.out.println("impossible");
}
```

**This rule is defined by the following XPath expression:**
``` xpath
//ForStatement
```

**Example(s):**

``` java
{%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.
      }
    }
    // <script>alert('XSS');</script>
}{%endraw%}
```

**This rule has the following properties:**

|Name|Default Value|Description|
|----|-------------|-----------|
|sampleRegexProperty|\\/\\\*\\s+(default\|package)\\s+\\\*\\/|The property is of type regex|
|XSSpropertyTest|&lt;script&gt;alert('XSS');&lt;/script&gt;|&lt;script&gt;alert('XSS');&lt;/script&gt;|
|escapingNeeded|this is escaped: \||You should be able to use \| in the description|

**Use this rule with the default properties by just referencing it:**
``` xml
<rule ref="category/java/sample.xml/XSSInDocumentation" />
```

**Use this rule and customize it:**
``` xml
<rule ref="category/java/sample.xml/XSSInDocumentation">
    <properties>
        <property name="sampleRegexProperty" value="\/\*\s+(default|package)\s+\*\/" />
        <property name="XSSpropertyTest" value="&lt;script&gt;alert('XSS');&lt;/script&gt;" />
        <property name="escapingNeeded" value="this is escaped: |" />
    </properties>
</rule>
```
