Yue Gan | af3c412 | 2016-12-05 14:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.testing.coverage; |
| 16 | |
| 17 | import java.util.List; |
| 18 | import java.util.Map; |
| 19 | import java.util.TreeMap; |
| 20 | import org.jacoco.core.analysis.Analyzer; |
Lukacs Berki | 22b79d3 | 2017-01-05 08:59:22 +0000 | [diff] [blame] | 21 | import org.jacoco.core.analysis.IClassCoverage; |
| 22 | import org.jacoco.core.analysis.ICoverageVisitor; |
Yue Gan | af3c412 | 2016-12-05 14:36:02 +0000 | [diff] [blame] | 23 | import org.jacoco.core.data.ExecutionData; |
| 24 | import org.jacoco.core.data.ExecutionDataStore; |
| 25 | import org.jacoco.core.internal.data.CRC64; |
| 26 | import org.jacoco.core.internal.flow.ClassProbesAdapter; |
| 27 | import org.objectweb.asm.ClassReader; |
| 28 | |
| 29 | /** |
| 30 | * Analyzer that process the branch coverage detail information. |
| 31 | * |
| 32 | * <p>Reuse the Analyzer class from Jacoco to avoid duplicating the content detection logic. |
| 33 | * Override the main {@code Analyzer.analyzeClass} method which does the main work. |
| 34 | */ |
| 35 | public class BranchDetailAnalyzer extends Analyzer { |
| 36 | |
| 37 | private final ExecutionDataStore executionData; |
| 38 | private final Map<String, BranchCoverageDetail> branchDetails; |
| 39 | |
| 40 | public BranchDetailAnalyzer(final ExecutionDataStore executionData) { |
Lukacs Berki | 22b79d3 | 2017-01-05 08:59:22 +0000 | [diff] [blame] | 41 | super(executionData, new ICoverageVisitor() { |
| 42 | @Override |
| 43 | public void visitCoverage(IClassCoverage coverage) { |
| 44 | } |
| 45 | }); |
| 46 | |
Yue Gan | af3c412 | 2016-12-05 14:36:02 +0000 | [diff] [blame] | 47 | this.executionData = executionData; |
| 48 | this.branchDetails = new TreeMap<String, BranchCoverageDetail>(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void analyzeClass(final ClassReader reader) { |
| 53 | final Map<Integer, BranchExp> lineToBranchExp = mapProbes(reader); |
| 54 | |
| 55 | long classid = CRC64.checksum(reader.b); |
| 56 | ExecutionData classData = executionData.get(classid); |
| 57 | if (classData == null) { |
| 58 | return; |
| 59 | } |
| 60 | boolean[] probes = classData.getProbes(); |
| 61 | |
| 62 | BranchCoverageDetail detail = new BranchCoverageDetail(); |
| 63 | |
| 64 | for (Map.Entry<Integer, BranchExp> entry : lineToBranchExp.entrySet()) { |
| 65 | int line = entry.getKey(); |
| 66 | BranchExp branchExp = entry.getValue(); |
| 67 | List<CovExp> branches = branchExp.getBranches(); |
| 68 | |
| 69 | detail.setBranches(line, branches.size()); |
| 70 | for (int branchIdx = 0; branchIdx < branches.size(); branchIdx++) { |
| 71 | if (branches.get(branchIdx).eval(probes)) { |
| 72 | detail.setTakenBit(line, branchIdx); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | if (detail.linesWithBranches().size() > 0) { |
| 77 | branchDetails.put(reader.getClassName(), detail); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Generate the line to probeExp map so that we can evaluate the coverage. |
| 82 | private Map<Integer, BranchExp> mapProbes(final ClassReader reader) { |
| 83 | final ClassProbesMapper mapper = new ClassProbesMapper(); |
| 84 | final ClassProbesAdapter adapter = new ClassProbesAdapter(mapper, false); |
| 85 | reader.accept(adapter, 0); |
| 86 | |
| 87 | return mapper.result(); |
| 88 | } |
| 89 | |
| 90 | public Map<String, BranchCoverageDetail> getBranchDetails() { |
| 91 | return branchDetails; |
| 92 | } |
| 93 | } |