Skip to content

Instantly share code, notes, and snippets.

@ctoestreich
Created November 21, 2011 15:50
Show Gist options
  • Save ctoestreich/1383023 to your computer and use it in GitHub Desktop.
Save ctoestreich/1383023 to your computer and use it in GitHub Desktop.
GrailsDomainHasEqualsAstVisitor
/*
* Copyright 2011 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.ClassNode
import org.codenarc.rule.AbstractAstVisitor
import org.codenarc.rule.AbstractAstVisitorRule
import org.codenarc.util.AstUtil
/**
* Checks that Grails domain classes redefine equals().
*
* @author <a href="mailto:geli.crick@osoco.es">Geli Crick</a>
* @author Hamlet D'Arcy
*/
class GrailsDomainHasEqualsRule extends AbstractAstVisitorRule {
String name = 'GrailsDomainHasEquals'
int priority = 2
Class astVisitorClass = GrailsDomainHasEqualsAstVisitor
String applyToFilesMatching = GrailsUtil.DOMAIN_FILES
}
class GrailsDomainHasEqualsAstVisitor extends AbstractAstVisitor {
void visitClassComplete(ClassNode classNode) {
if (isFirstVisit(classNode)) {
if (!AstUtil.getAnnotation(classNode, 'EqualsAndHashCode') && !classNode.methods.any { AstUtil.isMethodNode(it, 'equals', 1) }) {
addViolation(classNode, "The domain class $classNode.name should define an equals(Object) method")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment