### Question:
DateFormatConstant extends BugChecker implements VariableTreeMatcher { private static Fix threadLocalFix( VariableTree tree, VisitorState state, final VarSymbol sym, SuggestedFix rename) { SuggestedFix.Builder fix = SuggestedFix.builder() .merge(rename) .replace( tree.getType(), String.format("ThreadLocal<%s>", state.getSourceForNode(tree.getType()))) .prefixWith(tree.getInitializer(), "ThreadLocal.withInitial(() -> ") .postfixWith(tree.getInitializer(), ")"); CompilationUnitTree unit = state.getPath().getCompilationUnit(); unit.accept( new TreeScanner<Void, Void>() { @Override public Void visitIdentifier(IdentifierTree tree, Void unused) { if (Objects.equals(ASTHelpers.getSymbol(tree), sym)) { fix.postfixWith(tree, ".get()"); } return null; } }, null); return fix.build(); }  @Override Description matchVariable(VariableTree tree, VisitorState state);  }### Answer:
@Test public void threadLocalFix() { BugCheckerRefactoringTestHelper.newInstance(new DateFormatConstant(), getClass()) .addInputLines( "in/Test.java", "import java.text.SimpleDateFormat;", "import java.text.DateFormat;", "import java.util.Date;", "class Test {", " private static final DateFormat DATE_FORMAT =", " new SimpleDateFormat(\"yyyy-MM-dd HH:mm\");", " static String f(Date d) {", " return DATE_FORMAT.format(d);", " }", "}") .addOutputLines( "out/Test.java", "import java.text.SimpleDateFormat;", "import java.text.DateFormat;", "import java.util.Date;", "class Test {", " private static final ThreadLocal<DateFormat> dateFormat = ", " ThreadLocal.withInitial(() -> new SimpleDateFormat(\"yyyy-MM-dd HH:mm\"));", " static String f(Date d) {", " return dateFormat.get().format(d);", " }", "}") .doTest(); }