Skip to content

Commit

Permalink
Narrow filtering of imports in modules (#26)
Browse files Browse the repository at this point in the history
This ensures that when you have:

module/com/example/Foo
otherpackage/com/example/not_in_module/Bar

module's dependency on Bar isn't filtered out.
  • Loading branch information
illicitonion authored May 17, 2022
1 parent 6e9d408 commit 06df61d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
10 changes: 9 additions & 1 deletion java/gazelle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"sort"
"strings"
"unicode"

"github.com/bazel-contrib/rules_jvm/java/gazelle/javaconfig"
"github.com/bazel-contrib/rules_jvm/java/gazelle/private/java"
Expand Down Expand Up @@ -234,7 +235,14 @@ func (l javaLang) generateModuleRoot(args language.GenerateArgs, cfg *javaconfig
filteredImports := filterImports(allImports, func(i string) bool {
for _, n := range allPackageNames {
if strings.HasPrefix(i, n) {
return false
// Assume the standard java convention of class names starting with upper case
// and package components starting with lower case.
// Without this check, one module with dependencies on a subpackage which _isn't_
// in the module won't be detected.
suffixRunes := []rune(i[len(n):])
if len(suffixRunes) >= 2 && suffixRunes[0] == '.' && unicode.IsUpper(suffixRunes[1]) {
return false
}
}
}
return true
Expand Down
5 changes: 4 additions & 1 deletion java/gazelle/testdata/module-granularity/module1/BUILD.want
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ java_library(
"src/main/java/com/example/hello/Hello.java",
"src/main/java/com/example/hello/world/World.java",
],
_gazelle_imports = ["java.lang.String"],
_gazelle_imports = [
"com.example.hello.notworld.NotWorld",
"java.lang.String",
],
_java_packages = [
"com.example.hello",
"com.example.hello.world",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.hello;

import com.example.hello.world.World;
import com.example.hello.notworld.NotWorld;

public class Hello {
public static void main(String[] args) {
World one = new World();
System.out.printf("Hello, %s!", one.name);
System.out.printf("Hello also, %s!", NotWorld.NOT_WORLD);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.hello.notworld;

public class NotWorld {
public static final String NOT_WORLD = "Not World!";
}

0 comments on commit 06df61d

Please sign in to comment.