Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ollin/2c3c26f71d2c30e254f14ce73aee3e3b to your computer and use it in GitHub Desktop.
Save ollin/2c3c26f71d2c30e254f14ce73aee3e3b to your computer and use it in GitHub Desktop.
Used IDE refactoring to extract local variable (2 occurences) - java.lang.StackOverflowError
package marsrover;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static java.time.LocalDate.of;
class NotProvableRefactoringExtractLocalVariableAllOccurencesTest {
@Test
void works() {
// given
History history = new History();
history.addBevor(of(2020, 1, 1));
history.addBevor(of(2019, 1, 1));
history.addBevor(of(2018, 1, 1));
// when
LocalDate earliesDate = history.getEarliestDate();
// then
Assert.assertEquals(earliesDate, of(2018, 1, 1));
}
@Test
void after_refactoring_bum() {
// given
History history = new History();
history.addBevorExtractVariableAllOccurences(of(2020, 1, 1));
// when
history.addBevorExtractVariableAllOccurences(of(2019, 1, 1));
// Then
// java.lang.StackOverflowError
}
private static class History {
private PointInHistory actual;
void addBevor(LocalDate bevorLast) {
if (null == this.actual) {
this.actual = new PointInHistory(bevorLast);
}
this.actual.setBevor(new PointInHistory(bevorLast));
}
void addBevorExtractVariableAllOccurences(LocalDate bevorLast) {
PointInHistory actual = new PointInHistory(bevorLast);
if (null == this.actual) {
this.actual = actual;
}
this.actual.setBevor(actual);
}
public LocalDate getEarliestDate() {
if (null == actual) {
return LocalDate.MAX;
}
return this.actual.getEarliestDate();
}
}
private static class PointInHistory {
private final LocalDate date;
private PointInHistory bevor;
public PointInHistory(LocalDate date) {
this.date = date;
}
void setBevor(PointInHistory bevor) {
if (null == this.bevor) {
this.bevor = bevor;
return;
}
this.bevor.setBevor(bevor);
}
public LocalDate getEarliestDate() {
if (null == this.bevor) {
return this.date;
}
return this.bevor.getEarliestDate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment