Skip to content

Commit

Permalink
Updated JavaTimeDefaultTimeZone to avoid NPE'ing when the function it
Browse files Browse the repository at this point in the history
finds was statically imported.

Fixes #1252

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=241514576
  • Loading branch information
nick-someone authored and ronshapiro committed Apr 18, 2019
1 parent af2e2bf commit 33f78de
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
Expand Up @@ -30,7 +30,6 @@
import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


Expand Down Expand Up @@ -105,31 +104,39 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return Description.NO_MATCH; return Description.NO_MATCH;
} }


MethodSymbol method = ASTHelpers.getSymbol(tree);
String replacementMethod = method.name.toString();

// we special case Clock because the replacement isn't just an overload, but a new API entirely
if (CLOCK_MATCHER.matches(tree, state)) {
replacementMethod = "system";
}

String idealReplacementCode = "ZoneId.of(\"America/Los_Angeles\")"; String idealReplacementCode = "ZoneId.of(\"America/Los_Angeles\")";


SuggestedFix.Builder fixBuilder = SuggestedFix.builder(); SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
String zoneIdName = SuggestedFixes.qualifyType(state, fixBuilder, "java.time.ZoneId"); String zoneIdName = SuggestedFixes.qualifyType(state, fixBuilder, "java.time.ZoneId");
String replacementCode = zoneIdName + ".systemDefault()"; String replacementCode = zoneIdName + ".systemDefault()";


fixBuilder.replace( // The method could be statically imported and have no receiver: if so, just swap out the whole
state.getEndPosition(ASTHelpers.getReceiver(tree)), // tree as opposed to surgically replacing the post-receiver part..
state.getEndPosition(tree), ExpressionTree receiver = ASTHelpers.getReceiver(tree);
"." + replacementMethod + "(" + replacementCode + ")"); // we special case Clock because the replacement isn't just an overload, but a new API entirely
boolean systemDefaultZoneClockMethod = CLOCK_MATCHER.matches(tree, state);
String replacementMethod =
systemDefaultZoneClockMethod ? "system" : ASTHelpers.getSymbol(tree).name.toString();
if (receiver != null) {
fixBuilder.replace(
state.getEndPosition(receiver),
state.getEndPosition(tree),
"." + replacementMethod + "(" + replacementCode + ")");
} else {
if (systemDefaultZoneClockMethod) {
fixBuilder.addStaticImport("java.time.Clock.systemDefaultZone");
}
fixBuilder.replace(tree, replacementMethod + "(" + replacementCode + ")");
}


return buildDescription(tree) return buildDescription(tree)
.setMessage( .setMessage(
String.format( String.format(
"%s.%s is not allowed because it silently uses the system default time-zone. You " "%s.%s is not allowed because it silently uses the system default time-zone. You "
+ "must pass an explicit time-zone (e.g., %s) to this method.", + "must pass an explicit time-zone (e.g., %s) to this method.",
method.owner.getSimpleName(), method, idealReplacementCode)) ASTHelpers.getSymbol(tree).owner.getSimpleName(),
ASTHelpers.getSymbol(tree),
idealReplacementCode))
.addFix(fixBuilder.build()) .addFix(fixBuilder.build())
.build(); .build();
} }
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/ */
package com.google.errorprone.bugpatterns.time; package com.google.errorprone.bugpatterns.time;


import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper; import com.google.errorprone.CompilationTestHelper;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
Expand Down Expand Up @@ -42,6 +43,29 @@ public void clock() {
.doTest(); .doTest();
} }


@Test
public void staticImportOfStaticMethod() {
BugCheckerRefactoringTestHelper.newInstance(
new JavaTimeDefaultTimeZone(), JavaTimeDefaultTimeZoneTest.class)
.addInputLines(
"in/TestClass.java",
"import static java.time.LocalDate.now;",
"",
"import java.time.LocalDate;",
"public class TestClass {",
" LocalDate date = now();",
"}")
.addOutputLines(
"out/TestClass.java",
"import static java.time.LocalDate.now;",
"import java.time.LocalDate;",
"import java.time.ZoneId;",
"public class TestClass {",
" LocalDate date = now(ZoneId.systemDefault());",
"}")
.doTest();
}

@Test @Test
public void localDate() { public void localDate() {
helper helper
Expand Down

0 comments on commit 33f78de

Please sign in to comment.