ClassRowWriter.java

  1. /*******************************************************************************
  2.  * Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
  3.  * This program and the accompanying materials are made available under
  4.  * the terms of the Eclipse Public License 2.0 which is available at
  5.  * http://www.eclipse.org/legal/epl-2.0
  6.  *
  7.  * SPDX-License-Identifier: EPL-2.0
  8.  *
  9.  * Contributors:
  10.  *    Brock Janiczak - initial API and implementation
  11.  *
  12.  *******************************************************************************/
  13. package org.jacoco.report.csv;

  14. import java.io.IOException;

  15. import org.jacoco.core.analysis.IClassCoverage;
  16. import org.jacoco.core.analysis.ICounter;
  17. import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
  18. import org.jacoco.report.ILanguageNames;

  19. /**
  20.  * Writer for rows in the CVS report representing the summary data of a single
  21.  * class.
  22.  */
  23. class ClassRowWriter {

  24.     private static final CounterEntity[] COUNTERS = { CounterEntity.INSTRUCTION,
  25.             CounterEntity.BRANCH, CounterEntity.LINE, CounterEntity.COMPLEXITY,
  26.             CounterEntity.METHOD };

  27.     private final DelimitedWriter writer;

  28.     private final ILanguageNames languageNames;

  29.     /**
  30.      * Creates a new row writer that writes class information to the given CSV
  31.      * writer.
  32.      *
  33.      * @param writer
  34.      *            writer for csv output
  35.      * @param languageNames
  36.      *            converter for Java identifiers
  37.      * @throws IOException
  38.      *             in case of problems with the writer
  39.      */
  40.     public ClassRowWriter(final DelimitedWriter writer,
  41.             final ILanguageNames languageNames) throws IOException {
  42.         this.writer = writer;
  43.         this.languageNames = languageNames;
  44.         writeHeader();
  45.     }

  46.     private void writeHeader() throws IOException {
  47.         writer.write("GROUP", "PACKAGE", "CLASS");
  48.         for (final CounterEntity entity : COUNTERS) {
  49.             writer.write(entity.name() + "_MISSED");
  50.             writer.write(entity.name() + "_COVERED");
  51.         }
  52.         writer.nextLine();
  53.     }

  54.     /**
  55.      * Writes the class summary information as a row.
  56.      *
  57.      * @param groupName
  58.      *            name of the group
  59.      * @param packageName
  60.      *            vm name of the package
  61.      * @param node
  62.      *            class coverage data
  63.      * @throws IOException
  64.      *             in case of problems with the writer
  65.      */
  66.     public void writeRow(final String groupName, final String packageName,
  67.             final IClassCoverage node) throws IOException {
  68.         writer.write(groupName);
  69.         writer.write(languageNames.getPackageName(packageName));
  70.         final String className = languageNames.getClassName(node.getName(),
  71.                 node.getSignature(), node.getSuperName(),
  72.                 node.getInterfaceNames());
  73.         writer.write(className);

  74.         for (final CounterEntity entity : COUNTERS) {
  75.             final ICounter counter = node.getCounter(entity);
  76.             writer.write(counter.getMissedCount());
  77.             writer.write(counter.getCoveredCount());
  78.         }

  79.         writer.nextLine();
  80.     }

  81. }