Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

if statement - SonarQube Java rule for checking debug/trace logging level

I'm trying to write a customized java rule to check if the debug/trace log levels are enabled. If the log level check has been forgotten, the rule should report an issue.

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

public class CheckDebugAndTraceLevel {
    
    private static final Log LOG = LogFactory.getLog(CheckDebugAndTraceLevel.class);

    void foo()
    { 
        if(LOG.isDebugEnabled()) 
        { 
            LOG.debug("some debug text.."); 
        } 
        LOG.debug("some debug text.."); // Noncompliant {{ check LOG.debug with an if statement}}
        
        if(LOG.isTraceEnabled()) 
        { 
            LOG.trace("some debug text.."); 
        } 
        LOG.trace("some text.."); // Noncompliant {{ check LOG.trace with an if statement}}
    } 
}

I requested the first token of each statement to get the semantics of each code line partially. Is there any possibility to request the whole SyntaxToken and not only the first and the last one? Right now, my rule looks like this:

package org.sonar.samples.java.checks;

import java.util.List;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.StatementTree;
import org.sonar.plugins.java.api.tree.VariableTree;

@Rule(key = "DebugTraceCheck",
      name = "Checks if debug & trace of the logger is enabled",
      priority = Priority.CRITICAL,
      tags = {"bug"})
public class DebugAndTraceRule extends BaseTreeVisitor implements JavaFileScanner
{
    private JavaFileScannerContext context;
    private boolean logFlag = false; 
    
    @Override
    public void visitMethod(MethodTree tree)
    {
        super.visitMethod(tree);
        String logOption = "";
        List<StatementTree> statements = tree.block().body();
        
        for(StatementTree statement : statements)
        {
            logOption = statement.firstToken().text();
            
            if(((logOption + ".debug").equals("LOG.debug") ||
                    (logOption + ".trace").equals("LOG.trace")) &&
                    logFlag)
            {
                context.reportIssue(this, tree, "debug/trace levels of your logger must be enabled!");
            }
        }
    }

    @Override
    public void visitVariable(VariableTree tree) 
    {
        super.visitVariable(tree);
        if(tree.type().symbolType().toString().equals("Log")) {
            logFlag = true;
        }
    }

    @Override
    public void scanFile(JavaFileScannerContext context) {
        this.context = context;
        scan(context.getTree());
    }
}

But I getting an AssertionError during my JUnit tests. If someone could help me out, I would really appreciate it!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...