Search in sources :

Example 1 with MetricOption

use of net.sourceforge.pmd.lang.metrics.MetricOption in project pmd by pmd.

the class CycloMetric method computeFor.

// TODO:cf Cyclo should develop factorized boolean operators to count them
@Override
public double computeFor(final MethodLikeNode node, MetricOptions options) {
    Set<MetricOption> opts = options.getOptions();
    JavaParserDecoratedVisitor visitor = new // TODO decorators are unmaintainable, change that someday
    JavaParserDecoratedVisitor(// TODO decorators are unmaintainable, change that someday
    CycloBaseVisitor.INSTANCE) {

        // stops the visit when stumbling on a lambda or class decl
        @Override
        public Object visit(JavaNode localNode, Object data) {
            // TODO generalize that to other metrics
            return localNode.isFindBoundary() && !localNode.equals(node) ? data : super.visit(localNode, data);
        }
    };
    if (opts.contains(CycloOption.CONSIDER_ASSERT)) {
        visitor.decorateWith(new CycloAssertAwareDecorator());
    }
    if (!opts.contains(CycloOption.IGNORE_BOOLEAN_PATHS)) {
        visitor.decorateWith(new CycloPathAwareDecorator());
    }
    MutableInt cyclo = (MutableInt) node.jjtAccept(visitor, new MutableInt(1));
    return (double) cyclo.getValue();
}
Also used : CycloPathAwareDecorator(net.sourceforge.pmd.lang.java.metrics.impl.visitors.CycloPathAwareDecorator) CycloAssertAwareDecorator(net.sourceforge.pmd.lang.java.metrics.impl.visitors.CycloAssertAwareDecorator) JavaParserDecoratedVisitor(net.sourceforge.pmd.lang.java.ast.JavaParserDecoratedVisitor) MutableInt(org.apache.commons.lang3.mutable.MutableInt) MetricOption(net.sourceforge.pmd.lang.metrics.MetricOption) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Aggregations

JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1 JavaParserDecoratedVisitor (net.sourceforge.pmd.lang.java.ast.JavaParserDecoratedVisitor)1 CycloAssertAwareDecorator (net.sourceforge.pmd.lang.java.metrics.impl.visitors.CycloAssertAwareDecorator)1 CycloPathAwareDecorator (net.sourceforge.pmd.lang.java.metrics.impl.visitors.CycloPathAwareDecorator)1 MetricOption (net.sourceforge.pmd.lang.metrics.MetricOption)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1