diff --git a/src/main/java/com/cflint/tools/CFLintFilter.java b/src/main/java/com/cflint/tools/CFLintFilter.java index 4dff42df4..90cee0420 100644 --- a/src/main/java/com/cflint/tools/CFLintFilter.java +++ b/src/main/java/com/cflint/tools/CFLintFilter.java @@ -1,156 +1,156 @@ -package com.cflint.tools; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import com.cflint.BugInfo; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class CFLintFilter { - - private ArrayList> data = null; - private List includeCodes = null; - private List excludeCodes = null; - boolean verbose = false; - - @SuppressWarnings("unchecked") - private CFLintFilter(final String data) throws IOException { - if (data != null && data.trim().length() > 0) { - ObjectMapper mapper = new ObjectMapper(); - this.data = mapper.readValue(data, ArrayList.class); - }else{ - this.data = new ArrayList>(); - } - } - - @Deprecated - public void addFilter(final Map filter){ - data.add(filter); - } - - public void excludeCode(final String ... codes){ - if(codes.length == 0) - return; - excludeCodes = Arrays.asList(codes); - } - - public void includeCode(final String ... codes){ - if(codes.length == 0) - return; - if(includeCodes == null){ - includeCodes = new ArrayList(); - } - includeCodes.addAll(Arrays.asList(codes)); - } - - - public static CFLintFilter createFilter(boolean verbose) throws IOException { - String data = null; - try { - final InputStream is = CFLintFilter.class.getResourceAsStream("/cflintexclude.json"); - if(is == null){ - if(verbose){ - System.out.println("No cflintexclude.json on classpath."); - } - return new CFLintFilter(null); - } - if(verbose){ - final URL url = CFLintFilter.class.getResource("/cflintexclude.json"); - System.out.println("Using exclude file " + url); - } - - final byte b[] = new byte[is.available()]; - is.read(b); - data = new String(b); - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - final CFLintFilter filter = new CFLintFilter(data); - filter.setVerbose(verbose); - if (verbose){ - System.out.println("Exclude rule count : " + filter.data.size()); - } - return filter; - } - - public static CFLintFilter createFilter(final String excludeJSON,boolean verbose) throws IOException { - final CFLintFilter filter = new CFLintFilter(excludeJSON); - filter.setVerbose(verbose); - return filter; - } - public static CFLintFilter createFilter(final String excludeJSON) throws IOException { - final CFLintFilter filter = new CFLintFilter(excludeJSON); - return filter; - } - - public boolean include(final BugInfo bugInfo) { - if (includeCodes != null && !includeCodes.contains(bugInfo.getMessageCode())){ - return false; - } - if (excludeCodes != null && excludeCodes.contains(bugInfo.getMessageCode())){ - return false; - } - if (data != null) { - for (final Map item : data) { - - if (item.containsKey("file")) { - if (!bugInfo.getFilename().matches(item.get("file").toString())) { - continue; - }else if (verbose){ - System.out.println("Exclude matched file " + bugInfo.getFilename()); - } - } - if (item.containsKey("code")) { - if (!bugInfo.getMessageCode().matches(item.get("code").toString())) { - continue; - }else if (verbose){ - System.out.println("Exclude matched message code " + bugInfo.getMessageCode()); - } - } - if (item.containsKey("function")) { - if (bugInfo.getFunction() == null - || !bugInfo.getFunction().matches(item.get("function").toString())) { - continue; - }else if (verbose){ - System.out.println("Exclude matched function name " + bugInfo.getFunction()); - } - } - if (item.containsKey("variable")) { - if (bugInfo.getVariable() == null - || !bugInfo.getVariable().matches(item.get("variable").toString())) { - continue; - }else if (verbose){ - System.out.println("Exclude matched variable name " + bugInfo.getVariable()); - } - } - if (item.containsKey("line")) { - if (bugInfo.getLine() > 0 - || !new Integer(bugInfo.getLine()).toString().matches(item.get("line").toString())) { - continue; - }else if (verbose){ - System.out.println("Exclude matched line " + bugInfo.getLine()); - } - } - if (item.containsKey("expression")) { - if (bugInfo.getExpression() == null - || !bugInfo.getExpression().matches(item.get("expression").toString())) { - continue; - }else if (verbose){ - System.out.println("Exclude matched expression " + bugInfo.getExpression()); - } - } - return false; - } - } - return true; - } - - public void setVerbose(boolean verbose) { - this.verbose = verbose; - } -} +package com.cflint.tools; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import com.cflint.BugInfo; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class CFLintFilter { + + private ArrayList> data = null; + private List includeCodes = null; + private List excludeCodes = null; + boolean verbose = false; + + @SuppressWarnings("unchecked") + private CFLintFilter(final String data) throws IOException { + if (data != null && data.trim().length() > 0) { + ObjectMapper mapper = new ObjectMapper(); + this.data = mapper.readValue(data, ArrayList.class); + }else{ + this.data = new ArrayList>(); + } + } + + @Deprecated + public void addFilter(final Map filter){ + data.add(filter); + } + + public void excludeCode(final String ... codes){ + if(codes.length == 0) + return; + excludeCodes = Arrays.asList(codes); + } + + public void includeCode(final String ... codes){ + if(codes.length == 0) + return; + if(includeCodes == null){ + includeCodes = new ArrayList(); + } + includeCodes.addAll(Arrays.asList(codes)); + } + + + public static CFLintFilter createFilter(boolean verbose) throws IOException { + String data = null; + try { + final InputStream is = CFLintFilter.class.getResourceAsStream("/cflintexclude.json"); + if(is == null){ + if(verbose){ + System.out.println("No cflintexclude.json on classpath."); + } + return new CFLintFilter(null); + } + if(verbose){ + final URL url = CFLintFilter.class.getResource("/cflintexclude.json"); + System.out.println("Using exclude file " + url); + } + + final byte b[] = new byte[is.available()]; + is.read(b); + data = new String(b); + } catch (final IOException ioe) { + ioe.printStackTrace(); + } + final CFLintFilter filter = new CFLintFilter(data); + filter.setVerbose(verbose); + if (verbose){ + System.out.println("Exclude rule count : " + filter.data.size()); + } + return filter; + } + + public static CFLintFilter createFilter(final String excludeJSON,boolean verbose) throws IOException { + final CFLintFilter filter = new CFLintFilter(excludeJSON); + filter.setVerbose(verbose); + return filter; + } + public static CFLintFilter createFilter(final String excludeJSON) throws IOException { + final CFLintFilter filter = new CFLintFilter(excludeJSON); + return filter; + } + + public boolean include(final BugInfo bugInfo) { + if (includeCodes != null && !includeCodes.contains(bugInfo.getMessageCode())){ + return false; + } + if (excludeCodes != null && excludeCodes.contains(bugInfo.getMessageCode())){ + return false; + } + if (data != null) { + for (final Map item : data) { + + if (item.containsKey("file")) { + if (!bugInfo.getFilename().matches(item.get("file").toString())) { + continue; + }else if (verbose){ + System.out.println("Exclude matched file " + bugInfo.getFilename()); + } + } + if (item.containsKey("code")) { + if (!bugInfo.getMessageCode().matches(item.get("code").toString())) { + continue; + }else if (verbose){ + System.out.println("Exclude matched message code " + bugInfo.getMessageCode()); + } + } + if (item.containsKey("function")) { + if (bugInfo.getFunction() == null + || !bugInfo.getFunction().matches(item.get("function").toString())) { + continue; + }else if (verbose){ + System.out.println("Exclude matched function name " + bugInfo.getFunction()); + } + } + if (item.containsKey("variable")) { + if (bugInfo.getVariable() == null + || !bugInfo.getVariable().matches(item.get("variable").toString())) { + continue; + }else if (verbose){ + System.out.println("Exclude matched variable name " + bugInfo.getVariable()); + } + } + if (item.containsKey("line")) { + if (bugInfo.getLine() > 0 + || !new Integer(bugInfo.getLine()).toString().matches(item.get("line").toString())) { + continue; + }else if (verbose){ + System.out.println("Exclude matched line " + bugInfo.getLine()); + } + } + if (item.containsKey("severity")) { + if (bugInfo.getSeverity() == null + || !bugInfo.getSeverity().matches(item.get("severity").toString())) { + continue; + }else if (verbose){ + System.out.println("Exclude matched severity " + bugInfo.getLine()); + } + } + return false; + } + } + return true; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } +} diff --git a/src/test/java/com/cflint/TestCFBugsFilter.java b/src/test/java/com/cflint/TestCFBugsFilter.java index d4d5ff797..42b9b1b26 100644 --- a/src/test/java/com/cflint/TestCFBugsFilter.java +++ b/src/test/java/com/cflint/TestCFBugsFilter.java @@ -29,4 +29,35 @@ public void testInclude1() throws IOException { CFLintFilter filter = CFLintFilter.createFilter("[{\"code\":\"OTH_ERROR\"}]"); assertTrue(filter.include(bugInfo)); } + @Test + public void testIncludeSeverity() throws IOException { + BugInfo bugInfo = new BugInfo.BugInfoBuilder().setFunction("testf").setMessageCode("PARSE_ERROR").setSeverity("INFO").build(); + CFLintFilter filter = CFLintFilter.createFilter("[{\"severity\":\"INFO\"}]"); + assertFalse(filter.include(bugInfo)); + } + @Test + public void testIncludeSeverity2() throws IOException { + BugInfo bugInfo = new BugInfo.BugInfoBuilder().setFunction("testf").setMessageCode("PARSE_ERROR").setSeverity("WARNING").build(); + CFLintFilter filter = CFLintFilter.createFilter("[{\"severity\":\"INFO\"}]"); + assertTrue(filter.include(bugInfo)); + } + + @Test + public void testIncludeCompound() throws IOException { + BugInfo bugInfo = new BugInfo.BugInfoBuilder().setFunction("testf").setMessageCode("PARSE_ERROR").setSeverity("INFO").build(); + CFLintFilter filter = CFLintFilter.createFilter("[{\"severity\":\"INFO\",\"code\":\"OTH_ERROR\"}]"); + assertTrue(filter.include(bugInfo)); + } + @Test + public void testIncludeCompound2() throws IOException { + BugInfo bugInfo = new BugInfo.BugInfoBuilder().setFunction("testf").setMessageCode("PARSE_ERROR").setSeverity("WARNING").build(); + CFLintFilter filter = CFLintFilter.createFilter("[{\"severity\":\"INFO\",\"code\":\"PARSE_ERROR\"}]"); + assertTrue(filter.include(bugInfo)); + } + @Test + public void testIncludeCompound3() throws IOException { + BugInfo bugInfo = new BugInfo.BugInfoBuilder().setFunction("testf").setMessageCode("PARSE_ERROR").setSeverity("WARNING").build(); + CFLintFilter filter = CFLintFilter.createFilter("[{\"severity\":\"WARNING\",\"code\":\"PARSE_ERROR\"}]"); + assertFalse(filter.include(bugInfo)); + } }