Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaneberly committed Jul 19, 2017
1 parent 721097f commit c86009f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/cflint/BugInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ public BugInfo build(final CFExpression expression, final Element elem) {
return bugInfo;
}

private final String notNull(final String in) {
private final String notNull(final Object in) {
if (in == null) {
return "";
}
return in.trim();
return in.toString().trim();
}

private void doMessageText(final Element elem) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/cflint/config/CFLintPluginInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public PluginMessage getMessageByCode(final String messageCode) {

public static class PluginParameter {
private String name;
private String value;
private Object value;

public String getName() {
return name;
Expand All @@ -230,12 +230,12 @@ public void setName(final String name) {
this.name = name;
}

public String getValue() {
public Object getValue() {
return value;
}

@XmlAttribute(name = "value")
public void setValue(final String value) {
public void setValue(final Object value) {
this.value = value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cflint/plugins/CFLintScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface CFLintScanner {

void expression(CFScriptStatement expression, Context context, BugList bugs);

void setParameter(String name, String value);
void setParameter(String name, Object value);

void startFile(String fileName, BugList bugs);

Expand Down
31 changes: 24 additions & 7 deletions src/main/java/com/cflint/plugins/CFLintScannerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class CFLintScannerAdapter implements CFLintScanner, CFLintStructureListener {

Map<String, String> params = new HashMap<String, String>();
Map<String, Object> params = new HashMap<String, Object>();

public CFLintScannerAdapter() {
super();
Expand Down Expand Up @@ -51,7 +51,7 @@ public void expression(final CFScriptStatement expression, final Context context
}

@Override
public void setParameter(final String name, final String value) {
public void setParameter(final String name, final Object value) {
if (name != null) {
params.put(name.toLowerCase(), value);
params.put(name, value);
Expand All @@ -69,21 +69,38 @@ public String getParameter(final String name) {
if(propertyForName!=null && propertyForName.trim().length()>0){
return propertyForName;
}
if (name != null) {
return params.get(name.toLowerCase());
if (name != null && params.get(name.toLowerCase()) != null) {
return params.get(name.toLowerCase()).toString();
}

return null;
}
public String getParameterNotNull(final String name) {
if (name != null) {
String retval = params.get(name.toLowerCase());
Object retval = params.get(name.toLowerCase());
if(retval != null){
return retval;
return retval.toString();
}
}
return "";
}
public <E> E getParameter(final String name, final Class<E> clazz) {
final String propertyForName = System.getProperty(getClass().getSimpleName() + "." + name);
if(propertyForName!=null && propertyForName.trim().length()>0){
if(clazz.equals(String.class)){
return (E)propertyForName;
}
if(List.class.isAssignableFrom(clazz)){
return (E)Arrays.asList(propertyForName.split(","));
}
System.err.println("Cannot associate property " + getClass().getSimpleName() + "." + name + " as a " + clazz.getName());
}
if (name != null && params.get(name.toLowerCase()) != null) {
return (E)params.get(name.toLowerCase());
}

return null;
}
/**
* Return parameter split by comma
* @param name
Expand All @@ -97,7 +114,7 @@ public int currentLine(final CFExpression expression, final Context context) {
return expression.getLine() + context.startLine() - 1;
}

public Map<String, String> getParams() {
public Map<String, Object> getParams() {
return params;
}

Expand Down
41 changes: 22 additions & 19 deletions src/main/java/com/cflint/plugins/core/UnusedLocalVarChecker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cflint.plugins.core;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.cflint.BugList;
Expand All @@ -21,7 +22,9 @@ public class UnusedLocalVarChecker extends CFLintScannerAdapter {
// LinkedHashMap is ordered.
protected Map<String, VarInfo> localVariables = new LinkedHashMap<String, VarInfo>();
//protected Map<String, Integer> variableLineNo = new HashMap<String, Integer>();


protected List<String> usedTagAttributes=null;

@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {
if (expression instanceof CFFullVarExpression) {
Expand Down Expand Up @@ -102,26 +105,26 @@ public VarInfo(String name, Boolean used){

@Override
public void element(final Element element, final Context context, final BugList bugs) {
//cfquery/@name usage is enough
if(element.getName()!= null && "cfquery".equals(element.getName().toLowerCase())){
final String name = element.getAttributeValue("name");
if(name!= null && localVariables.containsKey(name.toLowerCase())){
localVariables.put(name.toLowerCase(), new VarInfo(name, true));
}
}
//cfloop/@index usage is enough
if(element.getName()!= null && "cfloop".equals(element.getName().toLowerCase())){
final String name = element.getAttributeValue("index");
if(name!= null && localVariables.containsKey(name.toLowerCase())){
localVariables.put(name.toLowerCase(), new VarInfo(name, true));
try{
for(String tagInfo: usedTagAttributes){
final String parts[] = (tagInfo + "//").split("/");
if(element.getName()!= null && parts[0].equals(element.getName().toLowerCase())){
final String name = element.getAttributeValue(parts[1]);
if(name!= null && localVariables.containsKey(name.toLowerCase())){
localVariables.put(name.toLowerCase(), new VarInfo(name, true));
}
}
}
}catch(Exception e){
System.err.println(e.getMessage() + " in UnusedLocalVarChecker");
}
//cfloop/@item usage is enough
if(element.getName()!= null && "cfloop".equals(element.getName().toLowerCase())){
final String name = element.getAttributeValue("item");
if(name!= null && localVariables.containsKey(name.toLowerCase())){
localVariables.put(name.toLowerCase(), new VarInfo(name, true));
}
}

@Override
public void setParameter(String name, Object value) {
super.setParameter(name, value);
if("UsedTagAttributes".equals(name)){
usedTagAttributes = getParameter("UsedTagAttributes",List.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void element(final Element element, final Context context, final BugList
}

@Override
public void setParameter(String name, String value) {
public void setParameter(String name, Object value) {
super.setParameter(name, value);

populateExclusions();
Expand Down
23 changes: 22 additions & 1 deletion src/main/resources/cflint.definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,28 @@
"messageText": "Local variable ${variable} is not used in function ${function}, consider removing it."
}
],
"parameter": []
"parameter": [
{
"name": "UsedTagAttributes",
"value": ["cfquery/name",
"cfloop/index",
"cfloop/item",
"cfchart/name",
"cfdocument/name",
"cfftp/name",
"cfhtmltopdf/name",
"cfhttp/resultname",
"cfimage/name",
"cfimap/name",
"cfldap/name",
"cfoutput/query",
"cfpdf/name",
"cfreport/name",
"cfsavecontent/name",
"cfstoreproc/result",
"cfxml/variable"]
}
]
},
{
"name": "UnusedArgumentChecker",
Expand Down

0 comments on commit c86009f

Please sign in to comment.