Coverage Report - org.codenarc.rule.design.AbstractClassWithoutAbstractMethodRule
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractClassWithoutAbstractMethodAstVisitor
100%
5/5
100%
24/24
0
AbstractClassWithoutAbstractMethodAstVisitor$_visitClassEx_closure1
100%
1/1
N/A
0
AbstractClassWithoutAbstractMethodRule
100%
1/1
N/A
0
 
 1  
 /*
 2  
  * Copyright 2009 the original author or authors.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.codenarc.rule.design
 17  
 
 18  
 import org.codehaus.groovy.ast.ClassNode
 19  
 import org.codenarc.rule.AbstractAstVisitor
 20  
 import org.codenarc.rule.AbstractAstVisitorRule
 21  
 
 22  
 import java.lang.reflect.Modifier
 23  
 
 24  
 /**
 25  
  * The abstract class does not contain any abstract methods. An abstract class suggests an incomplete implementation,
 26  
  * which is to be completed by subclasses implementing the abstract methods. If the class is intended to be used as a
 27  
  * base class only (not to be instantiated direcly) a protected constructor can be provided prevent direct instantiation.
 28  
  *
 29  
  * @author 'Hamlet D'Arcy'
 30  
  */
 31  15
 class AbstractClassWithoutAbstractMethodRule extends AbstractAstVisitorRule {
 32  
     String name = 'AbstractClassWithoutAbstractMethod'
 33  
     int priority = 2
 34  
     Class astVisitorClass = AbstractClassWithoutAbstractMethodAstVisitor
 35  
 }
 36  
 
 37  1155
 class AbstractClassWithoutAbstractMethodAstVisitor extends AbstractAstVisitor {
 38  
     @Override protected void visitClassEx(ClassNode node) {
 39  
 
 40  1155
         if (!node.isInterface() && Modifier.isAbstract(node.modifiers) && !node.superClass.name.startsWith('Abstract') && !node.superClass.name.startsWith('Base')) {
 41  3
             if (!node.methods.any {  Modifier.isAbstract(it.modifiers)  }) {
 42  1
                 addViolation(node, "The abstract class $node.name contains no abstract methods")
 43  
             }
 44  
         }
 45  1155
         super.visitClassEx(node)
 46  
     }
 47  
 }