diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java index d8fe222e76..173d967ae2 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java @@ -30,7 +30,7 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { public static final String NAME = "codeclimate"; public static final String BODY_PLACEHOLDER = "REPLACE_THIS_WITH_MARKDOWN"; public static final int REMEDIATION_POINTS_DEFAULT = 50000; - public static final String[] CODECLIMATE_DEFAULT_CATEGORIES = new String[]{ "Style" }; + public static final String[] CODECLIMATE_DEFAULT_CATEGORIES = new String[] { "Style" }; // Note: required by https://github.com/codeclimate/spec/blob/master/SPEC.md protected static final String NULL_CHARACTER = "\u0000"; @@ -62,12 +62,14 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { } /** - * Generate a CodeClimateIssue suitable for processing into JSON from the given RuleViolation. + * Generate a CodeClimateIssue suitable for processing into JSON from the + * given RuleViolation. + * * @param rv RuleViolation to convert. * @return The generated issue. */ private CodeClimateIssue asIssue(RuleViolation rv) { - CodeClimateIssue issue = new CodeClimateIssue(); + CodeClimateIssue issue = new CodeClimateIssue(); issue.check_name = rule.getName(); issue.description = cleaned(rv.getDescription()); issue.content = new CodeClimateIssue.Content(BODY_PLACEHOLDER); @@ -75,18 +77,18 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { issue.remediation_points = getRemediationPoints(); issue.categories = getCategories(); - switch(rule.getPriority()) { - case HIGH: - issue.severity = "critical"; - break; - case MEDIUM_HIGH: - case MEDIUM: - case MEDIUM_LOW: - issue.severity = "normal"; - break; - case LOW: - issue.severity = "info"; - break; + switch (rule.getPriority()) { + case HIGH: + issue.severity = "critical"; + break; + case MEDIUM_HIGH: + case MEDIUM: + case MEDIUM_LOW: + issue.severity = "normal"; + break; + case LOW: + issue.severity = "info"; + break; } return issue; @@ -98,45 +100,44 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { } private CodeClimateIssue.Location getLocation(RuleViolation rv) { - CodeClimateIssue.Location result; + CodeClimateIssue.Location result; - String pathWithoutCcRoot = StringUtils.removeStartIgnoreCase(rv.getFilename(), "/code/"); + String pathWithoutCcRoot = StringUtils.removeStartIgnoreCase(rv.getFilename(), "/code/"); - if(rule.hasDescriptor(CODECLIMATE_REMEDIATION_MULTIPLIER) && !rule.getProperty(CODECLIMATE_BLOCK_HIGHLIGHTING)) { - result = new CodeClimateIssue.Location(pathWithoutCcRoot, rv.getBeginLine(), rv.getBeginLine()); - } - else { - result = new CodeClimateIssue.Location(pathWithoutCcRoot, rv.getBeginLine(), rv.getEndLine()); - } + if (rule.hasDescriptor(CODECLIMATE_REMEDIATION_MULTIPLIER) + && !rule.getProperty(CODECLIMATE_BLOCK_HIGHLIGHTING)) { + result = new CodeClimateIssue.Location(pathWithoutCcRoot, rv.getBeginLine(), rv.getBeginLine()); + } else { + result = new CodeClimateIssue.Location(pathWithoutCcRoot, rv.getBeginLine(), rv.getEndLine()); + } - return result; + return result; } private int getRemediationPoints() { - int remediation_points = REMEDIATION_POINTS_DEFAULT; + int remediation_points = REMEDIATION_POINTS_DEFAULT; - if(rule.hasDescriptor(CODECLIMATE_REMEDIATION_MULTIPLIER)) { - remediation_points *= rule.getProperty(CODECLIMATE_REMEDIATION_MULTIPLIER); + if (rule.hasDescriptor(CODECLIMATE_REMEDIATION_MULTIPLIER)) { + remediation_points *= rule.getProperty(CODECLIMATE_REMEDIATION_MULTIPLIER); } - return remediation_points; + return remediation_points; } private String[] getCategories() { - String[] result; + String[] result; - if(rule.hasDescriptor(CODECLIMATE_CATEGORIES)) { + if (rule.hasDescriptor(CODECLIMATE_CATEGORIES)) { Object[] categories = rule.getProperty(CODECLIMATE_CATEGORIES); result = new String[categories.length]; for (int i = 0; i < categories.length; i++) { result[i] = String.valueOf(categories[i]); } - } - else { - result = CODECLIMATE_DEFAULT_CATEGORIES; + } else { + result = CODECLIMATE_DEFAULT_CATEGORIES; } - return result; + return result; } private static String getPmdDeveloperURL() { @@ -148,50 +149,51 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { } private String getBody() { - String result = "## " + rule.getName() + "\\n\\n" + - "Since: PMD " + rule.getSince() + "\\n\\n" + - "Priority: " + rule.getPriority() + "\\n\\n" + - "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): " + Arrays.toString(getCategories()).replaceAll("[\\[\\]]","") + "\\n\\n" + - "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): " + getRemediationPoints() + "\\n\\n" + - cleaned(rule.getDescription()); + String result = "## " + rule.getName() + "\\n\\n" + "Since: PMD " + rule.getSince() + "\\n\\n" + "Priority: " + + rule.getPriority() + "\\n\\n" + + "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): " + + Arrays.toString(getCategories()).replaceAll("[\\[\\]]", "") + "\\n\\n" + + "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): " + + getRemediationPoints() + "\\n\\n" + cleaned(rule.getDescription()); - if(!rule.getExamples().isEmpty()) { - result += "\\n\\n### Example:\\n\\n"; + if (!rule.getExamples().isEmpty()) { + result += "\\n\\n### Example:\\n\\n"; - for(String snippet : rule.getExamples()) { - snippet = snippet.replaceAll("\\n", "\\\\n"); - snippet = snippet.replaceAll("\\t", "\\\\t"); - result += "```java\\n" + snippet + "\\n``` "; - } - } + for (String snippet : rule.getExamples()) { + snippet = snippet.replaceAll("\\n", "\\\\n"); + snippet = snippet.replaceAll("\\t", "\\\\t"); + result += "```java\\n" + snippet + "\\n``` "; + } + } - if(!rule.getPropertyDescriptors().isEmpty()) { - result += "\\n\\n### [PMD properties](" + pmdDeveloperUrl + ")\\n\\n"; - result += "Name | Value | Description\\n"; - result += "--- | --- | ---\\n"; + if (!rule.getPropertyDescriptors().isEmpty()) { + result += "\\n\\n### [PMD properties](" + pmdDeveloperUrl + ")\\n\\n"; + result += "Name | Value | Description\\n"; + result += "--- | --- | ---\\n"; - for(PropertyDescriptor property : rule.getPropertyDescriptors()) { - @SuppressWarnings("unchecked") - PropertyDescriptor typed = (PropertyDescriptor)property; - T value = rule.getProperty(typed); - String propertyValue = typed.asDelimitedString(value); - if (propertyValue == null) propertyValue = ""; - propertyValue = propertyValue.replaceAll("(\n|\r\n|\r)", "\\\\n"); + for (PropertyDescriptor property : rule.getPropertyDescriptors()) { + @SuppressWarnings("unchecked") + PropertyDescriptor typed = (PropertyDescriptor) property; + T value = rule.getProperty(typed); + String propertyValue = typed.asDelimitedString(value); + if (propertyValue == null) + propertyValue = ""; + propertyValue = propertyValue.replaceAll("(\n|\r\n|\r)", "\\\\n"); - String porpertyName = property.name(); - porpertyName = porpertyName.replaceAll("\\_", "\\\\_"); + String porpertyName = property.name(); + porpertyName = porpertyName.replaceAll("\\_", "\\\\_"); - result += porpertyName + " | " + propertyValue + " | " + property.description() + "\\n"; - } - } - return cleaned(result); + result += porpertyName + " | " + propertyValue + " | " + property.description() + "\\n"; + } + } + return cleaned(result); } private String cleaned(String original) { - String result = original.trim(); - result = result.replaceAll("\\s+", " "); - result = result.replaceAll("\\s*[\\r\\n]+\\s*", ""); - result = result.replaceAll("\"","'"); - return result; + String result = original.trim(); + result = result.replaceAll("\\s+", " "); + result = result.replaceAll("\\s*[\\r\\n]+\\s*", ""); + result = result.replaceAll("\"", "'"); + return result; } }