Search in sources :

Example 6 with CFLintScanner

use of com.cflint.plugins.CFLintScanner in project CFLint by cflint.

the class ConfigUtils method loadPlugin.

public static CFLintScanner loadPlugin(final PluginInfoRule ruleInfo) {
    final String shortClassName = ruleInfo.getClassName() != null && ruleInfo.getClassName().trim().length() > 0 ? ruleInfo.getClassName() : ruleInfo.getName();
    final String className = PLUGIN_PACKAGE + "." + shortClassName.trim();
    try {
        final Class<?> pluginClass = Class.forName(className);
        final CFLintScanner plugin = (CFLintScanner) pluginClass.newInstance();
        for (final PluginParameter param : ruleInfo.getParameters()) {
            plugin.setParameter(param.getName(), param.getValue());
        }
        ruleInfo.setPluginInstance(plugin);
        return plugin;
    } catch (final Exception e) {
        log.error("Could not load plugin " + className, e);
    }
    return null;
}
Also used : CFLintScanner(com.cflint.plugins.CFLintScanner) PluginParameter(com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginParameter) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 7 with CFLintScanner

use of com.cflint.plugins.CFLintScanner in project CFLint by cflint.

the class CFLint method reportRule.

protected void reportRule(final Element elem, final Object expression, final Context context, final CFLintScanner pluginParm, final ContextMessage msg) {
    final String msgcode = msg.getMessageCode();
    final String nameVar = msg.getVariable();
    final CFLintScanner plugin = msg.getSource() == null ? pluginParm : msg.getSource();
    if (checkForDisabled(elem, msgcode)) {
        return;
    }
    if (configuration == null) {
        throw new NullPointerException("Configuration is null");
    }
    PluginInfoRule ruleInfo;
    if ("PLUGIN_ERROR".equals(msgcode)) {
        ruleInfo = new PluginInfoRule();
        final PluginMessage msgInfo = new PluginMessage("PLUGIN_ERROR");
        msgInfo.setMessageText("Error in plugin: ${variable}");
        msgInfo.setSeverity("ERROR");
        ruleInfo.getMessages().add(msgInfo);
    } else {
        if (plugin == null) {
            throw new NullPointerException("Plugin not set.  Plugin should be using addMessage(messageCode,variable,source) to report messages in parent contexts");
        }
        ruleInfo = configuration.getRuleForPlugin(plugin);
    }
    if (ruleInfo == null) {
        throw new NullPointerException("Rule not found for " + plugin.getClass().getSimpleName());
    }
    final PluginMessage msgInfo = ruleInfo.getMessageByCode(msgcode);
    if (configuration == null) {
        throw new NullPointerException("Message definition not found for [" + msgcode + "] in " + plugin.getClass().getSimpleName());
    }
    final BugInfoBuilder bldr = new BugInfo.BugInfoBuilder().setMessageCode(msgcode).setVariable(nameVar).setFunction(context.getFunctionName()).setFilename(context.getFilename()).setComponent(context.getComponentName());
    if (msgInfo != null) {
        bldr.setSeverity(msgInfo.getSeverity());
        bldr.setMessage(msgInfo.getMessageText());
    } else {
        String errMessage = "Message code: " + msgcode + " is not configured correctly.";
        fireCFLintException(new NullPointerException(errMessage), PLUGIN_ERROR, "", null, null, null, null);
        bldr.setSeverity("WARNING");
        bldr.setMessage(msgcode);
    }
    if (expression instanceof CFStatement) {
        bldr.setExpression(((CFStatement) expression).Decompile(0));
    } else if (expression instanceof CFScriptStatement) {
        bldr.setExpression(((CFScriptStatement) expression).Decompile(0));
    } else if (elem != null) {
        bldr.setExpression(elem.toString());
    }
    bldr.setRuleParameters(ruleInfo.getParameters());
    if (configuration.includes(ruleInfo.getMessageByCode(msgcode)) && !configuration.excludes(ruleInfo.getMessageByCode(msgcode))) {
        if (expression instanceof CFExpression) {
            BugInfo bugInfo = bldr.build((CFExpression) expression, elem);
            final Token token = ((CFExpression) expression).getToken();
            if (!suppressed(bugInfo, token, context)) {
                bugs.add(bugInfo);
            }
        } else {
            final BugInfo bug = bldr.build((CFParsedStatement) expression, elem);
            if (msg.getLine() != null) {
                bug.setLine(msg.getLine());
                bug.setColumn(0);
            }
            bugs.add(bug);
        }
    }
}
Also used : BugInfoBuilder(com.cflint.BugInfo.BugInfoBuilder) CFStatement(cfml.parsing.cfscript.CFStatement) CFScriptStatement(cfml.parsing.cfscript.script.CFScriptStatement) PluginMessage(com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage) CFLintScanner(com.cflint.plugins.CFLintScanner) Token(org.antlr.v4.runtime.Token) PluginInfoRule(com.cflint.config.CFLintPluginInfo.PluginInfoRule) BugInfoBuilder(com.cflint.BugInfo.BugInfoBuilder) CFExpression(cfml.parsing.cfscript.CFExpression)

Aggregations

CFLintScanner (com.cflint.plugins.CFLintScanner)7 IOException (java.io.IOException)6 ParseException (cfml.parsing.reporting.ParseException)5 ContextMessage (com.cflint.plugins.Context.ContextMessage)5 RecognitionException (org.antlr.runtime.RecognitionException)5 CFExpression (cfml.parsing.cfscript.CFExpression)4 CFScriptStatement (cfml.parsing.cfscript.script.CFScriptStatement)3 Context (com.cflint.plugins.Context)3 CFIdentifier (cfml.parsing.cfscript.CFIdentifier)2 CFFuncDeclStatement (cfml.parsing.cfscript.script.CFFuncDeclStatement)2 CFLintStructureListener (com.cflint.plugins.CFLintStructureListener)2 CFAssignmentExpression (cfml.parsing.cfscript.CFAssignmentExpression)1 CFFullVarExpression (cfml.parsing.cfscript.CFFullVarExpression)1 CFFunctionExpression (cfml.parsing.cfscript.CFFunctionExpression)1 CFMember (cfml.parsing.cfscript.CFMember)1 CFStatement (cfml.parsing.cfscript.CFStatement)1 CFTernaryExpression (cfml.parsing.cfscript.CFTernaryExpression)1 CFVarDeclExpression (cfml.parsing.cfscript.CFVarDeclExpression)1 CFCase (cfml.parsing.cfscript.script.CFCase)1 CFCatchStatement (cfml.parsing.cfscript.script.CFCatchStatement)1