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.Map; |
| 18 | import java.util.TreeMap; |
| 19 | import org.jacoco.core.internal.flow.ClassProbesVisitor; |
| 20 | import org.jacoco.core.internal.flow.MethodProbesVisitor; |
| 21 | import org.objectweb.asm.FieldVisitor; |
| 22 | |
| 23 | /** A visitor that maps each source code line to the probes corresponding to the lines. */ |
| 24 | public class ClassProbesMapper extends ClassProbesVisitor { |
| 25 | private Map<Integer, BranchExp> classLineToBranchExp; |
| 26 | |
| 27 | public Map<Integer, BranchExp> result() { |
| 28 | return classLineToBranchExp; |
| 29 | } |
| 30 | |
| 31 | /** Create a new probe mapper object. */ |
| 32 | public ClassProbesMapper() { |
| 33 | classLineToBranchExp = new TreeMap<Integer, BranchExp>(); |
| 34 | } |
| 35 | |
| 36 | /** Returns a visitor for mapping method code. */ |
| 37 | @Override |
| 38 | public MethodProbesVisitor visitMethod( |
| 39 | int access, String name, String desc, String signature, String[] exceptions) { |
| 40 | return new MethodProbesMapper() { |
| 41 | @Override |
| 42 | public void visitEnd() { |
| 43 | super.visitEnd(); |
| 44 | classLineToBranchExp.putAll(result()); |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public FieldVisitor visitField( |
| 51 | int access, String name, String desc, String signature, Object value) { |
| 52 | return super.visitField(access, name, desc, signature, value); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void visitTotalProbeCount(int count) { |
| 57 | // Nothing to do. Maybe perform some sanity checks here. |
| 58 | } |
| 59 | } |