Dear Team,
I'm try to analyze the .exec file with original classes(not instrumented) to get method Coverage. However, the analyzer could not correctly match the classes in .exec files with those in the jarFile in reason that all mc.getLineCounter().getTotalCount() equal to 0.
I have tried a lot to figure out this. It shows that the .exec file is exactly correct (checked by example .java, ExecDump.java). Did I do anything wrong?
There are covData.exec content dumped by ExecDump.java
exec file: D:\SourceCode\Java\backend\receive.exec
CLASS ID HITS/PROBES CLASS NAME
Session "unknownhost-306fec17": Wed Sep 19 17:18:24 CST 2018 - Wed Sep 19 17:18:26 CST 2018
d9d3aa9e33acfe29 7 of 12 com/plytruth/infome/ItemListActivity
7e30ed01fe7c2f4f 1 of 2 com/plytruth/infome/ItemListActivity$1
865c8c6da7532cf7 1 of 2 android/support/v7/appcompat/R$styleable
6e6d9a10c432a97b 1 of 2 android/support/v7/recyclerview/R$styleable
c747e27c42ca67a4 1 of 2 android/support/design/R$styleable
2c17e4e4f51ca42d 2 of 8 com/plytruth/infome/ItemListActivity$SimpleItemRecyclerViewAdapter
165abed0f8f80308 1 of 4 com/plytruth/infome/ItemListActivity$SimpleItemRecyclerViewAdapter$1
263a2ab51e1894f7 1 of 7 com/plytruth/infome/dummy/DummyContent
My code is:
/*******************************************************************************
* Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.coverage;
import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.IMethodCoverage;
import org.jacoco.core.data.ExecutionDataReader;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.data.SessionInfoStore;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
public class CoverageServiceImpl implements CoverageService.Iface{
private static Logger logger = Logger.getLogger(CoverageServiceImpl.class.getSimpleName());
private static Map<String, HashMap<String, Boolean>> appCovRecord = new HashMap<>();
public static void main(String[] args) {
try {
InputStream fis = new FileInputStream(new File("D:\\SourceCode\\Java\\backend\\covData.exec"));
CoverageServiceImpl csi = new CoverageServiceImpl();
// Get coverage by analyze origin classes with covData.exec
CovStatus cs = csi.getCoverage("D:\\SourceCode\\Java\\InfoMe\\app\\tools\\origin\\debug.jar", fis, true);
System.out.println(cs.getMCovered());
System.out.println(cs.getMTotal());
} catch (Exception e) {
e.printStackTrace();
}
}
public CovStatus getCoverage(String jarPath, InputStream in, boolean withHistory) {
HashMap<String, Boolean> methodCov = getCovRecord(jarPath, withHistory);
// read covData to execData
final ExecutionDataReader reader = new ExecutionDataReader(in);
final ExecutionDataStore execData = new ExecutionDataStore();
final SessionInfoStore sessionInfos = new SessionInfoStore();
reader.setSessionInfoVisitor(sessionInfos);
reader.setExecutionDataVisitor(execData);
try {
reader.read();
} catch (Exception e) {
e.printStackTrace();
}
// analyze execData with origin classes
final CoverageBuilder coverageBuilder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(execData, coverageBuilder);
File jarFile = new File(jarPath);
try {
analyzer.analyzeAll(jarFile);
} catch (IOException ioe) {
ioe.printStackTrace();
}
// Let's dump some metrics and line coverage information:
int mTotal = 0;
for (final IClassCoverage cc : coverageBuilder.getClasses()) {
for (final IMethodCoverage mc: cc.getMethods()) {
mTotal++;
if (mc.getLineCounter().getCoveredCount()> 0 ) {
methodCov.put(cc.getName()+ "_" + mc.getName(), true);
System.out.println(cc.getName()+ "_" + mc.getName());
}
}
}
return new CovStatus(methodCov.size(), mTotal);
}
public HashMap<String, Boolean> getCovRecord(String jarpath, boolean withHistory) {
HashMap<String, Boolean> newRecord = new HashMap<>();
if (withHistory) {
if (appCovRecord.containsKey(jarpath)) {
newRecord = appCovRecord.get(jarpath);
} else {
appCovRecord.put(jarpath, newRecord);
}
}
return newRecord;
}
}
my code, the covData.exec file and the origin jar are appended.