org.codenarc.rule.grails.GrailsServletContextReferenceRule.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of CodeNarc Show documentation
Show all versions of CodeNarc Show documentation
The CodeNarc project provides a static analysis tool for Groovy code.
/*
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codenarc.rule.grails
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codenarc.rule.AbstractAstVisitor
import org.codenarc.rule.AbstractAstVisitorRule
/**
* Rule that checks for references to the servletContext object from within Grails controller and
* taglib classes.
*
* This rule is intended as a "governance" rule to enable monitoring and controlling access to the
* servletContext from within application source code. Storing objects in the servletContext may
* inhibit scalability and/or performance and should be carefully considered. Furthermore, access
* to the servletContext is not synchronized, so reading/writing objects from the servletContext must
* be manually synchronized, as described in The Definitive Guide to Grails (2nd edition).
*
* Enabling this rule may make most sense in a team environment where team members exhibit a broad
* range of skill and experience levels. Appropriate servletContext access can be configured as
* exceptions to this rule by configuring either the doNotApplyToFilenames
or
* doNotApplyToFilesMatching
property of the rule.
*
* This rule sets the default value of applyToFilesMatching
to only match files
* under the 'grails-app/controllers' or 'grails-app/taglib' folders. You can override this
* with a different regular expression value if appropriate.
*
* @author Chris Mair
*/
class GrailsServletContextReferenceRule extends AbstractAstVisitorRule {
String name = 'GrailsServletContextReference'
int priority = 2
Class astVisitorClass = GrailsServletContextReferenceAstVisitor
String applyToFilesMatching = GrailsUtil.CONTROLLERS_AND_TAGLIB_FILES
}
class GrailsServletContextReferenceAstVisitor extends AbstractAstVisitor {
@Override
void visitVariableExpression(VariableExpression expression) {
if (isFirstVisit(expression) && expression.variable == 'servletContext') {
addViolation(expression, 'Storing objects in the servletContext can limit scalability')
}
super.visitVariableExpression(expression)
}
}