Skip to content

Commit

Permalink
Make global cache thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Dec 14, 2017
1 parent f2fb810 commit cae5236
Showing 1 changed file with 9 additions and 6 deletions.
Expand Up @@ -27,6 +27,9 @@
import com.github.javaparser.ast.type.UnknownType; import com.github.javaparser.ast.type.UnknownType;


import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.Map;

import static java.util.Collections.synchronizedMap;


/** /**
* We want to recognize and ignore "phantom" nodes, like the fake type of variable in FieldDeclaration * We want to recognize and ignore "phantom" nodes, like the fake type of variable in FieldDeclaration
Expand All @@ -35,7 +38,7 @@ class PhantomNodeLogic {


private static final int LEVELS_TO_EXPLORE = 3; private static final int LEVELS_TO_EXPLORE = 3;


private static final IdentityHashMap<Node, Boolean> isPhantomNodeCache = new IdentityHashMap<>(); private static final Map<Object, Boolean> isPhantomNodeCache = synchronizedMap(new IdentityHashMap<>());


private static final AstObserver cacheCleaner = new AstObserverAdapter() { private static final AstObserver cacheCleaner = new AstObserverAdapter() {
@Override @Override
Expand All @@ -45,19 +48,19 @@ public void parentChange(Node observedNode, Node previousParent, Node newParent)
}; };


static boolean isPhantomNode(Node node) { static boolean isPhantomNode(Node node) {
boolean res;
if (isPhantomNodeCache.containsKey(node)) { if (isPhantomNodeCache.containsKey(node)) {
res = isPhantomNodeCache.get(node); return isPhantomNodeCache.get(node);
} else { } else {
if (node instanceof UnknownType) { if (node instanceof UnknownType) {
return true; return true;
} }
res = (node.getParentNode().isPresent() && !node.getParentNode().get().getRange().get().contains( boolean res = (node.getParentNode().isPresent() &&
node.getRange().get()) || inPhantomNode(node, LEVELS_TO_EXPLORE)); !node.getParentNode().get().getRange().get().contains(node.getRange().get())
|| inPhantomNode(node, LEVELS_TO_EXPLORE));
isPhantomNodeCache.put(node, res); isPhantomNodeCache.put(node, res);
node.register(cacheCleaner); node.register(cacheCleaner);
return res;
} }
return res;
} }


/** /**
Expand Down

0 comments on commit cae5236

Please sign in to comment.