diff options
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java index c4f95fe22c..e5677d813e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaUtil.java @@ -61,8 +61,13 @@ public final class JavaUtil { } /** - * Find the index of the "java" or "javatests" segment in a Java path fragment - * that precedes the source root. + * Finds the index of the segment in a Java path fragment that precedes the source root. + * Starts from the first "java" or "javatests" or "src" segment. + * If the found item was "src", check if this is followed by "main" or "test" and then "java" + * or "resources" (maven layout). + * If the found item was "src", or "java"/"javatests" at the first segment, check for a nested + * root directory (src, java or javatests). A nested root must be followed by (com|net|org), + * or matching maven structure for nested "src", to be accepted, to avoid false positives. * * @param path a Java source dir or file path * @return the index of the java segment or -1 iff no java segment was found. @@ -71,7 +76,42 @@ public final class JavaUtil { if (path.isAbsolute()) { throw new IllegalArgumentException("path must not be absolute: '" + path + "'"); } - return path.getFirstSegment(ImmutableSet.of("java", "javatests")); + int rootIndex = path.getFirstSegment(ImmutableSet.of("java", "javatests", "src")); + if (rootIndex < 0) { + return rootIndex; + } + final boolean isSrc = "src".equals(path.getSegment(rootIndex)); + int checkMavenIndex = isSrc ? rootIndex : -1; + if (rootIndex == 0 || isSrc) { + // Check for a nested "src" directory. + // Also, to support an existing case, "javatests" within "src". + for (int i = rootIndex + 1, max = path.segmentCount() - 2; i <= max; i++) { + String segment = path.getSegment(i); + if ("src".equals(segment) + || (isSrc && ("javatests".equals(segment) || "java".equals(segment)))) { + String next = path.getSegment(i + 1); + if ("com".equals(next) || "org".equals(next) || "net".equals(next)) { + // Check for common first element of java package, to avoid false positives. + rootIndex = i; + } else if ("src".equals(segment)) { + // Also accept maven style src/(main|test)/(java|resources). + checkMavenIndex = i; + } + break; + } + } + } + // Check for (main|test)/(java|resources) after /src/. + if (checkMavenIndex >= 0 && checkMavenIndex + 2 < path.segmentCount()) { + String next = path.getSegment(checkMavenIndex + 1); + if ("main".equals(next) || "test".equals(next)) { + next = path.getSegment(checkMavenIndex + 2); + if ("java".equals(next) || "resources".equals(next)) { + rootIndex = checkMavenIndex + 2; + } + } + } + return rootIndex; } /** |