Skip to content

Commit

Permalink
feat: Make Sonar checks accessible by type and key (#169)
Browse files Browse the repository at this point in the history
* Refactor sonar checks to be accessible by type

* Add rudimentary tests for Checks class

* Refactor getting checks by key to a public method

* Make check maps unmodifiable

* Rename primary checks map

* Remove redundant whitespace

* Reformat with spotless

* Add tests for exceptional behavior of getChecks methods

* Remove redundant import
  • Loading branch information
slarse authored Oct 28, 2020
1 parent 3b20395 commit 59ab66c
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 187 deletions.
149 changes: 2 additions & 147 deletions src/main/java/sorald/Constants.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
package sorald;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.sonar.java.checks.*;
import org.sonar.java.checks.naming.MethodNamedEqualsCheck;
import org.sonar.java.checks.naming.MethodNamedHashcodeOrEqualCheck;
import org.sonar.java.checks.serialization.CustomSerializationMethodCheck;
import org.sonar.java.checks.serialization.ExternalizableClassConstructorCheck;
import org.sonar.java.checks.serialization.SerializableFieldInSerializableClassCheck;
import org.sonar.java.checks.serialization.SerializableObjectInSessionCheck;
import org.sonar.java.checks.serialization.SerializableSuperConstructorCheck;
import org.sonar.java.checks.spring.ControllerWithSessionAttributesCheck;
import org.sonar.java.checks.spring.SpringComponentWithWrongScopeCheck;
import org.sonar.java.checks.spring.SpringIncompatibleTransactionalCheck;
import org.sonar.java.checks.spring.SpringScanDefaultPackageCheck;
import org.sonar.java.checks.synchronization.DoubleCheckedLockingCheck;
import org.sonar.java.checks.synchronization.SynchronizationOnGetClassCheck;
import org.sonar.java.checks.synchronization.TwoLocksWaitCheck;
import org.sonar.java.checks.synchronization.ValueBasedObjectUsedForLockCheck;
import org.sonar.java.checks.unused.UnusedReturnedDataCheck;
import org.sonar.java.checks.unused.UnusedThrowableCheck;
import org.sonar.java.se.checks.*;
import org.sonar.plugins.java.api.JavaFileScanner;
import sorald.sonar.Checks;

public class Constants {

Expand Down Expand Up @@ -71,134 +54,6 @@ public class Constants {
public static final List<Class<? extends JavaFileScanner>> SONAR_CHECK_CLASSES;

static {
List<Class<? extends JavaFileScanner>> sonarChecks = new ArrayList<>();
sonarChecks.add(ControllerWithSessionAttributesCheck.class);
sonarChecks.add(SpringScanDefaultPackageCheck.class);
sonarChecks.add(TwoLocksWaitCheck.class);
sonarChecks.add(PreparedStatementAndResultSetCheck.class);
sonarChecks.add(ThreadSleepCheck.class);
sonarChecks.add(PrintfFailCheck.class);
sonarChecks.add(ThreadWaitCallCheck.class);
sonarChecks.add(SpringIncompatibleTransactionalCheck.class);
sonarChecks.add(DoubleCheckedLockingCheck.class);
sonarChecks.add(GettersSettersOnRightFieldCheck.class);
sonarChecks.add(RunFinalizersCheck.class);
sonarChecks.add(ScheduledThreadPoolExecutorZeroCheck.class);
sonarChecks.add(ReuseRandomCheck.class);
sonarChecks.add(ObjectFinalizeOverloadedCheck.class);
sonarChecks.add(ReturnInFinallyCheck.class);
sonarChecks.add(ThreadLocalCleanupCheck.class);
sonarChecks.add(CompareStringsBoxedTypesWithEqualsCheck.class);
sonarChecks.add(InputStreamReadCheck.class);
sonarChecks.add(CompareToNotOverloadedCheck.class);
sonarChecks.add(IterableIteratorCheck.class);
sonarChecks.add(OverwrittenKeyCheck.class);
sonarChecks.add(DateFormatWeekYearCheck.class);
sonarChecks.add(UnusedThrowableCheck.class);
sonarChecks.add(CollectionSizeAndArrayLengthCheck.class);
sonarChecks.add(AllBranchesAreIdenticalCheck.class);
sonarChecks.add(SynchronizedOverrideCheck.class);
sonarChecks.add(ValueBasedObjectUsedForLockCheck.class);
sonarChecks.add(AssertOnBooleanVariableCheck.class);
sonarChecks.add(VolatileVariablesOperationsCheck.class);
sonarChecks.add(SynchronizationOnGetClassCheck.class);
sonarChecks.add(DoubleCheckedLockingAssignmentCheck.class);
sonarChecks.add(StringCallsBeyondBoundsCheck.class);
sonarChecks.add(RawByteBitwiseOperationsCheck.class);
sonarChecks.add(SyncGetterAndSetterCheck.class);
sonarChecks.add(StaticMultithreadedUnsafeFieldsCheck.class);
sonarChecks.add(NullShouldNotBeUsedWithOptionalCheck.class);
sonarChecks.add(DoublePrefixOperatorCheck.class);
sonarChecks.add(WrongAssignmentOperatorCheck.class);
sonarChecks.add(UnusedReturnedDataCheck.class);
sonarChecks.add(InappropriateRegexpCheck.class);
sonarChecks.add(NotifyCheck.class);
sonarChecks.add(SynchronizedFieldAssignmentCheck.class);
sonarChecks.add(SerializableObjectInSessionCheck.class);
sonarChecks.add(WaitInSynchronizeCheck.class);
sonarChecks.add(ForLoopFalseConditionCheck.class);
sonarChecks.add(ForLoopIncrementSignCheck.class);
sonarChecks.add(TransactionalMethodVisibilityCheck.class);
sonarChecks.add(ServletInstanceFieldCheck.class);
sonarChecks.add(ToStringReturningNullCheck.class);
sonarChecks.add(EqualsOnAtomicClassCheck.class);
sonarChecks.add(IgnoredReturnValueCheck.class);
sonarChecks.add(ConfusingOverloadCheck.class);
sonarChecks.add(CollectionInappropriateCallsCheck.class);
sonarChecks.add(SillyEqualsCheck.class);
sonarChecks.add(PrimitiveWrappersInTernaryOperatorCheck.class);
sonarChecks.add(InterruptedExceptionCheck.class);
sonarChecks.add(ThreadOverridesRunCheck.class);
sonarChecks.add(LongBitsToDoubleOnIntCheck.class);
sonarChecks.add(UselessIncrementCheck.class);
sonarChecks.add(SillyStringOperationsCheck.class);
sonarChecks.add(NonSerializableWriteCheck.class);
sonarChecks.add(ArrayHashCodeAndToStringCheck.class);
sonarChecks.add(CollectionCallingItselfCheck.class);
sonarChecks.add(BigDecimalDoubleConstructorCheck.class);
sonarChecks.add(InvalidDateValuesCheck.class);
sonarChecks.add(ReflectionOnNonRuntimeAnnotationCheck.class);
sonarChecks.add(CustomSerializationMethodCheck.class);
sonarChecks.add(ExternalizableClassConstructorCheck.class);
sonarChecks.add(ClassComparedByNameCheck.class);
sonarChecks.add(DuplicateConditionIfElseIfCheck.class);
sonarChecks.add(SynchronizationOnStringOrBoxedCheck.class);
sonarChecks.add(HasNextCallingNextCheck.class);
sonarChecks.add(IdenticalOperandOnBinaryExpressionCheck.class);
sonarChecks.add(LoopExecutingAtMostOnceCheck.class);
sonarChecks.add(SelfAssignementCheck.class);
sonarChecks.add(StringBufferAndBuilderWithCharCheck.class);
sonarChecks.add(MethodNamedHashcodeOrEqualCheck.class);
sonarChecks.add(ThreadRunCheck.class);
sonarChecks.add(MethodNamedEqualsCheck.class);
sonarChecks.add(DoubleBraceInitializationCheck.class);
sonarChecks.add(VolatileNonPrimitiveFieldCheck.class);
sonarChecks.add(ToArrayCheck.class);
sonarChecks.add(AbsOnNegativeCheck.class);
sonarChecks.add(IgnoredStreamReturnValueCheck.class);
sonarChecks.add(IteratorNextExceptionCheck.class);
sonarChecks.add(CompareToResultTestCheck.class);
sonarChecks.add(CastArithmeticOperandCheck.class);
sonarChecks.add(ShiftOnIntOrLongCheck.class);
sonarChecks.add(CompareToReturnValueCheck.class);
sonarChecks.add(ImmediateReverseBoxingCheck.class);
sonarChecks.add(EqualsArgumentTypeCheck.class);
sonarChecks.add(InnerClassOfNonSerializableCheck.class);
sonarChecks.add(SerializableSuperConstructorCheck.class);
sonarChecks.add(ParameterReassignedToCheck.class);
sonarChecks.add(EqualsOverridenWithHashCodeCheck.class);
sonarChecks.add(ObjectFinalizeOverridenCallsSuperFinalizeCheck.class);
sonarChecks.add(SpringComponentWithWrongScopeCheck.class);
sonarChecks.add(ConstructorInjectionCheck.class);
sonarChecks.add(ClassWithoutHashCodeInHashStructureCheck.class);
sonarChecks.add(InstanceOfAlwaysTrueCheck.class);
sonarChecks.add(NullDereferenceInConditionalCheck.class);
sonarChecks.add(FloatEqualityCheck.class);
sonarChecks.add(IfConditionAlwaysTrueOrFalseCheck.class);
sonarChecks.add(ObjectFinalizeCheck.class);
sonarChecks.add(GetClassLoaderCheck.class);
sonarChecks.add(MathOnFloatCheck.class);
sonarChecks.add(SymmetricEqualsCheck.class);

sonarChecks.add(ObjectOutputStreamCheck.class);
sonarChecks.add(NoWayOutLoopCheck.class);
sonarChecks.add(UnclosedResourcesCheck.class);
sonarChecks.add(DivisionByZeroCheck.class);
sonarChecks.add(LocksNotUnlockedCheck.class);
sonarChecks.add(StreamConsumedCheck.class);
sonarChecks.add(StreamNotConsumedCheck.class);
sonarChecks.add(OptionalGetBeforeIsPresentCheck.class);
sonarChecks.add(MinMaxRangeCheck.class);
sonarChecks.add(ConditionalUnreachableCodeCheck.class);
sonarChecks.add(NullDereferenceCheck.class);
sonarChecks.add(NonNullSetToNullCheck.class);
sonarChecks.add(CustomUnclosedResourcesCheck.class);
sonarChecks.add(DeadStoreCheck.class);
sonarChecks.add(SerializableFieldInSerializableClassCheck.class);

// sonarsChecks.add(DefaultMessageListenerContainerCheck.class);
// sonarsChecks.add(SingleConnectionFactoryCheck.class);
// sonarsChecks.add(DependencyWithSystemScopeCheck.class);
SONAR_CHECK_CLASSES = Collections.unmodifiableList(sonarChecks);
SONAR_CHECK_CLASSES = Collections.unmodifiableList(Checks.getAllChecks());
}
}
Loading

0 comments on commit 59ab66c

Please sign in to comment.