Hi, please look the following code and help me solve the question I mentioned in Comment(Line number 85) below:

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.ICounter;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.IRuntime;
import org.jacoco.core.runtime.LoggerRuntime;

public class TestClass {

	public static class TrgClass {
		TrgClass() {

		void calc() {
			int a = 5, b = 20, c = 100, d;
			d = a + b;
			c = c - d;
			System.out.println("c = " + c);

	private InputStream getTargetClass(final String name) {
		final String resource = '/' + name.replace('.', '/') + ".class";
		return getClass().getResourceAsStream(resource);

	private class MemoryClassLoader extends ClassLoader {
		private final Map<String, byte[]> definitions = new HashMap<String, byte[]>();

		public void addDefinition(final String name, final byte[] bytes) {
			definitions.put(name, bytes);

		protected Class<?> loadClass(final String name, final boolean resolve)
				throws ClassNotFoundException {
			final byte[] bytes = definitions.get(name);
			if (bytes != null) {
				return defineClass(name, bytes, 0, bytes.length);
			return super.loadClass(name, resolve);

	private void printCounter(final String unit, final ICounter counter) {
		final Integer missed = Integer.valueOf(counter.getMissedCount());
		final Integer total = Integer.valueOf(counter.getTotalCount());
		System.out.printf("%s of %s %s missed%n", missed, total, unit);

	private String getColor(final int status) {
		switch (status) {
		case ICounter.NOT_COVERED:
			return "red";
		case ICounter.PARTLY_COVERED:
			return "yellow";
		case ICounter.FULLY_COVERED:
			return "green";
		return "";

	private void pereformAnalysis() throws Exception {
		String targetName = TrgClass.class.getName();

		final IRuntime runtime = new LoggerRuntime();
		final Instrumenter instrumenter = new Instrumenter(runtime);
		final byte[] instrumented = instrumenter


		final MemoryClassLoader memClassLoader = new MemoryClassLoader();
		memClassLoader.addDefinition(targetName, instrumented);
		final Class<?> targetClass = memClassLoader.loadClass(targetName);

		 * Question:
		 * Here I want to cast targetClass into inner-class TrgClass
		 * and call calc() funtion.

		final ExecutionDataStore executionDataStore = new ExecutionDataStore();
		runtime.collect(executionDataStore, null, false);

		final CoverageBuilder coverageBuilder = new CoverageBuilder();
		final Analyzer analyzer = new Analyzer(executionDataStore,

		for (final IClassCoverage cc : coverageBuilder.getClasses()) {
			System.out.printf("Coverage of class %s%n", cc.getName());

			printCounter("instructions", cc.getInstructionCounter());
			printCounter("branches", cc.getBranchCounter());
			printCounter("lines", cc.getLineCounter());
			printCounter("methods", cc.getMethodCounter());
			printCounter("complexity", cc.getComplexityCounter());

			for (int i = cc.getFirstLine(); i <= cc.getLastLine(); i++) {
				System.out.printf("Line %s: %s%n", Integer.valueOf(i),


	public static void main(String[] args) throws Exception {
		new TestClass().pereformAnalysis();

Can you make a test program without the third party packages?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.