-
Notifications
You must be signed in to change notification settings - Fork 349
/
LineMapper.java
56 lines (44 loc) · 1.7 KB
/
LineMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.pitest.coverage.analysis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.pitest.classinfo.ClassByteArraySource;
import org.pitest.classinfo.ClassName;
import org.pitest.coverage.BlockLocation;
import org.pitest.coverage.LineMap;
import java.util.Optional;
import org.pitest.mutationtest.engine.Location;
public class LineMapper implements LineMap {
private final ClassByteArraySource source;
public LineMapper(final ClassByteArraySource source) {
this.source = source;
}
@Override
public Map<BlockLocation, Set<Integer>> mapLines(final ClassName clazz) {
final Map<BlockLocation, Set<Integer>> map = new HashMap<>();
final Optional<byte[]> maybeBytes = this.source.getBytes(clazz.asInternalName());
// classes generated at runtime eg by mocking frameworks
// will be instrumented but not available on the classpath
if (maybeBytes.isPresent()) {
final ClassReader cr = new ClassReader(maybeBytes.get());
final ClassNode classNode = new ClassNode();
cr.accept(classNode, ClassReader.EXPAND_FRAMES);
for (final Object m : classNode.methods) {
final MethodNode mn = (MethodNode) m;
final Location l = Location.location(clazz,
mn.name, mn.desc);
final List<Block> blocks = ControlFlowAnalyser.analyze(mn);
for (int i = 0; i != blocks.size(); i++) {
final Block each = blocks.get(i);
final BlockLocation bl = new BlockLocation(l, i);
map.put(bl, each.getLines());
}
}
}
return map;
}
}