Skip to content
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

Issue #13930: Migrate magicnumber.xml.template to use properties template #13971

Merged
merged 1 commit into from
Nov 15, 2023

Conversation

relentless-pursuit
Copy link

@relentless-pursuit relentless-pursuit commented Oct 30, 2023

Resolves #13930

@romani
Copy link
Member

romani commented Nov 3, 2023

@relentless-pursuit , please rebase.
Dependency update is merged

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items

src/xdocs/checks/coding/magicnumber.xml Outdated Show resolved Hide resolved
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am good to merge if this works, but last question:

@relentless-pursuit relentless-pursuit force-pushed the macrosMagicNumber branch 3 times, most recently from 10ff602 to 9d404cd Compare November 7, 2023 19:36
@romani
Copy link
Member

romani commented Nov 9, 2023

@relentless-pursuit , looks good to me, please make CI green.

@relentless-pursuit
Copy link
Author

relentless-pursuit commented Nov 9, 2023

@relentless-pursuit , looks good to me, please make CI green.

@romani, can you help me with any insights why CI is failing?

@romani
Copy link
Member

romani commented Nov 9, 2023

@relentless-pursuit , please report issue on this Check to sevntu issue tracker.
As workaround please make NON_BASE_TOKEN_PROPERTIES public, it is final and unmodified collection, so no harm.

@rnveach
Copy link
Member

rnveach commented Nov 9, 2023

@romani Why do you think this a bug?

Unexpected getter name. [SimpleAccessorNameNotation]

According to the code, if a method is a getter, has no parameters, only is a return statement with an expression, and we identify some type of field in the expression, then that field must have the same name (ignoring casing) as the method.
Field is NON_BASE_TOKEN_PROPERTIES and method is NonBaseTokenProperties. The field has _ while the method does not.

Why isn't NON_BASE_TOKEN_PROPERTIES made pubic? It appears to be immutable.

@romani
Copy link
Member

romani commented Nov 10, 2023

Why isn't NON_BASE_TOKEN_PROPERTIES made pubic? It appears to be immutable.

Yes.

Field is NON_BASE_TOKEN_PROPERTIES and method is NonBaseTokenProperties. The field has _ while the method does not.

It is unnatural to have getter with _ in name. Check should handle somehow fact that static final is forces to be upper cased and highly likely be named with _. But in getter better to not leak _.

@relentless-pursuit relentless-pursuit force-pushed the macrosMagicNumber branch 2 times, most recently from d8c98bf to dce5087 Compare November 10, 2023 02:49
@romani
Copy link
Member

romani commented Nov 10, 2023

Circle CI:

com.puppycrawl.tools.checkstyle.site
<entry_point TYPE="method" FQNAME="com.puppycrawl.tools.checkstyle.site.PropertiesMacro void writePropertyDefaultValueCell(org.apache.maven.doxia.sink.Sink sink, java.lang.String propertyName, java.lang.reflect.Field field, java.lang.Object instance)"/>
<problem_class id="NegativelyNamedBooleanVariable" severity="ERROR" attribute_key="ERRORS_ATTRIBUTES">Negatively named boolean variable</problem_class>
Boolean variable isNonBaseTokenProp is negatively named #loc
<highlighted_element>isNonBaseTokenProp</highlighted_element>

Ok, please name it isSpecialTokenProp

@relentless-pursuit relentless-pursuit force-pushed the macrosMagicNumber branch 2 times, most recently from 0867b15 to 72bb03c Compare November 10, 2023 03:14
@relentless-pursuit
Copy link
Author

Circle CI:

com.puppycrawl.tools.checkstyle.site
<entry_point TYPE="method" FQNAME="com.puppycrawl.tools.checkstyle.site.PropertiesMacro void writePropertyDefaultValueCell(org.apache.maven.doxia.sink.Sink sink, java.lang.String propertyName, java.lang.reflect.Field field, java.lang.Object instance)"/>
<problem_class id="NegativelyNamedBooleanVariable" severity="ERROR" attribute_key="ERRORS_ATTRIBUTES">Negatively named boolean variable</problem_class>
Boolean variable isNonBaseTokenProp is negatively named #loc
<highlighted_element>isNonBaseTokenProp</highlighted_element>

Ok, please name it isSpecialTokenProp

done

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge if ci pass

@@ -1037,7 +1037,8 @@ private static void validatePropertySectionPropertyEx(String fileName, String se
if (expectedValue != null) {
final String actualValue = columns.get(3).getTextContent().trim()
.replaceAll("\\s+", " ")
.replaceAll("\\s,", ",");
.replaceAll("\\s,", ",")
.replaceAll("\\s*\\.$", "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

Copy link
Author

@relentless-pursuit relentless-pursuit Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actual value: ARRAY_INIT, ASSIGN, DIV, ELIST, EXPR, LITERAL_NEW, METHOD_CALL, MINUS, PLUS, STAR, TYPECAST, UNARY_MINUS, UNARY_PLUS .
expect value: ARRAY_INIT, ASSIGN, DIV, ELIST, EXPR, LITERAL_NEW, METHOD_CALL, MINUS, PLUS, STAR, TYPECAST, UNARY_MINUS, UNARY_PLUS

i had to trim any period at the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this an issue in your PR? We have periods in other places.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below is the xml for atclauseorder for target property

<tr>
              <td>target</td>
              <td>Specify block tags targeted.</td>
              <td>subset of tokens
                <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">
                  TokenTypes
                </a>
              </td>
              <td>
                <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
                  CLASS_DEF</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMPACT_CTOR_DEF">
                  COMPACT_CTOR_DEF</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">
                  CTOR_DEF</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
                  ENUM_DEF</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
                  INTERFACE_DEF</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
                  METHOD_DEF</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
                  RECORD_DEF</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
                  VARIABLE_DEF</a>
              </td>
              <td>6.0</td>
            </tr>

Below is the xml for magicnumber for property constantWaiverParentToken

            <tr>
              <td>constantWaiverParentToken</td>
              <td>Specify tokens that are allowed in the AST path from the number literal to the enclosing constant definition.</td>
              <td>subset of tokens <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">TokenTypes</a></td>
              <td>
                <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ARRAY_INIT">
                    ARRAY_INIT</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ASSIGN">
                    ASSIGN</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DIV">
                    DIV</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ELIST">
                    ELIST</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#EXPR">
                    EXPR</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW">
                    LITERAL_NEW</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_CALL">
                    METHOD_CALL</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#MINUS">
                    MINUS</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PLUS">
                    PLUS</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STAR">
                    STAR</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#TYPECAST">
                    TYPECAST</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#UNARY_MINUS">
                    UNARY_MINUS</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#UNARY_PLUS">
                    UNARY_PLUS</a>
                  .
              </td>
              <td>6.11</td>
            </tr>

I think period is not uniform.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was reported in this PR because as far as i have observed in the files which had issues with migration in properties section, the PropertiesMacro.java has some code which adds period based on conditionals.

private static void writeTokensList(Sink sink, List<String> tokens, String tokenTypesLink)
            throws MacroExecutionException {
        for (int index = 0; index < tokens.size(); index++) {
            final String token = tokens.get(index);
            sink.rawText(INDENT_LEVEL_16);
            if (index != 0) {
                sink.text(SiteUtil.COMMA_SPACE);
            }
            writeLinkToToken(sink, tokenTypesLink, token);
        }
        if (tokens.isEmpty()) {
            sink.rawText(CODE_START);
            sink.text("empty");
            sink.rawText(CODE_END);
        }
        else {
            sink.rawText(INDENT_LEVEL_18);
            sink.rawText(SiteUtil.DOT);
            sink.rawText(INDENT_LEVEL_14);
        }
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please share output on how they are failing. We need to show such details with @rnveach to let him understand issue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please share output on how they are failing. We need to show such details with @rnveach to let him understand issue

I have already shared the failures. Re-sharing it again and adding some context to it.

for magicnumber.xml,

actual value: ARRAY_INIT, ASSIGN, DIV, ELIST, EXPR, LITERAL_NEW, METHOD_CALL, MINUS, PLUS, STAR, TYPECAST, UNARY_MINUS, UNARY_PLUS .
expect value: ARRAY_INIT, ASSIGN, DIV, ELIST, EXPR, LITERAL_NEW, METHOD_CALL, MINUS, PLUS, STAR, TYPECAST, UNARY_MINUS, UNARY_PLUS

for multiplestringliterals.xml

multiplestringliterals.xml section 'MultipleStringLiterals' should have the value for ignoreOccurrenceContext
expected: ANNOTATION
but was : ANNOTATION .
Expected :ANNOTATION
Actual   :ANNOTATION .

This is primarily happening because we add a . in the below code:

private static void writeTokensList(Sink sink, List<String> tokens, String tokenTypesLink)
            throws MacroExecutionException {
        for (int index = 0; index < tokens.size(); index++) {
            final String token = tokens.get(index);
            sink.rawText(INDENT_LEVEL_16);
            if (index != 0) {
                sink.text(SiteUtil.COMMA_SPACE);
            }
            writeLinkToToken(sink, tokenTypesLink, token);
        }
        if (tokens.isEmpty()) {
            sink.rawText(CODE_START);
            sink.text("empty");
            sink.rawText(CODE_END);
        }
        else {
            sink.rawText(INDENT_LEVEL_18);
            sink.rawText(SiteUtil.DOT);
            sink.rawText(INDENT_LEVEL_14);
        }
    }

And, this particular piece of code from XdocsPagesTest.java gets the content by of a node and all its descendants added by PropertiesMacro.java in form of XML.

            if (expectedValue != null) {
                final String actualValue = columns.get(3).getTextContent().trim()
                        .replaceAll("\\s+", " ")
                        .replaceAll("\\s,", ",");

To illustrate further, when actualValue is retrieved it sees a . and registers it while expectedValue doesn't, giving assertion error.

            <tr>
              <td>constantWaiverParentToken</td>
              <td>Specify tokens that are allowed in the AST path from the number literal to the enclosing constant definition.</td>
              <td>subset of tokens <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">TokenTypes</a></td>
              <td>
                <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ARRAY_INIT">
                    ARRAY_INIT</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ASSIGN">
                    ASSIGN</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DIV">
                    DIV</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ELIST">
                    ELIST</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#EXPR">
                    EXPR</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW">
                    LITERAL_NEW</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_CALL">
                    METHOD_CALL</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#MINUS">
                    MINUS</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PLUS">
                    PLUS</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STAR">
                    STAR</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#TYPECAST">
                    TYPECAST</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#UNARY_MINUS">
                    UNARY_MINUS</a>
                , <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#UNARY_PLUS">
                    UNARY_PLUS</a>
                  .
              </td>
              <td>6.11</td>
            </tr>

Finally, this pattern holds true for all xml for any specific property as such:

 <td>subset of tokens <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">TokenTypes</a></td>
              <td>
               // some links
                  .
              </td>

As a proof of concept, you can try migrating atclauseordr.xml and find this failure too. Currently, atclauseorder.xml isn't migrated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnveach i have pushed a new patchset which removes the need to trim . . Infact, it removes . from the xml.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest difference doesn't have a removed . and I see an added . in the old version now, so it seems this PR was adding a period that was never being made before. That is what was different and required the extra change in the test.

this pattern holds true for all xml for any specific property as such

This is not correct, as we don't see a bigger difference in the new PR. I now see there is a difference between tokens property, while the areas without a period are not tokens properties, but are properties that specify a list of tokens.

Now this is making more sense to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now done, I do see 1 more change.

config/checkstyle-non-main-files-suppressions.xml Outdated Show resolved Hide resolved
@relentless-pursuit relentless-pursuit force-pushed the macrosMagicNumber branch 2 times, most recently from 9afdda0 to e4d8302 Compare November 10, 2023 05:37
@romani
Copy link
Member

romani commented Nov 10, 2023

Semaphore is restarted

@relentless-pursuit relentless-pursuit force-pushed the macrosMagicNumber branch 2 times, most recently from 1a934ee to 5d858d9 Compare November 14, 2023 06:13
if (isSpecialTokenProp && !CURLY_BRACKET.equals(defaultValue)) {
final List<String> defaultValuesList =
Arrays.asList(COMMA_SPACE_PATTERN.split(defaultValue));
writeTokensListDotLess(sink, defaultValuesList, SiteUtil.PATH_TO_TOKEN_TYPES);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeTokensListDotLess(sink, defaultValuesList, SiteUtil.PATH_TO_TOKEN_TYPES);
writeTokensList(sink, defaultValuesList, SiteUtil.PATH_TO_TOKEN_TYPES, false);

Add a boolean onto the end of writeTokensList, and use that to control the period. Other writeTokensList calls should use true. Remove writeTokensListDotLess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnveach, the idea of a introducing a boolean variable was insightful. thank you @romani for the help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@relentless-pursuit I am against duplicating chunks of code if we can achieve what we need with simple changes.

@romani
Copy link
Member

romani commented Nov 14, 2023

Rebased one more time to fix problem caused by non compliant releasenotes

@romani
Copy link
Member

romani commented Nov 14, 2023

@rnveach , CI is green, please finish review.

@rnveach rnveach assigned romani and unassigned rnveach Nov 15, 2023
@romani romani merged commit 439d69c into checkstyle:master Nov 15, 2023
113 checks passed
@relentless-pursuit relentless-pursuit deleted the macrosMagicNumber branch December 4, 2023 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate magicnumber.xml.template to use properties template
3 participants