Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow JaCoCo to ignore package prefixes #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
JaCoCo Java Code Coverage Library
=================================

# This is a JaCoCo fork to allow package prefixes to be ignored when finding source files

It will produce unexpected results if attempting to generate reports across multiple packages.

See [this comment](https://github.com/jacoco/jacoco/pull/953#issuecomment-540843609) for more details

[![Build Status](https://dev.azure.com/jacoco-org/JaCoCo/_apis/build/status/JaCoCo?branchName=master)](https://dev.azure.com/jacoco-org/JaCoCo/_build/latest?definitionId=1&branchName=master)
[![Maven Central](https://img.shields.io/maven-central/v/org.jacoco/jacoco.svg)](http://search.maven.org/#search|ga|1|g%3Aorg.jacoco)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;

/**
* Abstract base class for {@link ISourceFileLocator} locator implementations
Expand Down Expand Up @@ -45,12 +46,8 @@ protected InputStreamSourceFileLocator(final String encoding,

public Reader getSourceFile(final String packageName, final String fileName)
throws IOException {
final InputStream in;
if (packageName.length() > 0) {
in = getSourceStream(packageName + "/" + fileName);
} else {
in = getSourceStream(fileName);
}
final InputStream in = getSourceStream(packageName.split("/"),
fileName);

if (in == null) {
return null;
Expand All @@ -63,6 +60,19 @@ public Reader getSourceFile(final String packageName, final String fileName)
}
}

private InputStream getSourceStream(String[] packageName, String fileName)
throws IOException {
for (int i = 0; i < packageName.length; i++) {
final String location = String.join("/",
Arrays.copyOfRange(packageName, i, packageName.length));
final InputStream in = getSourceStream(location + "/" + fileName);
if (in != null) {
return in;
}
}
return getSourceStream(fileName);
}

public int getTabWidth() {
return tabWidth;
}
Expand Down