Search code examples
groovycodenarc

CodeNarc supress JdbcREsultSetReference seems not working


I have class similar to this one:

import org.springframework.jdbc.core.RowMapper
import java.sql.ResultSet

class DataMapper implements RowMapper<Data> {

    @Override
    @SupressWarnings('JdbcResultSetReference')
    Data mapRow(ResultSet resultSet, int rowNum) throws SQLException {
        // get some values from resultSet and return desired Data
    }
}

This is one time script to migrate some data using groovy, so I want to suppress codenarc rule. In the ruleSet jdbc rules are included and I don't want to disable them, as they scanning all the project.

<ruleset xmlns="http://codenarc.org/ruleset/1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://codenarc.org/ruleset/1.0 
    http://codenarc.org/ruleset-schema.xsd"
    xsi:noNamespaceSchemaLocation="http://codenarc.org/ruleset-schema.xsd">

    <description>Static analysis rule set for Groovy sources</description>

    <!-- not related rules -->
    <ruleset-ref path='rulesets/jdbc.xml>
</ruleset>

I'm running static analysis within junit tests and get this error:

[codenarc] File: com/example/migrate/DataMapper.groovy
[codenarc]     Violation: Rule=JdbcResultSetReference P=2 Line=5 Msg=[Found reference to java.sql.ResultSet] Src=[import java.sql.ResultSet]
[codenarc]     Violation: Rule=JdbcResultSetReference P=2 Line=5 Msg=[Found reference to java.sql.ResultSet] Src=[import java.sql.ResultSet]
[codenarc] [CodeNarc (http://www.codenarc.org) v1.0]
[codenarc] CodeNarc completed: (p1=0; p2=2; p3=0) 5929ms

I tried moving @SupressWarnings to the class, but it still tells me that I'm violating the rule. So the question is: how to make that suppression to work?


Solution

  • Unfortunately those rules look at import statements, and the @SuppressWarnings does not work on those.

    One option is to disable that rule for your Mapper classes: e.g. in your codenarc.properties:

        JdbcResultSetReference.doNotApplyToClassNames = *Mapper
    

    or set that same property on the rule in your ruleset file.