New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequireThisCheck doesn't see outer classes for anonymous classes #3041

Closed
artembilan opened this Issue Mar 17, 2016 · 4 comments

Comments

Projects
None yet
4 participants
@artembilan

artembilan commented Mar 17, 2016

When we use properties from the outer class in the anonymous class the report ends up only with this..
For example:

public class Foo {

    private final String foo = "foo";

    public static void main(String[] args) {
        new Foo().run();
    }

    private void run() {
        Runnable task = new Runnable() {

            @Override
            public void run() {
                System.out.println(foo);
            }

        };
        task.run();
    }

}

The report looks like:

<file name="Foo.java">
<error line="32" column="52" severity="error" message="Reference to instance variable &apos;foo&apos; needs &quot;this.&quot;." source="com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck"/>
</file>

Although, when we move that Runnable to the normal inner class:

private void run() {
    Runnable task = new MyRunnable();
    task.run();
}

private class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println(foo);
    }

}

everything looks good:

<file name="Foo.java">
<error line="36" column="44" severity="error" message="Reference to instance variable &apos;foo&apos; needs &quot;Foo.this.&quot;." source="com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck"/>
</file>

Thanks and sorry if it duplicates something existing already.

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Mar 20, 2016

Member

@artembilan , next time you try to open issue on chekcstyle please follow our guidelines on how to report it - http://checkstyle.sourceforge.net/report_issue.html#How_to_report_a_bug , all details are matter.

latest behaviour is also not right too:

/var/tmp $ java -jar checkstyle-6.17-SNAPSHOT-all.jar -c Test.xml TestClass.java
Starting audit...
[ERROR] /var/tmp/TestClass.java:14:36: Reference to instance variable 'foo' needs "this.". [RequireThis]
Audit done.
Checkstyle ends with 1 errors.

/var/tmp $ cat Test.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name = "Checker">
    <module name="TreeWalker">
        <module name="RequireThis">
            <property name="validateOnlyOverlapping" value="false"/>
        </module>

    </module>
</module>


/var/tmp $ cat TestClass.java 
public class TestClass {

    private final String foo = "foo";

    public static void main(String[] args) {
        new Foo().run();
    }

    private void run() {
        Runnable task = new Runnable() {

            @Override
            public void run() {
                System.out.println(foo);
            }

        };
        task.run();
    }

}
Member

romani commented Mar 20, 2016

@artembilan , next time you try to open issue on chekcstyle please follow our guidelines on how to report it - http://checkstyle.sourceforge.net/report_issue.html#How_to_report_a_bug , all details are matter.

latest behaviour is also not right too:

/var/tmp $ java -jar checkstyle-6.17-SNAPSHOT-all.jar -c Test.xml TestClass.java
Starting audit...
[ERROR] /var/tmp/TestClass.java:14:36: Reference to instance variable 'foo' needs "this.". [RequireThis]
Audit done.
Checkstyle ends with 1 errors.

/var/tmp $ cat Test.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name = "Checker">
    <module name="TreeWalker">
        <module name="RequireThis">
            <property name="validateOnlyOverlapping" value="false"/>
        </module>

    </module>
</module>


/var/tmp $ cat TestClass.java 
public class TestClass {

    private final String foo = "foo";

    public static void main(String[] args) {
        new Foo().run();
    }

    private void run() {
        Runnable task = new Runnable() {

            @Override
            public void run() {
                System.out.println(foo);
            }

        };
        task.run();
    }

}
@MEZk

This comment has been minimized.

Show comment
Hide comment
@MEZk

MEZk Mar 20, 2016

Contributor

@romani
By default validateOnlyOverlapping is set to true. So there should not be violations in your example.
foo is not overlapped by variable or method argument.

Contributor

MEZk commented Mar 20, 2016

@romani
By default validateOnlyOverlapping is set to true. So there should not be violations in your example.
foo is not overlapped by variable or method argument.

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Mar 20, 2016

Member

@MEZk , thanks for a hint , I updated by comment.

Member

romani commented Mar 20, 2016

@MEZk , thanks for a hint , I updated by comment.

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 25, 2017

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 25, 2017

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 25, 2017

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 25, 2017

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 25, 2017

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 28, 2017

romani added a commit that referenced this issue Jan 28, 2017

@romani romani added the bug label Jan 28, 2017

@romani romani added this to the 7.5 milestone Jan 28, 2017

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Jan 28, 2017

Member

Fix is merged

Member

romani commented Jan 28, 2017

Fix is merged

@romani romani closed this Jan 28, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment