Skip to content

Commit

Permalink
Only compress class names with at least 2 package names.
Browse files Browse the repository at this point in the history
This avoid mistaking normal sentences in user added error message as class names. It is also rare and less useful to compress a short class name anyway.

PiperOrigin-RevId: 329401381
  • Loading branch information
java-team-github-bot authored and Guice Team committed Aug 31, 2020
1 parent 77abaf3 commit a689de7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions core/src/com/google/inject/internal/PackageNameCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ final class PackageNameCompressor {
// extra complications with taking apart types like List<Foo> to get the inner class names.
private static final Pattern CLASSNAME_PATTERN =
// Match lowercase package names with trailing dots. Start with a non-word character so we
// don't match substrings in like Bar.Foo and match the ar.Foo. Start a group to not include
// the non-word character.
Pattern.compile("[\\W](([a-z_0-9]++[.])++"
// Then match a name starting with an uppercase letter. This is the outer class name.
+ "[A-Z][\\w$]++)");
// don't match substrings in like Bar.Foo and match the com.foo.Foo. Require at least 2
// package names to avoid matching non package names like a sentence ending with a period and
// starting with an upper case letter without space, for example:
// foo.Must in message "Invalid value for foo.Must not be empty." should not be compressed.
// Start a group to not include the non-word character.
Pattern.compile(
"[\\W](([a-z_0-9]++[.]){2,}+"
// Then match a name starting with an uppercase letter. This is the outer class name.
+ "[A-Z][\\w$]++)");

/**
* Compresses an error message by stripping the packages out of class names and adding them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ public void testDoesNotCompressSubstringsOfClasses() {
assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(input);
}

@Test
public void testDoesNotCompressShortPackageNames() {
// This shouldn't try to compress the foo.Foo.
String input = "Something is wrong with foo.Foo should not be empty!";
assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(input);
}

@Test
public void testNoClassNamesDoNotPutInLegend() {
String input = "Something is wrong with something!";
Expand Down

0 comments on commit a689de7

Please sign in to comment.