All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlassian.clover.instr.groovy.GroovyUtils Maven / Gradle / Ivy

Go to download

Clover is an award winning code coverage and testing tool for Java and Groovy. It integrates easily with Maven, Ant, Grails, Eclipse and IntelliJ IDEA as well as with continuous integration servers such as Bamboo, Jenkins or Hudson. Note: before Clover 4.0 this artifact was named com.cenqua.clover:clover.

The newest version!
package com.atlassian.clover.instr.groovy;

import com.atlassian.clover.registry.FixedSourceRegion;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ClassHelper;

public class GroovyUtils {
    public static FixedSourceRegion newRegionFor(ASTNode statement) {
        return newRegionFor(statement, false);
    }

    public static FixedSourceRegion newRegionFor(ASTNode statement, boolean evenIfUnreportable) {
        if (isReportable(statement) || evenIfUnreportable) {
            return new FixedSourceRegion(statement.getLineNumber(), statement.getColumnNumber(), statement.getLastLineNumber(), statement.getLastColumnNumber());
        } else {
            return null;
        }
    }

    public static boolean isReportable(ASTNode node) {
        //Enum classes currently have no line/col information but their non-syntetic contents do.
        //We still need to instrument them

        return
            isEnum(node)
            || hasValidSourceRegion(node);
    }

    private static boolean isEnum(ASTNode node) {
        return (node instanceof ClassNode && ((ClassNode)node).isDerivedFrom(ClassHelper.Enum_Type));
    }

    /**
     * Returns true if given AST node looks to have correct source region definition: 
* 1) first/last line/column number is >= 1
* 2) last line >= first line
* 3) in case of single line node, also last column >= first column
* @return boolean - true if source region looks fine, false otherwise */ public static boolean hasValidSourceRegion(ASTNode node) { return node.getLineNumber() >= 1 && node.getColumnNumber() >= 1 && ( node.getLastLineNumber() > node.getLineNumber() || (node.getLastLineNumber() == node.getLineNumber() && node.getLastColumnNumber() >= node.getColumnNumber()) ); } public static boolean isScriptClass(ASTNode node) { return (node instanceof ClassNode && ((ClassNode)node).getSuperClass().getName().equals("groovy.lang.Script")); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy