简体   繁体   中英

grails-CodeNarc and Service Stateless ruleSet

I am trying to use codeNarc on a grails project, after installing it and running it I've have some rulesets violations messages that I would like to understand and resolve. The first on concern "GrailsStatelessService" and the second the "equals() and toString()" methods...

For the first one "GrailsStatelessService" the message I received is:

***************************
Violation in class app.TheServiceName. The class is marked as stateless but contains the non-final field 'aVariableName'
***************************

I've searched a little about that but not found a lot of tricks about that. Can someone please explain me what the real meaning of this ruleset and what I have to do to solve this problem/

About the second kind of ruleSet I found somewhere that it solved by overriding those methods in all the domain classes but is hat an obligation, a need, or do I just have to modify the ruleSet File to avoid those kinds of messages related to those rulesets?

And that introduces my last question: where to find this ruleSet File( the default one within codenarc) or the one i must include myself?

I find that the GrailsStatelessService rule does sometimes catch a real violation, so rather than disabling it, I modify it to ignore my commonly used field names.

BuildConfig.groovy:

codenarc.propertiesFile = 'grails-app/conf/codenarc.properties'

codenarc.properties:

GrailsStatelessService.addToIgnoreFieldNames=grailsApplication,applicationContext,sessionFactory

I am able to configure this CodeNarc rules as follows:

  1. Install CodeNarc plugin [ grails install-plugin codenarc ]

  2. Add following line in BuildConfig.groovy file [for details configuration]:

     codenarc.propertiesFile = 'grails-app/conf/codenarc.properties' 
  3. In codenarc.properties - file, I add following rule for ignore few RULEs

     codenarc.properties = { GrailsDomainHasEquals.enabled = false GrailsDomainHasToString.enabled = false EmptyMethod.enabled = false } 
  4. Run following command for generating CodeNarc Report: grails codenarc

Sometimes condenarc mix things ups. Adding Service at the end of the name of the service remove this "issue" if you are not using a class as stateless but codenarc believes so. I had this problem with this Service:

private CurrencyConverterFactory currencyConverterFactory

And I fixed with:

private CurrencyConverterFactory currencyConverterFactoryService

I hope this helps someone.

The documentation does a good job of explaining that rule:

Checks for non-final fields on a Grails service class. Grails service classes are singletons by default, and so they should be reentrant. In most cases, this implies (or at least encourages) that they should be stateless.

This rule ignores final fields (either instance or static). Fields that are static and non-final, however, do cause a violation.

If you are using the Grails CodeNarc plugin , then see the plugin documentation for a list of the CodeNarc rulesets that are included by default. There is also a section on "Configuring The CodeNarc RuleSet File(s)" -- so by all means create your own custom ruleset.

http://www.grails.org/plugin/codenarc/

It is expected that you customize the set of rules appropriate for your team/project. Other than the "basic" rulset, the other provided rulesets all contain rules that may or may not be appropriate for you.

The GrailsDomainHasToString and GrailsDomainHasEquals rules are perfect examples -- many organizations disable those rules.

See the CodeNarc documentation for more information on turning off a rule:

http://codenarc.sourceforge.net/codenarc-configuring-rules.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM