Skip to content

Commit 31c8625

Browse files
gaurabdgromani
authored andcommitted
Issue #331: fix rule template determination logic
1 parent 04e71c4 commit 31c8625

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@
714714
<limit>
715715
<counter>BRANCH</counter>
716716
<value>COVEREDRATIO</value>
717-
<minimum>0.89</minimum>
717+
<minimum>0.92</minimum>
718718
</limit>
719719
</limits>
720720
</rule>

src/main/java/org/sonar/plugins/checkstyle/metadata/CheckstyleMetadata.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.io.IOException;
2323
import java.io.InputStream;
24-
import java.lang.reflect.Method;
2524
import java.nio.charset.StandardCharsets;
2625
import java.util.ArrayList;
2726
import java.util.Arrays;
@@ -42,6 +41,7 @@
4241
import com.google.common.collect.ImmutableList;
4342
import com.puppycrawl.tools.checkstyle.meta.ModuleDetails;
4443
import com.puppycrawl.tools.checkstyle.meta.ModulePropertyDetails;
44+
import com.puppycrawl.tools.checkstyle.meta.ModuleType;
4545
import com.puppycrawl.tools.checkstyle.meta.XmlMetaReader;
4646

4747
public class CheckstyleMetadata {
@@ -85,11 +85,12 @@ public void createRulesWithMetadata() {
8585
new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE,
8686
null, "0d 0h 5min");
8787

88-
metadataRepo
89-
.forEach((checkKey, metadata) -> {
90-
final ModuleDetails moduleDetails = metadataRepo.get(checkKey);
88+
metadataRepo.entrySet().stream()
89+
.filter(entry -> entry.getValue().getModuleType() == ModuleType.CHECK)
90+
.forEach(check -> {
91+
final ModuleDetails moduleDetails = check.getValue();
9192
final SonarRulePropertyLoader.AdditionalRuleProperties additionalDetails =
92-
additionalRuleData.get(checkKey);
93+
additionalRuleData.get(check.getKey());
9394
final RulesDefinition.NewRule rule =
9495
repository.createRule(moduleDetails.getFullQualifiedName());
9596
rule.setHtmlDescription(moduleDetails.getDescription())
@@ -105,7 +106,7 @@ public void createRulesWithMetadata() {
105106
if (tag != null) {
106107
rule.setTags(tag);
107108
}
108-
if (isTemplateRule(moduleDetails.getFullQualifiedName())) {
109+
if (isTemplateRule(moduleDetails)) {
109110
rule.setTemplate(true);
110111
}
111112

@@ -135,14 +136,14 @@ private Class<?> getClass(String checkName) {
135136
}
136137

137138
/**
138-
* Determine whether the check is a template rule based on the method types of the check.
139+
* Determine whether the check is a template rule based on the number of the properties of
140+
* check.
139141
*
140-
* @param checkName check name
142+
* @param moduleDetails module
141143
* @return true if check is a template rule
142144
*/
143-
private boolean isTemplateRule(String checkName) {
144-
return Arrays.stream(getClass(checkName).getMethods())
145-
.anyMatch(CheckstyleMetadata::isSetter);
145+
private static boolean isTemplateRule(ModuleDetails moduleDetails) {
146+
return !moduleDetails.getProperties().isEmpty();
146147
}
147148

148149
/**
@@ -303,17 +304,6 @@ private static String getRuleTag(String checkPackage,
303304
return additionalDetails;
304305
}
305306

306-
/**
307-
* Check if the provided method is a setter.
308-
*
309-
* @param method class method
310-
* @return true if the method is a setter
311-
*/
312-
public static boolean isSetter(Method method) {
313-
return method.getName().startsWith("set")
314-
&& method.getParameterTypes().length == 1;
315-
}
316-
317307
/**
318308
* It converts the name received from ModuleDetails to Sonar rule name format
319309
* e.g. RightCurlyCheck -> Right Curly Check

src/test/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinitionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void test() {
6161
assertThat(repository.language()).isEqualTo("java");
6262

6363
final List<RulesDefinition.Rule> rules = repository.rules();
64-
assertThat(rules).hasSize(189);
64+
assertThat(rules).hasSize(179);
6565

6666
for (RulesDefinition.Rule rule : rules) {
6767
assertThat(rule.key()).isNotNull();

src/test/java/org/sonar/plugins/checkstyle/metadata/CheckstyleMetadataTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ public static void getPreparedRepository() {
4646
final RulesDefinition.Context context = new RulesDefinition.Context();
4747
definition.define(context);
4848
repository = context.repository(CheckstyleConstants.REPOSITORY_KEY);
49-
checkSet = Arrays.asList("com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck",
49+
checkSet = Arrays.asList(
50+
"com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck",
5051
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
5152
"com.puppycrawl.tools.checkstyle.checks.javadoc.NonEmptyAtclauseDescriptionCheck",
5253
"com.puppycrawl.tools.checkstyle.checks.coding.NestedForDepthCheck",
5354
"com.puppycrawl.tools.checkstyle.checks.javadoc.NonEmptyAtclauseDescriptionCheck",
54-
"com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck");
55+
"com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck",
56+
"com.puppycrawl.tools.checkstyle.checks.coding.AvoidInlineConditionalsCheck"
57+
);
5558
metadataRepo = new HashMap<>();
5659
XmlMetaReader.readAllModulesIncludingThirdPartyIfAny()
5760
.forEach(moduleDetails -> {

0 commit comments

Comments
 (0)