org.jacoco.core.analysis.DataflowAnalyzer.java Source code

Java tutorial

Introduction

Here is the source code for org.jacoco.core.analysis.DataflowAnalyzer.java

Source

/*******************************************************************************
 * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.core.analysis;

import org.jacoco.core.analysis.dua.IDuaCoverageVisitor;
import org.jacoco.core.data.AbstractExecutionDataStore;
import org.jacoco.core.data.ControlFlowExecutionData;
import org.jacoco.core.internal.analysis.StringPool;
import org.jacoco.core.internal.analysis.dua.ClassAnalyzer;
import org.jacoco.core.internal.data.CRC64;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;

/**
 * An {@link DataflowAnalyzer} instance processes a set of Java class files and
 * calculates coverage data for them. For each class file the result is reported
 * to a given {@link ICoverageVisitor} instance. In addition the
 * {@link DataflowAnalyzer} requires a {@link AbstractExecutionDataStore}
 * instance that holds the execution data for the classes to analyze. The
 * {@link DataflowAnalyzer} offers several methods to analyze classes from a
 * variety of sources.
 */
public class DataflowAnalyzer extends AbstractAnalyzer {

    private final AbstractExecutionDataStore executionDataStore;

    private final IDuaCoverageVisitor coverageVisitor;

    private final StringPool stringPool;

    /**
     * @param executionData
     * @param coverageVisitor
     */
    public DataflowAnalyzer(final AbstractExecutionDataStore executionData,
            final IDuaCoverageVisitor coverageVisitor) {
        this.executionDataStore = executionData;
        this.coverageVisitor = coverageVisitor;
        this.stringPool = new StringPool();
    }

    /**
     * Analyzes the class given as a ASM reader.
     * 
     * @param reader
     *            reader with class definitions
     */
    @Override
    public void analyzeClass(final ClassReader reader) {
        final ClassNode cn = new ClassNode(Opcodes.ASM5);
        reader.accept(cn, ClassReader.EXPAND_FRAMES);
        // do not analyze interfaces
        if ((cn.access & Opcodes.ACC_INTERFACE) != 0) {
            return;
        }

        final boolean[] probes = getProbes(CRC64.checksum(reader.b));

        if (probes == null) {
            return;
        }

        final ClassAnalyzer analyzer = new ClassAnalyzer(cn, probes, stringPool);

        analyzer.visit(cn);
        coverageVisitor.visitCoverage(analyzer.getCoverage());
    }

    private boolean[] getProbes(final long classId) {
        final ControlFlowExecutionData executionData = executionDataStore.get(classId);
        if (executionData != null) {
            return executionData.getProbes();
        }
        return null;
    }

}