Repository navigation
Conversation
|
There's already an intellij code style config at https://github.com/pmd/build-tools/blob/main/intellij-idea/PMD-code-style.xml |
dd1c27d to
6b3b386
Compare
Thank you for the hint. I updated the integrated file accordingly. |
|
Please don't include the intellij config at all. We don't want to have to maintain two copies of it. |
Currently everybody has to maintain the codestyle because you manually have to get, update and apply it to your projects. How about then just keeping the one that is automatically used to configure the IDE when opening the project and not the one that is you get by:
Anyway I will rollback these changes for now on this PR. |
6b3b386 to
c23cb51
Compare
|
Hey, thanks for this PR and sorry for the late feedback! Could you please:
|
a433c43 to
385df32
Compare
* Add/improve docs * Do a fast exit once it's detected that the type is over the length limit * Reuse the code from pretty print to not copy paste it * Improve violation msg * Add tests
58adc96 to
9ce19f2
Compare
|
@UncleOwen |
UncleOwen
left a comment
There was a problem hiding this comment.
Partial review. I will continue, once these concerns are addressed.
I'm also worried about this comment at the top of TypePrettyPrint.java:
/**
* Pretty-printing methods to display types. The current API is only
* offered for debugging, not for displaying types to users.
*/@oowekyala Do you remember, why this is there?
| try { | ||
| variableTypeMirror.acceptVisitor(Visitor.INSTANCE, state); | ||
| } catch (OverLengthLimitException ignored) { | ||
| // Quick exit/abort to not compute full string length |
There was a problem hiding this comment.
Have you measured this? I really don't like using exceptions like this. Computing the total length and checking it here would make the code much simpler, IMHO.
There was a problem hiding this comment.
When this rule is active likely all var statements in the scanned code will be greater than the limit or the user would have fixed them otherwise.
Computing/Resolving/Visiting any further elements once we are over the limit makes no sense and would just be a waste of resources and time.
I'm not sure if there is a better way (can the visiting somehow be canceled maybe?) but the exception based approach looked like a straightforward one.
And no I did no measurement as I deemed it not necessary as described by my above theoretical assumptions.
There was a problem hiding this comment.
Creating an exception isn't cheap either, filling in the stack trace is an expansive native call.
Please start from the most simple, most readable code (that is in this case: just build the full type name) and show that we actually need performance optimizations. Otherwise I'll think this is all premature optimization.
| final int limitLength; | ||
| final List<String> currentClassDeclarationCanonicalPrefixes; | ||
|
|
||
| int currentLength; |
There was a problem hiding this comment.
Do we need this variable? Isn't this just super.sb.length()?
There was a problem hiding this comment.
Yes, because the check for this variable is executed before the string builder is append.
There was a problem hiding this comment.
See my note about premature optimization.
| if (!s.currentClassDeclarationCanonicalPrefixes.isEmpty()) { | ||
| // last -> top-most | ||
| final String topMostCurrentClassDeclarationBinaryName = | ||
| s.currentClassDeclarationCanonicalPrefixes.get( | ||
| s.currentClassDeclarationCanonicalPrefixes.size() - 1); | ||
| if (canonicalName.startsWith(topMostCurrentClassDeclarationBinaryName)) { | ||
| final Optional<String> optMostMatchedCanonicalClassPrefix = | ||
| s.currentClassDeclarationCanonicalPrefixes.stream() | ||
| .filter(canonicalName::startsWith) | ||
| .findFirst(); | ||
| if (optMostMatchedCanonicalClassPrefix.isPresent()) { | ||
| s.append(canonicalName.substring(optMostMatchedCanonicalClassPrefix.get().length())); | ||
| return; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I don't understand what you're doing here. Is this for nested classes? Can't you get whatever you need here from enclosing.getSymbol()?
There was a problem hiding this comment.
Correct.
This code is for resolving the names of subclasses / nested classes. (e.g. simplifying Foo.Bar to Bar when inside Foo)
However enclosing is not usable because it's null here.
If there is a better way, feel free to leave a comment :)
There was a problem hiding this comment.
Ah, so you're calculating the relative type name, so to speak.
enclosing is null, because none of your test cases have any nested classes. At least that's my guess.
| </example> | ||
| </rule> | ||
|
|
||
| <rule name="AvoidVarForShortType" |
There was a problem hiding this comment.
Thanks for the PR!
Just one note on the name of the new rule. Using "short" here immediately made me think of the actual short primitive type that Java has. And we once (in PMD 6) had a rule AvoidUsingShortType...
So maybe something like "AvoidVarForSimpleType" might be better?
We use however "short" already for a couple of other rules in the same meaning like here (ShortClassName, ShortMethodName, ShortVariable), in that sense it seems to be consistent with what we have. Maybe the combination "ShortType" is what makes it misleading for me?
Maybe a more different name would be better? Something more neutral, like "VarForSimpleType" maybe? Looking at https://docs.pmd-code.org/latest/pmd_devdocs_major_rule_guidelines.html that name would better comply with "Do use rule names that name the underlying problem that violations exhibit" - the word "avoid" is just the reason, why one wants to have this rule enabled...
|
There is another rule for controlling when |
|
Ok so regarding all the comments here: Thank you for the hint with As for the name if this would be a separate rule: How about something like |
* restored original CaptureMatcher * refactored `computeCurrentClassDeclarationCanonicalPrefixes` because recursion is not needed * improved logic for limit
|
@UncleOwen |
|
Merging the two rules should be straightforward: Convert UseExplicitTypes to a java rule, then add what we have here. |
I've started a prototype for this approach -> #7088 I think, the new feature (allow var when the type name would be longer than ...) nicely fits into the existing rule. |
Describe the PR
The PR introduces a new rule:
AvoidVarForShortType(see docs in PR and issue below for details)Other noteworthy changes in this PR:
TypePrettyPrinter, moved it and made it overrideable - as it's needed by the new rule and to not copy paste it's code into the rule (which would result in maintaining the same code twice)Added IntelliJ IDEA code style configuration (based on https://github.com/pmd/build-tools/blob/main/intellij-idea/PMD-code-style.xml) because the imports kept getting sorted incorrectly which caused a ton of unwanted code changesRelated issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)