Skip to content

Commit

Permalink
Fix source path calculation for classes in default package (#6783)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog authored Mar 6, 2024
1 parent aeb7876 commit 04f98d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public CoverageInstrumentationFilter(String[] includedPackages, String[] exclude

@Override
public boolean test(String className) {
if (!className.contains("/")) {
// always include classes in default package
return true;
}
for (String excludedPackage : excludedPackages) {
if (className.startsWith(excludedPackage)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.io.IOException;
import java.nio.file.Path;
import javax.annotation.Nullable;

public interface PackageResolver {
/** @return the package path or <code>null</code> if the file is in the default package */
@Nullable
Path getPackage(Path sourceFile) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import javax.annotation.Nullable;

public class PackageResolverImpl implements PackageResolver {

Expand All @@ -26,7 +27,10 @@ public PackageResolverImpl(FileSystem fileSystem) {
* <p>It simply looks for a line, that contains the <code>package</code> keyword and extracts the
* part that goes after it and until the nearest <code>;</code> character, then verifies that the
* extracted part looks plausible by checking the actual file path.
*
* @return the package path or <code>null</code> if the file is in the default package
*/
@Nullable
@Override
public Path getPackage(Path sourceFile) throws IOException {
Path folder = sourceFile.getParent();
Expand Down Expand Up @@ -65,6 +69,6 @@ public Path getPackage(Path sourceFile) throws IOException {
}

// apparently there is no package declaration - class is located in the default package
return fileSystem.getPath("");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ private Path getSourceRoot(Path file) throws IOException {
} else if (!sourceType.isResource()) {
indexingStats.sourceFilesVisited++;
Path packagePath = packageResolver.getPackage(file);
packageTree.add(packagePath);
return getSourceRoot(file, packagePath);
if (packagePath != null) {
packageTree.add(packagePath);
return getSourceRoot(file, packagePath);
} else {
return file.getParent();
}

} else {
indexingStats.resourceFilesVisited++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class PackageResolverImplTest extends Specification {
def packagePath = packageResolver.getPackage(javaFilePath)

then:
packagePath == fileSystem.getPath(expectedPackageName)
packagePath == (expectedPackageName != null ? fileSystem.getPath(expectedPackageName) : null)

where:
path | contents | expectedPackageName
"/root/src/MyClass.java" | CLASS_IN_DEFAULT_PACKAGE | ""
"/root/src/MyClass.java" | CLASS_IN_DEFAULT_PACKAGE | null
"/root/src/foo/bar/MyClass.java" | CLASS_IN_FOO_BAR_PACKAGE | "foo/bar"
"/root/src/foo/bar/MyClass.java" | BLANK_LINES_BEFORE_PACKAGE | "foo/bar"
"/root/src/foo/bar/MyClass.java" | SPACES_BEFORE_PACKAGE | "foo/bar"
Expand Down

0 comments on commit 04f98d5

Please sign in to comment.