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

Fix some static analysis findings #2594

Open
2 of 4 tasks
timtebeek opened this issue Feb 8, 2023 · 7 comments
Open
2 of 4 tasks

Fix some static analysis findings #2594

timtebeek opened this issue Feb 8, 2023 · 7 comments
Labels
Ideal for Contribution Priority 4: Would Lowest priority. Would-be-nice to include issues when time allows it. Type: Enhancement Use to signal an issue enhances an already existing feature of the project.

Comments

@timtebeek
Copy link
Contributor

timtebeek commented Feb 8, 2023

Hi! It's me again with some more automated fixes. Rather than directly open several pull requests and potentially overwhelm you with review work, I'd thought this time around give you the option to check off what is and isn't welcome as a contribution, to hopefully make it a bit self served. Feel free to commit or create PRs for the trivial stuff yourself through the provided preview recipe links; I'm just hoping to be helpful looking at the current Sonar findings.

Enhancement Description

  • Code cleanup:

    • ConvertLogMessageMessageOnlyToLogMessageAndThrowable: {}
    • ParameterizedLogging
    • DefaultComesLast
    • EmptyBlock
    • EmptyNewlineAtEndOfFile
    • ForLoopControlVariablePostfixOperators
    • FinalizePrivateFields
    • MethodParamPad
    • NoWhitespaceAfter
    • NoWhitespaceBefore
    • PadEmptyForLoopComponents
    • TypecastParenPad
    • EqualsAvoidsNull
    • ExplicitInitialization
    • FallThrough
    • HideUtilityClassConstructor
    • NeedBraces
    • OperatorWrap
    • UnnecessaryParentheses
    • ReplaceThreadRunWithThreadStart
  • Fix Common static analysis issues:

    • ConvertLogMessageMessageOnlyToLogMessageAndThrowable: {}
    • ParameterizedLogging
    • AddSerialVersionUidToSerializable
    • AtomicPrimitiveEqualsUsesGet
    • BigDecimalRoundingConstantsToEnums
    • BooleanChecksNotInverted
    • CaseInsensitiveComparisonsDoNotChangeCase
    • CatchClauseOnlyRethrows
    • CovariantEquals
    • DefaultComesLast
    • EmptyBlock
    • EqualsAvoidsNull
    • ExplicitInitialization
    • ExternalizableHasNoArgsConstructor
    • FinalizePrivateFields
    • FallThrough
    • FixStringFormatExpressions
    • ForLoopIncrementInUpdate
    • IndexOfChecksShouldUseAStartPosition
    • IndexOfReplaceableByContains
    • IndexOfShouldNotCompareGreaterThanZero
    • InlineVariable
    • IsEmptyCallOnCollections
    • LambdaBlockToExpression
    • LowercasePackage
    • MethodNameCasing: {}
    • MinimumSwitchCases
    • ModifierOrder
    • MultipleVariableDeclarations
    • NeedBraces
    • NestedEnumsAreNotStatic
    • NewStringBuilderBufferWithCharArgument
    • NoDoubleBraceInitialization
    • NoEmptyCollectionWithRawType
    • NoEqualityInForCondition
    • NoFinalizer
    • NoPrimitiveWrappersForToStringOrCompareTo
    • NoRedundantJumpStatements
    • NoToStringOnStringType
    • NoValueOfOnStringType
    • ObjectFinalizeCallsSuper
    • PrimitiveWrapperClassConstructorToValueOf
    • RedundantFileCreation
    • RemoveExtraSemicolons
    • RenameLocalVariablesToCamelCase
    • RenameMethodsNamedHashcodeEqualOrTostring
    • RenamePrivateFieldsToCamelCase
    • ReplaceLambdaWithMethodReference
    • SimplifyBooleanExpression
    • SimplifyBooleanReturn
    • StaticMethodNotFinal
    • StringLiteralEquality
    • UnnecessaryCloseInTryWithResources
    • UnnecessaryExplicitTypeArguments
    • UnnecessaryParentheses
    • UnnecessaryPrimitiveAnnotations
    • UpperCaseLiteralSuffixes
    • UseDiamondOperator
    • UseJavaStyleArrayDeclarations
    • UseLambdaForFunctionalInterface
    • WhileInsteadOfFor
    • WriteOctalValuesAsDecimal
  • Apply (some of the) SLF4J best practices:

    • Convert Logger#error|warn(Throwable#message) to Logger#error|warn(, e)
    • Loggers should be named for their enclosing classes
    • Parameterize SLF4J logging statements
    • SLF4J logging statements should begin with constants
  • Spring Boot 2.x best practices:

    • NoRequestMappingAnnotation
    • ImplicitWebAnnotationNames
    • UnnecessarySpringExtension
    • BeanMethodsNotPublic
    • NoAutowiredOnConstructor
    • ConditionalOnBeanAnyNestedCondition
    • RestTemplateBuilderRequestFactory
    • ReplaceDeprecatedEnvironmentTestUtils

The above changes should all just work; if not it's a bug and I'd very much like to hear about it!

@timtebeek timtebeek added the Type: Enhancement Use to signal an issue enhances an already existing feature of the project. label Feb 8, 2023
@timtebeek
Copy link
Contributor Author

Turns out the preview links I posted earlier aren't as stable as I had hoped; I'll see about updating those. Here's how I created those links:

  1. Add a new repository group, containing AxonFramework
    image

  2. Use the Catalog or recipe links above to navigate; for instance: SLF4J Best Practices

  3. (De)select recipes from the Recipe List as desired
    image

  4. Click "DRY RUN" to execute the recipes

  5. The results will show in Recent recipe runs, for inspection and to possibly apply them through a commit or pull request.

@smcvb smcvb added Priority 4: Would Lowest priority. Would-be-nice to include issues when time allows it. Ideal for Contribution labels Feb 10, 2023
@smcvb
Copy link
Member

smcvb commented Feb 10, 2023

Welcome back, @timtebeek!

Although these are minor wins and not overly important for the inner workings of Axon Framework, I am very much into aligning the code base with standards.
Either through our own desired format or as stated by others (like what you've shared).

So, any of the pointers you've shared are free game to pick up if you want.
I will, of course, not guarantee an automatic merge from any of these; we'll most definitely review them before approval. :-)

But, as always, any form of contribution to Axon Framework is very much appreciated!

@timtebeek
Copy link
Contributor Author

As also mentioned here; there's also a recipe to consistently apply the Apache license header.
At present it would add the license header to a further 99 files: https://public.moderne.io/results/VBLvS

@timtebeek
Copy link
Contributor Author

@CodeDrivenMitch I had another quick look at running recipes for Axon, as we discussed, and figured these two might be worth applying since they ensure logging statements start with a constant rather than String format or concatenation: https://app.moderne.io/results/0NzdYUZ5d

type: specs.openrewrite.org/v1beta/recipe
name: com.github.timtebeek
displayName: Parameterized logging
description: With constant log messages
recipeList:
  - org.openrewrite.java.logging.slf4j.ParameterizedLogging
  - org.openrewrite.java.logging.slf4j.Slf4jLogShouldBeConstant

In terms of changes you'd get something like the following
image

The only thing I'm not sure about is how strict you all are on having those line breaks on the logged messages. Personally I feel having a constant String would be better, also when folks search for messages through Google, but it's a matter of taste and up to you.

@smcvb
Copy link
Member

smcvb commented Nov 21, 2023

The use of String#format can definitely go. If anything, that's POV from long ago or contributions; so no "religious" desire to have it that way.
But, the last one "seems" like a constant to me, but I guess it isn't due to the concatenation then?

@timtebeek
Copy link
Contributor Author

The use of String#format can definitely go. If anything, that's POV from long ago or contributions; so no "religious" desire to have it that way.

That's at least something to apply through that ParameterizedLogging then.

But, the last one "seems" like a constant to me, but I guess it isn't due to the concatenation then?

Only after compilation does that string concatenation become a constant, provided there's no variables in there.
(neat rabbit hole: Java 21: So How Should We Construct Strings Now?)

By only using constants you stop the pattern of string concatenation for logging statements, which here might have been fine, but you don't want to have to check every instance where that occurs. Slf4jLogShouldBeConstant can be run separately to see how you like the changes.

@smcvb
Copy link
Member

smcvb commented Nov 21, 2023

Awesome, I learned something new today! Thanks for the eye-opener, @timtebeek.
And agreed, running ParameterizedLogging and Slf4jLogShouldBeConstant makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ideal for Contribution Priority 4: Would Lowest priority. Would-be-nice to include issues when time allows it. Type: Enhancement Use to signal an issue enhances an already existing feature of the project.
Projects
None yet
Development

No branches or pull requests

2 participants