@Override public Map<BlockLocation, Set<Integer>> mapLines(final ClassName clazz) { final Map<BlockLocation, Set<Integer>> map = new HashMap<>(); final Optional<byte[]> maybeBytes = this.source.fetchClassBytes(clazz); // 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, MethodName.fromString(mn.name), mn.desc); final List<Block> blocks = ControlFlowAnalyser.analyze(mn); for (int i = 0; i != blocks.size(); i++) { final BlockLocation bl = new BlockLocation(l, i); map.put(bl, blocks.get(i).getLines()); } } } return map; }
private Map<BlockLocation, Set<Integer>> analyse(Class<?> clazz) throws ClassNotFoundException { when(this.source.fetchClassBytes(any(ClassName.class))).thenReturn( Optional.ofNullable(ClassUtils.classAsBytes(clazz))); final LineMap testee = new LineMapper(this.source); return testee.mapLines(ClassName.fromClass(clazz)); }
@Test public void shouldAllowClientsToRetrieveBytecode() { this.testee.fetchClassBytes(ClassName.fromString("Foo")); verify(this.repository).querySource(ClassName.fromString("Foo")); }
@Override public Map<BlockLocation, Set<Integer>> mapLines(final ClassName clazz) { final Map<BlockLocation, Set<Integer>> map = new HashMap<>(); final Optional<byte[]> maybeBytes = this.source.fetchClassBytes(clazz); // 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, MethodName.fromString(mn.name), mn.desc); final List<Block> blocks = ControlFlowAnalyser.analyze(mn); for (int i = 0; i != blocks.size(); i++) { final BlockLocation bl = new BlockLocation(l, i); map.put(bl, blocks.get(i).getLines()); } } } return map; }