Skip to content

Commit

Permalink
ConstantExpressions: stop bubbling up receiver chains if we hit a sta…
Browse files Browse the repository at this point in the history
…tic member.

This lets us regard, say, Duration.ofSeconds as the same method call as ofSeconds, in a fashion more agnostic to the import.

PiperOrigin-RevId: 441489355
  • Loading branch information
graememorgan authored and Error Prone Team committed Apr 13, 2022
1 parent 0d28558 commit 1636144
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public final String toString() {
return receiver + symbol().getSimpleName();
}
return receiver
+ (symbol().isStatic() ? symbol().owner.getSimpleName() + "." : "")
+ symbol().getSimpleName()
+ arguments().stream().map(Object::toString).collect(joining(", ", "(", ")"));
}
Expand Down Expand Up @@ -350,8 +351,9 @@ public Optional<PureMethodInvocation> symbolizeImmutableExpression(
? getReceiver(tree)
: null;

Symbol symbol = getSymbol(tree);
Optional<ConstantExpression> receiverConstant;
if (receiver == null) {
if (receiver == null || (symbol != null && symbol.isStatic())) {
receiverConstant = Optional.empty();
} else {
receiverConstant = constantExpression(receiver, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,27 @@ public void durationsComparedUsingFactoryMethods() {
.doTest();
}

@Test
public void durationsComparedUsingFactoryMethods_withDifferentImport() {
helper
.addSourceLines(
"Test.java",
"import static java.time.Duration.ofSeconds;",
"import java.time.Duration;",
"class Test {",
" public void test(Duration a, Duration b) {",
" if (a.equals(Duration.ofSeconds(1))) {",
" if (a.equals(Duration.ofSeconds(2))) {}",
" // BUG: Diagnostic contains:",
" if (a.equals(ofSeconds(1))) {}",
" // BUG: Diagnostic contains:",
" if (a.equals(java.time.Duration.ofSeconds(1))) {}",
" }",
" }",
"}")
.doTest();
}

@Test
public void autoValues() {
helper
Expand Down

0 comments on commit 1636144

Please sign in to comment.