Skip to content

[java] Unused Code: Java 8 receiver parameter with an internal class #719

@maggu2810

Description

@maggu2810

Rule Set:

rule set: Unused Code
rule: UnusedFormalParameter + UnusedPrivateMethod

Description:

Java 8 introduced the receiver parameter.
The relevant part of the Java Language Specification is here.

Either way, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated. The receiver parameter is not a formal parameter; more precisely, it is not a declaration of any kind of variable (§4.12.3), it is never bound to any value passed as an argument in a method invocation expression or qualified class instance creation expression, and it has no effect whatsoever at run time.

The receiver parameter itself is (at least it seems so) recognized correctly by PMD.

Also a non-static member class that is using a private method that is not called by another code is correctly identified and so no "unused private method" are complained.

But if we combine the Java 8 receiver parameter and a call of an inner class to an outer class method, there seems to be something wrong.

WRT the example below:
No error is detected if:

  • method test will be changed from private to protected
  • the receiver parameter is removed from the method

Code Sample demonstrating the issue:

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

public class ReceiverParameter {

    @Target(ElementType.TYPE_USE)
    public @interface AnnotatedUsage {
    }

    private class Intern {
        public void foo() {
            test(true, false);
        }
    }

    private final Intern intern;

    public ReceiverParameter() {
        this.intern = new Intern();
    }

    public void foo() {
        intern.foo();
    }

    private boolean test(@AnnotatedUsage ReceiverParameter this,final boolean foo, final boolean bar) {
        return foo == bar;
    }
}

Running PMD through:

CLI

The error has been previously detected by a project using Maven build system. I created a small example and use the command line, so it could be tested easily.

Command line:

pmd-bin-5.8.1/bin/run.sh pmd -d ReceiverParameter.java -f xml -l java -R rulesets/java/unusedcode.xml

Output:

<?xml version="1.0" encoding="UTF-8"?>
<pmd version="5.8.1" timestamp="2017-11-07T15:28:09.630">
<file name="/home/rathgeb/tmp/pmd/ReceiverParameter.java">
<violation beginline="26" endline="26" begincolumn="60" endcolumn="63" rule="UnusedFormalParameter" ruleset="Unused Code" class="ReceiverParameter" method="test" variable="this" externalInfoUrl="https://pmd.github.io/pmd-5.8.1/pmd-java/rules/java/unusedcode.html#UnusedFormalParameter" priority="3">
Avoid unused method parameters such as 'this'.
</violation>
<violation beginline="26" endline="26" begincolumn="21" endcolumn="101" rule="UnusedPrivateMethod" ruleset="Unused Code" class="ReceiverParameter" method="test" externalInfoUrl="https://pmd.github.io/pmd-5.8.1/pmd-java/rules/java/unusedcode.html#UnusedPrivateMethod" priority="3">
Avoid unused private methods such as 'test(ReceiverParameter,boolean,boolean)'.
</violation>
</file>
</pmd>

Metadata

Metadata

Assignees

Labels

a:bugPMD crashes or fails to analyse a file.in:symbol-tableAffects the symbol table code

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions