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

DesignForExtension: order of annotations change violation #3830

Closed
rnveach opened this Issue Feb 14, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@rnveach
Member

rnveach commented Feb 14, 2017

$ cat TestClass.java
import org.junit.Ignore;
import org.junit.Test;

public class TestClass {
    private static final List<String> NO_SQALE

    @Ignore
    @Test
    public void method2() {
test();
    }

    private void test() {}
}

$ cat TestConfig.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">
    <property name="charset" value="UTF-8"/>

    <module name="TreeWalker">
    <module name="DesignForExtension">
      <property name="ignoredAnnotations" value="Override, Test, Before, After, BeforeClass, AfterClass"/>
    </module>
    </module>
</module>

$ java -jar checkstyle-7.5.1-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[ERROR] TestClass.java:7:5: Class 'TestClass' looks like designed for extension (can be subclassed), but the method 'method2' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'TestClass' final or making the method 'method2' static/final/abstract/empty, or adding allowed annotation for the method. [DesignForExtension]
Audit done.
Checkstyle ends with 1 errors.

As you can see, the example I get a violation even though Test is added as an annotation.
If you swap the 2 annotations so Test is first, the violation goes away.
I am expecting either there to be no violation as the method is set to be ignored by the annotation.

@romani romani added the approved label Feb 15, 2017

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Feb 15, 2017

Member

@MEZk , please take a look , you were recently in this Check.

Member

romani commented Feb 15, 2017

@MEZk , please take a look , you were recently in this Check.

@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Feb 16, 2017

Member

The bug is somewhere in DesignForExtensionCheck.hasAnnotation or in TokenUtils.findFirstTokenByPredicate called by hasAnnotation.

final Optional<DetailAST> annotation = TokenUtils.findFirstTokenByPredicate(modifiers,
currentToken -> currentToken != null
&& currentToken.getType() == TokenTypes.ANNOTATION
&& annotationName.equals(getAnnotationName(currentToken)));

For the test code above, findFirstTokenByPredicate gives the following tokens when debug information is added:

Examine: ANNOTATION[7x4] - 159 - Ignore
Examine: @[7x4] - 170
Examine: @[8x4] - 170
Examine: void[9x11] - 49
Examine: EXPR[10x4] - 28
Examine: ([10x4] - 27
Examine: test[10x0] - 58
Examine: MODIFIERS[13x4] - 5
Examine: private[13x4] - 61
Examine: void[13x12] - 49
Examine: }[13x25] - 73

As you can see only the first annotation is given correctly. Then it starts giving the @ nodes which results in it skipping over the ANNOTATION nodes at 8x4.
It looks like when it reaches the last sibling and goes to the parent, it skips over the parent's sibling and jumps straight to the first child.
So I believe the issue to be with findFirstTokenByPredicate.
Further, I think lines 196 and 197 (in the code reference in the line above) are swapped and that is where the problem is.

Member

rnveach commented Feb 16, 2017

The bug is somewhere in DesignForExtensionCheck.hasAnnotation or in TokenUtils.findFirstTokenByPredicate called by hasAnnotation.

final Optional<DetailAST> annotation = TokenUtils.findFirstTokenByPredicate(modifiers,
currentToken -> currentToken != null
&& currentToken.getType() == TokenTypes.ANNOTATION
&& annotationName.equals(getAnnotationName(currentToken)));

For the test code above, findFirstTokenByPredicate gives the following tokens when debug information is added:

Examine: ANNOTATION[7x4] - 159 - Ignore
Examine: @[7x4] - 170
Examine: @[8x4] - 170
Examine: void[9x11] - 49
Examine: EXPR[10x4] - 28
Examine: ([10x4] - 27
Examine: test[10x0] - 58
Examine: MODIFIERS[13x4] - 5
Examine: private[13x4] - 61
Examine: void[13x12] - 49
Examine: }[13x25] - 73

As you can see only the first annotation is given correctly. Then it starts giving the @ nodes which results in it skipping over the ANNOTATION nodes at 8x4.
It looks like when it reaches the last sibling and goes to the parent, it skips over the parent's sibling and jumps straight to the first child.
So I believe the issue to be with findFirstTokenByPredicate.
Further, I think lines 196 and 197 (in the code reference in the line above) are swapped and that is where the problem is.

@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Feb 16, 2017

Member

@romani As shown in the debug output above, do you know why DesignForExtensionCheck.hasAnnotation has to examine the contents of the method? Won't this lead to false positives for this method?

Examine: test[10x0] - 58 This line is the method call inside method2.

Member

rnveach commented Feb 16, 2017

@romani As shown in the debug output above, do you know why DesignForExtensionCheck.hasAnnotation has to examine the contents of the method? Won't this lead to false positives for this method?

Examine: test[10x0] - 58 This line is the method call inside method2.

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Feb 16, 2017

Member

it should not review content of method. modificators and annotations is enough for decision.

Member

romani commented Feb 16, 2017

it should not review content of method. modificators and annotations is enough for decision.

@MEZk

This comment has been minimized.

Show comment
Hide comment
@MEZk

MEZk Feb 16, 2017

Contributor

I'll check this ASAP.

Contributor

MEZk commented Feb 16, 2017

I'll check this ASAP.

MEZk added a commit to MEZk/checkstyle that referenced this issue Feb 18, 2017

MEZk added a commit to MEZk/checkstyle that referenced this issue Feb 18, 2017

MEZk added a commit to MEZk/checkstyle that referenced this issue Feb 19, 2017

MEZk added a commit to MEZk/checkstyle that referenced this issue Feb 19, 2017

MEZk added a commit to MEZk/checkstyle that referenced this issue Feb 20, 2017

rnveach added a commit that referenced this issue Feb 23, 2017

@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Feb 23, 2017

Member

Fix is merged

Member

rnveach commented Feb 23, 2017

Fix is merged

@rnveach rnveach closed this Feb 23, 2017

@rnveach rnveach added this to the 7.6 milestone Feb 23, 2017

@rnveach rnveach added the bug label Feb 23, 2017

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