From 5ae4e99b0725bafc573772be2f37ae80fe268408 Mon Sep 17 00:00:00 2001 From: Michael Yan Date: Tue, 27 Sep 2022 12:22:38 +0800 Subject: [PATCH] Codenarc integration Using Codenarc v3.1.0 Config Codenarc with Rules --- build.gradle | 6 + gradle/codenarc/codenarc.groovy | 445 ++++++++++++++++++++++++++++++++ 2 files changed, 451 insertions(+) create mode 100644 gradle/codenarc/codenarc.groovy diff --git a/build.gradle b/build.gradle index 45268235..d5244448 100644 --- a/build.gradle +++ b/build.gradle @@ -64,6 +64,7 @@ subprojects { apply plugin: 'idea' apply plugin: 'java-library' apply plugin: 'checkstyle' + apply plugin: 'codenarc' sourceCompatibility = "1.8" targetCompatibility = "1.8" @@ -120,6 +121,11 @@ subprojects { toolVersion = "10.3.2" configDirectory.set(rootProject.file("gradle/checkstyle")) } + + codenarc { + toolVersion = '3.1.0' + setConfigFile(new File("$rootDir/gradle/codenarc/codenarc.groovy")) + } } apply from: "${commonBuild}/common-docs.gradle" diff --git a/gradle/codenarc/codenarc.groovy b/gradle/codenarc/codenarc.groovy new file mode 100644 index 00000000..e10f1784 --- /dev/null +++ b/gradle/codenarc/codenarc.groovy @@ -0,0 +1,445 @@ +ruleset { + description ''' + Grails ruleset adapted from basic one from https://codenarc.org/StarterRuleSet-AllRulesByCategory.groovy.txt + Each rule is commented and priority qualified. + ''' + + // rulesets/basic.xml + AssertWithinFinallyBlock + AssignmentInConditional + BigDecimalInstantiation + BitwiseOperatorInConditional + // BooleanGetBoolean + BrokenNullCheck + BrokenOddnessCheck + // ClassForName + ComparisonOfTwoConstants + ComparisonWithSelf + ConstantAssertExpression + ConstantIfExpression + ConstantTernaryExpression + DeadCode + DoubleNegative + DuplicateCaseStatement + DuplicateMapKey + DuplicateSetValue + EmptyCatchBlock + EmptyClass + EmptyElseBlock + EmptyFinallyBlock + EmptyForStatement + EmptyIfStatement + EmptyInstanceInitializer + // EmptyMethod + EmptyStaticInitializer + EmptySwitchStatement + EmptySynchronizedStatement + EmptyTryBlock + EmptyWhileStatement + // EqualsAndHashCode + EqualsOverloaded + ExplicitGarbageCollection + ForLoopShouldBeWhileLoop + HardCodedWindowsFileSeparator + HardCodedWindowsRootDirectory + IntegerGetInteger + MultipleUnaryOperators + ParameterAssignmentInFilterClosure + RandomDoubleCoercedToZero + RemoveAllOnSelf + ReturnFromFinallyBlock + ThrowExceptionFromFinallyBlock + + // rulesets/braces.xml + ElseBlockBraces + ForStatementBraces + IfStatementBraces + WhileStatementBraces + + // rulesets/comments.xml + // ClassJavadoc + // JavadocConsecutiveEmptyLines + // JavadocEmptyAuthorTag + // JavadocEmptyExceptionTag + // JavadocEmptyFirstLine + // JavadocEmptyLastLine + // JavadocEmptyParamTag + // JavadocEmptyReturnTag + // JavadocEmptySeeTag + // JavadocEmptySinceTag + // JavadocEmptyThrowsTag + // JavadocEmptyVersionTag + // JavadocMissingExceptionDescription + // JavadocMissingParamDescription + // JavadocMissingThrowsDescription + + // rulesets/concurrency.xml + // BusyWait + // DoubleCheckedLocking + // InconsistentPropertyLocking + // InconsistentPropertySynchronization + // NestedSynchronization + // StaticCalendarField + // StaticConnection + // StaticDateFormatField + // StaticMatcherField + // StaticSimpleDateFormatField + // SynchronizedMethod + // SynchronizedOnBoxedPrimitive + // SynchronizedOnGetClass + // SynchronizedOnReentrantLock + // SynchronizedOnString + // SynchronizedOnThis + // SynchronizedReadObjectMethod + // SystemRunFinalizersOnExit + // ThisReferenceEscapesConstructor + // ThreadGroup + // ThreadLocalNotStaticFinal + // ThreadYield + // UseOfNotifyMethod + // VolatileArrayField + // VolatileLongOrDoubleField + // WaitOutsideOfWhileLoop + + // rulesets/convention.xml + // CompileStatic + // ConfusingTernary + CouldBeElvis + // CouldBeSwitchStatement + // FieldTypeRequired + // HashtableIsObsolete + // IfStatementCouldBeTernary + // ImplicitClosureParameter + // ImplicitReturnStatement + InvertedCondition + InvertedIfElse + // LongLiteralWithLowerCaseL + // MethodParameterTypeRequired + // MethodReturnTypeRequired + // NoDef + // NoDouble + // NoFloat + // NoJavaUtilDate + NoTabCharacter + // ParameterReassignment + // PublicMethodsBeforeNonPublicMethods + // StaticFieldsBeforeInstanceFields + // StaticMethodsBeforeInstanceMethods + TernaryCouldBeElvis + // TrailingComma + // VariableTypeRequired + // VectorIsObsolete + + // rulesets/design.xml + // AbstractClassWithPublicConstructor + // AbstractClassWithoutAbstractMethod + // AssignmentToStaticFieldFromInstanceMethod + // BooleanMethodReturnsNull + // BuilderMethodWithSideEffects + // CloneableWithoutClone + // CloseWithoutCloseable + // CompareToWithoutComparable + // ConstantsOnlyInterface + // EmptyMethodInAbstractClass + // FinalClassWithProtectedMember + // ImplementationAsType + // Instanceof + // LocaleSetDefault + // NestedForLoop + OptionalCollectionReturnType + OptionalField + OptionalMethodParameter + PrivateFieldCouldBeFinal + PublicInstanceField + // ReturnsNullInsteadOfEmptyArray + // ReturnsNullInsteadOfEmptyCollection + // SimpleDateFormatMissingLocale + // StatelessSingleton + // ToStringReturnsNull + + // rulesets/dry.xml + // DuplicateListLiteral + // DuplicateMapLiteral + // DuplicateNumberLiteral + // DuplicateStringLiteral + + // rulesets/enhanced.xml + // CloneWithoutCloneable + // JUnitAssertEqualsConstantActualValue + // MissingOverrideAnnotation + // UnsafeImplementationAsMap + + // rulesets/exceptions.xml + // CatchArrayIndexOutOfBoundsException + // CatchError + // CatchException + // CatchIllegalMonitorStateException + // CatchIndexOutOfBoundsException + // CatchNullPointerException + // CatchRuntimeException + // CatchThrowable + // ConfusingClassNamedException + // ExceptionExtendsError + // ExceptionExtendsThrowable + // ExceptionNotThrown + // MissingNewInThrowStatement + // ReturnNullFromCatchBlock + // SwallowThreadDeath + // ThrowError + // ThrowException + // ThrowNullPointerException + // ThrowRuntimeException + // ThrowThrowable + + // rulesets/formatting.xml + BlankLineBeforePackage + BlockEndsWithBlankLine + BlockStartsWithBlankLine + BracesForClass + BracesForForLoop + BracesForIfElse + BracesForMethod + BracesForTryCatchFinally + ClassEndsWithBlankLine + ClassStartsWithBlankLine + ClosureStatementOnOpeningLineOfMultipleLineClosure + ConsecutiveBlankLines + FileEndsWithoutNewline + Indentation + LineLength(length: 150) + MissingBlankLineAfterImports + MissingBlankLineAfterPackage + MissingBlankLineBeforeAnnotatedField + SpaceAfterCatch + SpaceAfterClosingBrace + SpaceAfterComma + SpaceAfterFor + SpaceAfterIf + SpaceAfterMethodCallName + SpaceAfterMethodDeclarationName + SpaceAfterNotOperator + SpaceAfterOpeningBrace + SpaceAfterSemicolon + SpaceAfterSwitch + SpaceAfterWhile + SpaceAroundClosureArrow + SpaceAroundMapEntryColon(characterAfterColonRegex: /\s/) + SpaceAroundOperator + SpaceBeforeClosingBrace + SpaceBeforeOpeningBrace + SpaceInsideParentheses + TrailingWhitespace + + // rulesets/generic.xml + IllegalClassMember + IllegalClassReference + IllegalPackageReference + IllegalRegex + IllegalString + IllegalSubclass + RequiredRegex + RequiredString + StatelessClass + + // // rulesets/grails.xml + // GrailsDomainGormMethods + // GrailsDomainHasEquals + // GrailsDomainHasToString + // GrailsDomainReservedSqlKeywordName + // GrailsDomainStringPropertyMaxSize + // GrailsDomainWithServiceReference + // GrailsDuplicateConstraint + // GrailsDuplicateMapping + // GrailsMassAssignment + // GrailsPublicControllerMethod + // GrailsServletContextReference + // GrailsStatelessService + + // rulesets/groovyism.xml + // AssignCollectionSort + // AssignCollectionUnique + // ClosureAsLastMethodParameter + // CollectAllIsDeprecated + // ConfusingMultipleReturns + // ExplicitArrayListInstantiation + // ExplicitCallToAndMethod + // ExplicitCallToCompareToMethod + // ExplicitCallToDivMethod + // ExplicitCallToEqualsMethod + // ExplicitCallToGetAtMethod + // ExplicitCallToLeftShiftMethod + // ExplicitCallToMinusMethod + // ExplicitCallToModMethod + // ExplicitCallToMultiplyMethod + // ExplicitCallToOrMethod + // ExplicitCallToPlusMethod + // ExplicitCallToPowerMethod + // ExplicitCallToPutAtMethod + // ExplicitCallToRightShiftMethod + // ExplicitCallToXorMethod + // ExplicitHashMapInstantiation + // ExplicitHashSetInstantiation + // ExplicitLinkedHashMapInstantiation + // ExplicitLinkedListInstantiation + // ExplicitStackInstantiation + // ExplicitTreeSetInstantiation + // GStringAsMapKey + // GStringExpressionWithinString + // GetterMethodCouldBeProperty + // GroovyLangImmutable + // UseCollectMany + // UseCollectNested + + // rulesets/imports.xml + DuplicateImport + ImportFromSamePackage + ImportFromSunPackages + MisorderedStaticImports(comesBefore: false) + NoWildcardImports + UnnecessaryGroovyImport + UnusedImport + + // rulesets/jdbc.xml + // DirectConnectionManagement + // JdbcConnectionReference + // JdbcResultSetReference + // JdbcStatementReference + + // rulesets/junit.xml + // ChainedTest + // CoupledTestCase + // JUnitAssertAlwaysFails + // JUnitAssertAlwaysSucceeds + // JUnitFailWithoutMessage + // JUnitLostTest + // JUnitPublicField + // JUnitPublicNonTestMethod + // JUnitPublicProperty + // JUnitSetUpCallsSuper + // JUnitStyleAssertions + // JUnitTearDownCallsSuper + // JUnitTestMethodWithoutAssert + // JUnitUnnecessarySetUp + // JUnitUnnecessaryTearDown + // JUnitUnnecessaryThrowsException + // SpockIgnoreRestUsed + // UnnecessaryFail + // UseAssertEqualsInsteadOfAssertTrue + // UseAssertFalseInsteadOfNegation + // UseAssertNullInsteadOfAssertEquals + // UseAssertSameInsteadOfAssertTrue + // UseAssertTrueInsteadOfAssertEquals + // UseAssertTrueInsteadOfNegation + + // rulesets/logging.xml + // LoggerForDifferentClass + // LoggerWithWrongModifiers + // LoggingSwallowsStacktrace + // MultipleLoggers + // PrintStackTrace + // Println + // SystemErrPrint + // SystemOutPrint + + // rulesets/naming.xml + AbstractClassName + ClassName + ClassNameSameAsFilename + // ClassNameSameAsSuperclass + // ConfusingMethodName + // FactoryMethodName + FieldName(ignoreFieldNames: 'serialVersionUID,log,logger') + InterfaceName + InterfaceNameSameAsSuperInterface + MethodName + ObjectOverrideMisspelledMethodName + PackageName + PackageNameMatchesFilePath + ParameterName + PropertyName + VariableName + + // rulesets/security.xml + // FileCreateTempFile + // InsecureRandom + // JavaIoPackageAccess + // NonFinalPublicField + // NonFinalSubclassOfSensitiveInterface + // ObjectFinalize + // PublicFinalizeMethod + // SystemExit + // UnsafeArrayDeclaration + + // rulesets/serialization.xml + // EnumCustomSerializationIgnored + // SerialPersistentFields + // SerialVersionUID + // SerializableClassMustDefineSerialVersionUID + + // rulesets/size.xml + // AbcMetric // Requires the GMetrics jar + // ClassSize + // CrapMetric // Requires the GMetrics jar and a Cobertura coverage file + // CyclomaticComplexity // Requires the GMetrics jar + // MethodCount + // MethodSize + // NestedBlockDepth + // ParameterCount + + // rulesets/unnecessary.xml + AddEmptyString + ConsecutiveLiteralAppends + ConsecutiveStringConcatenation + UnnecessaryBigDecimalInstantiation + UnnecessaryBigIntegerInstantiation + UnnecessaryBooleanExpression + UnnecessaryBooleanInstantiation + UnnecessaryCallForLastElement + UnnecessaryCallToSubstring + // UnnecessaryCast + // UnnecessaryCatchBlock + // UnnecessaryCollectCall + // UnnecessaryCollectionCall + // UnnecessaryConstructor + UnnecessaryDefInFieldDeclaration + UnnecessaryDefInMethodDeclaration + UnnecessaryDefInVariableDeclaration + UnnecessaryDotClass + UnnecessaryDoubleInstantiation + UnnecessaryElseStatement + UnnecessaryFinalOnPrivateMethod + UnnecessaryFloatInstantiation + UnnecessaryGString + // UnnecessaryGetter + UnnecessaryIfStatement + UnnecessaryInstanceOfCheck + UnnecessaryInstantiationToGetClass + UnnecessaryIntegerInstantiation + UnnecessaryLongInstantiation + UnnecessaryModOne + UnnecessaryNullCheck + UnnecessaryNullCheckBeforeInstanceOf + // UnnecessaryObjectReferences + // UnnecessaryOverridingMethod + UnnecessaryPackageReference + UnnecessaryParenthesesForMethodCallWithClosure + UnnecessaryPublicModifier + UnnecessaryReturnKeyword + UnnecessarySafeNavigationOperator + UnnecessarySelfAssignment + UnnecessarySemicolon + // UnnecessarySetter + UnnecessaryStringInstantiation + UnnecessaryTernaryExpression + UnnecessaryToString + UnnecessaryTransientModifier + + // rulesets/unused.xml + // UnusedArray + // UnusedMethodParameter + // UnusedObject + // UnusedPrivateField + // UnusedPrivateMethod + // UnusedPrivateMethodParameter + // UnusedVariable +} \ No newline at end of file