咨询Emma生成的coverage.ec文件格式(自定义代码覆盖率报告需求)
coverage.ec File Format Great question! I’ve messed around with Emma’s coverage files before, so let me walk you through what I know about the coverage.ec format—since it’s a binary file, you can’t just open it in a text editor and make sense of it right away.
High-Level Background
coverage.ec is Emma’s compact binary format for storing raw coverage data. It’s designed to be small and fast to write/read, which is why it doesn’t use plaintext. The file is made up of a fixed header followed by a series of typed data records that track coverage for classes, methods, and (if enabled) individual lines of code.
Detailed Breakdown of the Format
Let’s break it down piece by piece:
- File Header
- Starts with a magic string:
EMMA-COV(7 ASCII bytes) — this is how Emma identifies the file as a valid coverage data file. - Next is a 4-byte integer representing the Emma version (e.g., Emma 2.1 uses
0x00000201). - Then a bitmask flag (4 bytes) that tells you what kind of data is included (like whether line-level coverage is present, or if there’s a timestamp included).
- Starts with a magic string:
- Data Records
The rest of the file is a sequence of records, each starting with a 1-byte type code that tells you what kind of record it is. The most common ones are:- Class Record: Marks the start of coverage data for a specific class. Includes the fully qualified class name (UTF-8 encoded string) and a hash of the class file (to ensure it matches the compiled code Emma instrumented).
- Method Record: Tied to a parent class, this includes the method’s signature (UTF-8), its start/end line numbers, and execution counts for each code block within the method.
- Line Coverage Record: Only present if line-level coverage was enabled. Stores execution counts for individual lines, mapped to the corresponding class and method.
How to Parse It for Your Custom Report
You don’t have to reinvent the wheel here—Emma’s own source code has all the parsing logic you need. If you want to build a custom parser, check out these core classes:
com.vladium.emma.data.CoverageDataReader: This is the class Emma uses to readcoverage.ecfiles and parse them into an in-memory data structure.com.vladium.emma.data.CoverageData: This holds the parsed coverage data, with methods to access class, method, and line-level coverage counts.
You can either include Emma’s jar in your project and reuse these classes directly, or use the source code as a reference to build your own parser from scratch.
Quick Debugging Tip
If you want to see the raw coverage data in plaintext to cross-reference with the binary file, use Emma’s built-in report tool to convert it:
java -cp emma.jar com.vladium.emma.report report -r txt -in coverage.ec -out coverage.txt
The generated coverage.txt will show you all the coverage details in human-readable format, which can help you map the binary records to actual coverage data as you build your custom parser.
内容的提问来源于stack exchange,提问作者Feri




