Skip to content

Commit

Permalink
Correctly look up metadata from checkstyle marker
Browse files Browse the repository at this point in the history
Rule metadata is registered only under the short name of the rule. But
markers created for checkstyle violations use the fully qualified name
of the checker class instead. Register the fully qualified name as
alternative name in the metadata, and the lookup from a stored marker
works as expected.

Fixes checkstyle#397
  • Loading branch information
Bananeweizen authored and Calixte committed Apr 2, 2024
1 parent b948631 commit f3e1590
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,21 @@ private static RuleMetadata createRuleMetadata(ModuleDetails moduleDetails) {
packageTokens[packageTokens.length - 1], MetadataFactory.getDefaultSeverity(),
false, true, true, false, group);
ruleMeta.setDescription(moduleDetails.getDescription());

var altName = moduleDetails.getFullQualifiedName();
registerAlternative(altName, ruleMeta);

moduleDetails.getProperties().forEach(modulePropertyDetails -> ruleMeta.getPropertyMetadata()
.add(createPropertyConfig(moduleDetails, modulePropertyDetails)));

return ruleMeta;
}

private static void registerAlternative(String alternativeName, RuleMetadata ruleMetadata) {
ruleMetadata.addAlternativeName(alternativeName);
sAlternativeNamesMap.put(alternativeName, ruleMetadata);
}

/**
* Create module property config data based on current/default data,
* which are overridden partially with all the metadata fetched from checkstyle.
Expand Down Expand Up @@ -573,12 +582,9 @@ private static void doInitialization() throws CheckstylePluginException {
* @return
*/
private static String groupId(String metadataFile) {
var start = metadataFile.indexOf("checks/") + 7;
var end = metadataFile.lastIndexOf("/");
if (start >= end) {
return "";
}
return metadataFile.substring(start, end);
String res = StringUtils.substringBetween(metadataFile, "/checks/", "/");
res = StringUtils.defaultString(res, metadataFile);
return res;
}

/**
Expand Down Expand Up @@ -767,10 +773,7 @@ private static void processModules(Element groupElement, RuleGroupMetadata group
for (Element altNameEl : moduleEl.elements(XMLTags.ALTERNATIVE_NAME_TAG)) {

String alternativeName = altNameEl.attributeValue(XMLTags.INTERNAL_NAME_TAG);

// register alternative name
sAlternativeNamesMap.put(alternativeName, module);
module.addAlternativeName(alternativeName);
registerAlternative(alternativeName, module);
}

// process message keys
Expand Down

0 comments on commit f3e1590

Please sign in to comment.