Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaneberly committed Oct 24, 2016
1 parent 36bc9d6 commit beff1ec
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 1 deletion.
117 changes: 117 additions & 0 deletions src/main/java/com/cflint/config/CFLintRuleGroups.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.cflint.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;

//@XmlRootElement(name = "CFLint-Plugin")
@JsonInclude(Include.NON_NULL)
public class CFLintRuleGroups {

List<CFLintRuleGroups.RuleSet> rules = new ArrayList<CFLintRuleGroups.RuleSet>();

public List<CFLintRuleGroups.RuleSet> getRules() {
return rules;
}

@XmlElement(name = "ruleSet")
public void setRules(final List<CFLintRuleGroups.RuleSet> rules) {
this.rules = rules;
}

public CFLintRuleGroups.RuleSet getGroupByRuleName(final String ruleName) {
for (final CFLintRuleGroups.RuleSet ruleSet : rules) {
if (ruleName != null && ruleName.equals(ruleSet.getName())) {
return ruleSet;
}
}
return null;
}

@JsonInclude(Include.NON_NULL)
public static class RuleSet {

String name;
String description;
List<String> rules = new ArrayList<String>();

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public List<String> getRules() {
return rules;
}

@XmlAttribute(name = "name")
public void setName(final String name) {
this.name = name;
}
@XmlAttribute(name = "description")
public void setDescription(final String description) {
this.description = description;
}
@XmlElement(name = "rules")
public void setRules(final List<String> rules) {
this.rules = rules;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final RuleSet other = (RuleSet) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
public String toString(){
try {
return ConfigUtils.marshalJson(this);
} catch (Exception e) {
return "";
}
}
}

public String toString(){
try {
return ConfigUtils.marshalJson(this);
} catch (Exception e) {
return "";
}
}
}
19 changes: 18 additions & 1 deletion src/main/java/com/cflint/config/ConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static Unmarshaller createUnmarshaller() throws JAXBException {
}

protected static synchronized void init() throws JAXBException {
CFLintConfigContext = JAXBContext.newInstance(CFLintPluginInfo.class, CFLintConfig.class);
CFLintConfigContext = JAXBContext.newInstance(CFLintPluginInfo.class, CFLintConfig.class, CFLintRuleGroups.class, CFLintRuleGroups.RuleSet.class);
}

public static String marshal(final Object obj) throws JAXBException {
Expand Down Expand Up @@ -150,6 +150,23 @@ public static HashMap<String, String> loadDescriptions() {

return descriptions;
}

/**
* Load the plugin rule groups
*
* @return CFLintPluginInfo instance of plugin definitions
*/
public static CFLintRuleGroups loadDefaultRuleSets() {
final InputStream jsonInputStream = ConfigUtils.class.getResourceAsStream("/cflint.rulesets.json");
if (jsonInputStream != null) {
try {
return unmarshalJson(jsonInputStream, CFLintRuleGroups.class);
} catch (final IOException e) {
e.printStackTrace(System.err);
}
}
return new CFLintRuleGroups();
}

static final String PLUGIN_PACKAGE = "com.cflint.plugins.core";

Expand Down
121 changes: 121 additions & 0 deletions src/main/resources/cflint.rulesets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"ruleSet": [
{
"name": "BugProne",
"description": "Rules that highlight potential bugs",
"rules": [
"ARG_VAR_CONFLICT",
"NO_DEFAULT_INSIDE_SWITCH",
"NESTED_CFOUTPUT",
"OUTPUT_ATTR",
"MISSING_VAR",
"UNUSED_LOCAL_VARIABLE",
"UNUSED_METHOD_ARGUMENT",
"COMPARE_INSTEAD_OF_ASSIGN"
]
},
{
"name": "Correctness",
"description": "Rules that encourage correctness",
"rules": [
"ARG_TYPE_MISSING",
"ARG_TYPE_ANY",
"ARG_DEFAULT_MISSING",
"ARG_VAR_MIXED",
"QUERYNEW_DATATYPE",
"FUNCTION_TYPE_MISSING",
"FUNCTION_TYPE_ANY",
"FILE_SHOULD_START_WITH_LOWERCASE",
"UNQUOTED_STRUCT_KEY"
]
},
{
"name": "Practice",
"description": "Rules that highlight bad practice",
"rules": [
"GLOBAL_VAR",
"AVOID_USING_CFDUMP_TAG",
"AVOID_USING_CFEXECUTE_TAG",
"AVOID_USING_CFABORT_TAG",
"AVOID_USING_ABORT",
"AVOID_USING_WRITEDUMP",
"AVOID_USING_CFINSERT_TAG",
"AVOID_USING_CFMODULE_TAG",
"AVOID_USING_CFUPDATE_TAG",
"AVOID_USING_CFINCLUDE_TAG",
"AVOID_USING_ISDEBUGMODE",
"AVOID_USING_CREATEOBJECT",
"AVOID_USING_DEBUG_ATTR",
"AVOID_USING_CFSETTING_DEBUG"
]
},
{
"name": "Security",
"description": "Rules that identify security risks",
"rules": [
"QUERYPARAM_REQ",
"CFQUERYPARAM_REQ"
]
},
{
"name": "Complexity",
"description": "Rules that identify areas of complexity",
"rules": [
"COMPLEX_BOOLEAN_CHECK",
"EXCESSIVE_FUNCTIONS",
"EXCESSIVE_FUNCTION_LENGTH",
"EXCESSIVE_COMPONENT_LENGTH",
"EXCESSIVE_ARGUMENTS",
"FUNCTION_TOO_COMPLEX"
]
},
{
"name": "Style",
"description": "Rules encourage good code style and documentation",
"rules": [
"COMPONENT_HINT_MISSING",
"FUNCTION_HINT_MISSING",
"ARG_HINT_MISSING",
"ARG_HINT_MISSING_SCRIPT",
"EXPLICIT_BOOLEAN_CHECK",
"VAR_INVALID_NAME",
"VAR_ALLCAPS_NAME",
"SCOPE_ALLCAPS_NAME",
"VAR_TOO_SHORT",
"VAR_TOO_LONG",
"VAR_TOO_WORDY",
"VAR_IS_TEMPORARY",
"VAR_HAS_PREFIX_OR_POSTFIX",
"ARGUMENT_INVALID_NAME",
"ARGUMENT_ALLCAPS_NAME",
"ARGUMENT_TOO_SHORT",
"ARGUMENT_TOO_LONG",
"ARGUMENT_TOO_WORDY",
"ARGUMENT_IS_TEMPORARY",
"ARGUMENT_HAS_PREFIX_OR_POSTFIX",
"METHOD_INVALID_NAME",
"METHOD_ALLCAPS_NAME",
"METHOD_TOO_SHORT",
"METHOD_TOO_LONG",
"METHOD_TOO_WORDY",
"METHOD_IS_TEMPORARY",
"METHOD_HAS_PREFIX_OR_POSTFIX",
"COMPONENT_INVALID_NAME",
"COMPONENT_ALLCAPS_NAME",
"COMPONENT_TOO_SHORT",
"COMPONENT_TOO_LONG",
"COMPONENT_TOO_WORDY",
"COMPONENT_IS_TEMPORARY",
"COMPONENT_HAS_PREFIX_OR_POSTFIX"
]
},
{
"name": "ModernSyntax",
"description": "Rules encourage modern syntax",
"rules": [
"AVOID_USING_STRUCTNEW",
"AVOID_USING_ARRAYNEW"
]
}
]
}
44 changes: 44 additions & 0 deletions src/test/java/com/cflint/integration/TestCFRuleGroupConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.cflint.integration;

import java.util.ArrayList;

import org.junit.Test;

import com.cflint.config.CFLintPluginInfo;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage;
import com.cflint.config.CFLintRuleGroups;
import com.cflint.config.CFLintRuleGroups.RuleSet;
import com.cflint.config.ConfigUtils;

import junit.framework.Assert;

public class TestCFRuleGroupConfig {

@Test
public void consistencyCheck(){
final CFLintPluginInfo pluginInfo = ConfigUtils.loadDefaultPluginInfo();
final CFLintRuleGroups ruleSets = ConfigUtils.loadDefaultRuleSets();
System.out.println(ruleSets.getRules());
ArrayList<String> allMesgs = new ArrayList<String>();
for(PluginInfoRule rule:pluginInfo.getRules()){
for(PluginMessage message:rule.getMessages()){
allMesgs.add(message.getCode());
}
}
ArrayList<String> groupedMesgs = new ArrayList<String>();
for(RuleSet rule:ruleSets.getRules()){
groupedMesgs.addAll(rule.getRules());
}
ArrayList<String> xgroupedMesgs = new ArrayList<String>(groupedMesgs);
xgroupedMesgs.removeAll(allMesgs);
if(xgroupedMesgs.size() > 0 || allMesgs.size()>0){
System.out.println("-- listed in groups, but not defined in definition ----");
System.out.println(xgroupedMesgs);
allMesgs.removeAll(groupedMesgs);
System.out.println("-- listed in definition, but not defined in groups ----");
System.out.println(allMesgs);
Assert.fail("cflint.definition.json and cflint.rulesets.json should agree");
}
}
}

0 comments on commit beff1ec

Please sign in to comment.