Skip to content

Commit

Permalink
perf: MainTest#checkModelIsTree needs less memory (#1596)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky authored and surli committed Oct 13, 2017
1 parent e204d07 commit 088869a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/test/java/spoon/test/main/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,25 @@ protected void exit(CtElement e) {
assertEquals("All parents have to be consistent", 0, inconsistentParents.size());
}



/*
* contract: each element is used only once
* For example this is always true: field.getType() != field.getDeclaringType()
*/
private void checkModelIsTree(CtPackage rootPackage) {
Exception dummyException = new Exception("STACK");
PrinterHelper problems = new PrinterHelper(rootPackage.getFactory().getEnvironment());
Map<CtElement, Exception> allElements = new IdentityHashMap<>();
rootPackage.filterChildren(null).forEach((CtElement ele) -> {
Exception secondStack = new Exception("STACK");
//uncomment this line to get stacktrace of real problem. The dummyException is used to avoid OutOfMemoryException
// Exception secondStack = new Exception("STACK");
Exception secondStack = dummyException;
Exception firstStack = allElements.put(ele, secondStack);
if (firstStack != null) {
if(firstStack == dummyException) {
Assert.fail("The Spoon model is not a tree. The " + ele.getClass().getSimpleName() + ":" + ele.toString() + " is shared");
}
//the element ele was already visited. It means it used on more places
//report the stacktrace of first and second usage, so that place can be found easily
problems.write("The element " + ele.getClass().getSimpleName()).writeln()
Expand Down

0 comments on commit 088869a

Please sign in to comment.