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 #13758: Add pattern array converter for auto bean #14740

Merged
merged 1 commit into from May 2, 2024

Conversation

Lmh-java
Copy link
Contributor

@Lmh-java Lmh-java commented Mar 30, 2024

For Issue #13758.

  1. Create auto configuration for Pattern[] with a fixed separator (in this PR)
  2. Update AbstractClassCouplingCheck to use this new property type (in this PR)
  3. Update ImportOrderCheck to replace use of Pattern[] with String[] (In this PR)

Agreement on update is archived at #14764 (comment)

@Lmh-java Lmh-java marked this pull request as ready for review March 30, 2024 07:34
@Lmh-java
Copy link
Contributor Author

Lmh-java commented Mar 30, 2024

#14740 (comment)

In this PR, I have:

  1. Create auto configuration for Pattern[] .....

@rnveach @romani Could you please share your thoughts on this plan? I truly appreciate your opinions.

Also, for #13758 (comment), which one do you think would be a better solution in our case?

Update: CI is failing, but I will fix them later if we've decided the plan :).

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.

Please read #13758 (comment)

item to disucuss

@Override
public Object convert(Class type, Object value) {
final String plainString = value.toString();
final String[] patternStrings = plainString.split(REGEX_SEPARATOR);
Copy link
Member

Choose a reason for hiding this comment

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

bad side effect of this is we should clearly mention that we do not allow , as part of regexp to match what user needs. So user can not use , in it Patterns, it is delimiter.
\, will also not work, as we simply splitting for just ,.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@romani, do you want me to close this PR for now? Since it might be confusing for others. We can re-open it later to further modify Pattern[] if it is the right time. :)

Copy link
Member

Choose a reason for hiding this comment

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

Let's keep it open until we come to agreement on how to fix issue.

@romani
Copy link
Member

romani commented Apr 8, 2024

@Lmh-java , please put in this PR all from #14764 (comment)

@Lmh-java Lmh-java force-pushed the minghao/unify-pattern-array branch 5 times, most recently from 6869cde to 5b49c08 Compare April 9, 2024 05:57
@@ -316,7 +316,8 @@
<!-- Main has an annotated field for each command line option. This is by design. -->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[@SimpleName='Checker']
| //ClassOrInterfaceDeclaration[@SimpleName='Main']"/>
| //ClassOrInterfaceDeclaration[@SimpleName='Main']
| //ClassOrInterfaceDeclaration[@SimpleName='ImportOrderCheck']"/>
Copy link
Contributor Author

@Lmh-java Lmh-java Apr 9, 2024

Choose a reason for hiding this comment

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

Suppress this because PWD complains that ImportOrderCheck has too many class fields.

Copy link
Member

Choose a reason for hiding this comment

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

I am ok with this.

Comment on lines +267 to +276
/**
* Complied array of patterns for property {@code groups}.
*/
private Pattern[] groupsReg = EMPTY_PATTERN_ARRAY;

/**
* Complied array of patterns for property {@code staticGroups}.
*/
private Pattern[] staticGroupsReg = EMPTY_PATTERN_ARRAY;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add these two more fields to store the compiled regex expressions. I have to expose String[] to users, but we also need to store the complied regex (we don't want to compile every time we want to use the regex). I am curious whether there are better solutions to this, since this change brings us more suppressions of all kinds.

Copy link
Member

@romani romani Apr 13, 2024

Choose a reason for hiding this comment

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

To next reviewers:
to avoid dealing with long discussion on what is actually a type that user should care, I am ok with this update.
I hope in some future we will agree on type system and simplify this Check

@Lmh-java Lmh-java force-pushed the minghao/unify-pattern-array branch 2 times, most recently from 3080d29 to 7d5d4d2 Compare April 10, 2024 09:44
String string = "";
entry.values().stream()
.flatMap(List::stream)
.map(Constraint::new)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Constraint::new will not be recognized as a reference and will not be recorded. I am adding this test file to pass CI right now. I think it would be better if we could fix this false negative in a separated PR to make everything easier.

Copy link
Member

Choose a reason for hiding this comment

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

Please use here File class, as you did in issue description

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 382 to 391
final String[] expected = {};

verifyWithInlineConfigParser(
getPath("InputClassFanOutComplexityLambdaNew.java"), expected);
Copy link
Contributor Author

@Lmh-java Lmh-java Apr 10, 2024

Choose a reason for hiding this comment

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

This test case is not correct, but I have to add it to pass the current CI. InputClassFanOutComplexityLambdaNew.java should NOT have 0 complexity.
See #14740 (comment)

Copy link
Member

@romani romani Apr 13, 2024

Choose a reason for hiding this comment

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

Please put comment, in code, no violations until #14787

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@Lmh-java
Copy link
Contributor Author

Lmh-java commented Apr 10, 2024

ClassFanOut produces violations when running no-exception-xwiki if I remove

excludeClassesRegexps.add(CommonUtil.createPattern("^$"));

But if I keep it, Pitest will complain about it. If this function call is removed by Pietist, nothing in our unit test will complain.

After investigating it, I found that if this line is removed, sometimes there will be a few more class references counted. This is because when handling lambda expressions with a new keyword (such as Constrant::new), the following code will get "" (an blank string)

final String fullIdentName = FullIdent.createFullIdent(ast).getText();

Therefore, ^$ will match the blank string and ignore that. However, if we set the default value of excludeClassesRegexps to an empty array. This case will not be ignored, and therefore more class references will be counted.

Ignoring such lambda expressions with ^$ can be problematic. I think the test example I added is a false negative of this case, which no reference classes are counted.
https://github.com/checkstyle/checkstyle/blob/7d5d4d21e3a139ec80120f02e08def8d347f9911/src/test/resources/com/puppycrawl/tools/checkstyle/checks/metrics/classfanoutcomplexity/InputClassFanOutComplexityLambdaNew.java
I will keep investigating this and send a new PR to fix this. For now, I have come up with a workaround to support us to finish this PR first before we move on.

PS: this is an issue with AbstractClassCouplingCheck, so it also affects ClassFanOutComplexityCheck and ClassDataAbstractionCouplingCheck

@Lmh-java Lmh-java requested a review from romani April 10, 2024 10:07
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:

config/pitest-suppressions/pitest-imports-suppressions.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.

please apply review items to all cases:

@Lmh-java Lmh-java force-pushed the minghao/unify-pattern-array branch 2 times, most recently from a70942d to 9d67d78 Compare April 13, 2024 22:21
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

@Lmh-java Lmh-java force-pushed the minghao/unify-pattern-array branch from 9d67d78 to a0fb82f Compare April 14, 2024 01:29
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

Copy link
Member

@rnveach rnveach left a comment

Choose a reason for hiding this comment

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

Start Issue #13758.

How does this start the issue but doesn't completely resolve it? What is planned next?

src/xdocs/property_types.xml Show resolved Hide resolved
@@ -371,4 +371,18 @@ public void testClearStatePackageName() throws Exception {
.isTrue();
}

/**
* Test [ClassName]::new expression. Such expression will be processed as a
Copy link
Member

Choose a reason for hiding this comment

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

What does this have to do with the pattern array converter? All the properties in the file are default which means it doesn't go against the converter, right?

Copy link
Contributor Author

@Lmh-java Lmh-java Apr 16, 2024

Choose a reason for hiding this comment

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

This has nothing to do with the converter. Please see #14740 (comment).

This is a part of the workaround for ClassFanOutCheck until #14787

TLDR;
I have to keep the following line, otherwise the CI will fail.

excludeClassesRegexps.add(CommonUtil.createPattern("^$"));

Added this test case to kill Pitest mutation for this line. After #14787 is solved, this test case will report violations.

@@ -40,7 +40,7 @@
&lt;a href="https://en.wikipedia.org/wiki/ASCII#Order"&gt;ASCII sort order&lt;/a&gt;.
It affects both type imports and static imports.</description>
</property>
<property default-value="" name="groups" type="java.util.regex.Pattern[]">
<property default-value="" name="groups" type="java.lang.String[]">
Copy link
Member

@rnveach rnveach Apr 16, 2024

Choose a reason for hiding this comment

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

Has it been discussed what impact this has on 3rd party tools like eclipse-cs or sonar-cs?

Copy link
Member

Choose a reason for hiding this comment

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

It will be declared breaking compatibility, I hope eclipscs will not be actually affected.

Copy link
Member

Choose a reason for hiding this comment

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

So we are not going to do any analysis or warning?

Copy link
Member

Choose a reason for hiding this comment

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

yes, let move forward, and just declare breaking changes.

@Lmh-java Lmh-java force-pushed the minghao/unify-pattern-array branch from 9c35f3e to 8dc065a Compare April 16, 2024 06:18
@Lmh-java Lmh-java requested a review from rnveach April 16, 2024 19:52
final String plainString = value.toString();
Pattern[] patterns = new Pattern[0];
if (!CommonUtil.isBlank(plainString)) {
final String[] patternStrings = plainString.split(COMMA_SEPARATOR);
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I missed this before.



This is similar to RelaxedStringArrayConverter and it trims the input during the split. You are not.

You can see in testBeanConverterPatternArray that the space remains in the regular expression. This violates your example you added in property_types.xml as it shows the space removed.

Originally we were using RelaxedStringArrayConverter for these type of instances, so we should continue the trimming it does. We need to implement trim in this method. I would expect spaces to be trimmed.

Copy link
Contributor Author

@Lmh-java Lmh-java Apr 20, 2024

Choose a reason for hiding this comment

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

Sorry I missed this before.

Totally no worries. This is a great catch. I was about to ask about this, but as time went by, I forgot...

Added trim. I have also updated the new converter to have the same structure with RelaxedStringArrayConverter, so we can reduce one import to Arrays.

@rnveach
Copy link
Member

rnveach commented Apr 20, 2024

Start Issue #13758.

How does this start the issue but doesn't completely resolve it? What is planned next?

I did not get a response, but I see text was updated in first post. I am more curious now. Is this PR plan to finish the issue?

@Lmh-java
Copy link
Contributor Author

Lmh-java commented Apr 20, 2024

Start Issue #13758.

How does this start the issue but doesn't completely resolve it? What is planned next?

I did not get a response, but I see text was updated in first post. I am more curious now. Is this PR plan to finish the issue?

@rnveach
Yes, I had updated the old PR description. Sorry for forgetting to reply the message. All these 3 steps listed now should be completed in this PR, and it should finish the issue.

The only thing needs to be done after this PR is #14787, but it is not relevant to #13758 (not relevant to unify pattern array.)

@Lmh-java Lmh-java force-pushed the minghao/unify-pattern-array branch 2 times, most recently from aa80438 to 6bdcb72 Compare April 20, 2024 20:36
@Lmh-java Lmh-java requested a review from rnveach April 20, 2024 20:55
@Lmh-java Lmh-java force-pushed the minghao/unify-pattern-array branch from 6bdcb72 to 9f55855 Compare April 20, 2024 21:03
@rnveach rnveach assigned nrmancuso and unassigned rnveach Apr 24, 2024
@rnveach rnveach requested a review from nrmancuso April 24, 2024 16:51
@nrmancuso
Copy link
Member

Github, generate site

@rnveach
Copy link
Member

rnveach commented May 2, 2024

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/9f55855_2024115508/property_types.html#

Weird it failed to generated the fragment Pattern[].

Edit: From just glancing, it looks like the code is looking at the source for subsection with an id and not section with a name.

@nrmancuso nrmancuso merged commit 67dd5b4 into checkstyle:master May 2, 2024
113 checks passed
@Lmh-java
Copy link
Contributor Author

Lmh-java commented May 3, 2024

@rnveach
Could you please explain a little more on "fragment Pattern[]". I am not very sure what it means.
I can see Pattern[] on the site.
Pattern[] section:
image
Content section.
image

@rnveach
Copy link
Member

rnveach commented May 3, 2024

https://en.wikipedia.org/wiki/URI_fragment
In the URL, it is represented by the # and what comes after it.

#14740 (comment)
The links generated by the GHA did not provide the correct fragment for the last one. My edit provided why it didn't.

@Lmh-java
Copy link
Contributor Author

Lmh-java commented May 3, 2024

@rnveach I get it right now.

So we expect the last link to be https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/9f55855_2024115508/property_types.html#Pattern.5B.5D
(with an anchor in the end)

I will open an issue for this to the main checkstyle repo.

@nrmancuso
Copy link
Member

@Lmh-java i think we might have an issue open for it, please try to search and see if it turns up

@Lmh-java
Copy link
Contributor Author

Lmh-java commented May 4, 2024

I didn't see an issue related to this specifically, I will try to open an issue.

Edit: please see issue #14857. Not sure about my wording, please feel free to change.

@nrmancuso
Copy link
Member

Thanks @Lmh-java !

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.

None yet

4 participants