From 15915a2558f65c1d6a82ffd895f25d22790ec928 Mon Sep 17 00:00:00 2001 From: Christian Dietrich Date: Mon, 12 Jul 2021 17:09:23 +0200 Subject: [PATCH] clear caches on updateInternalState Fixes eclipse/xtext-eclipse#1697 Signed-off-by: Christian Dietrich --- .../eclipse/xtext/resource/XtextResourceTest.java | 8 ++++++++ .../eclipse/xtext/util/OnChangeEvictingCache.java | 14 ++++++++++++-- .../org/eclipse/xtext/resource/XtextResource.java | 14 +++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/org.eclipse.xtext.tests/src/org/eclipse/xtext/resource/XtextResourceTest.java b/org.eclipse.xtext.tests/src/org/eclipse/xtext/resource/XtextResourceTest.java index 81d3210b50..2526ce952b 100755 --- a/org.eclipse.xtext.tests/src/org/eclipse/xtext/resource/XtextResourceTest.java +++ b/org.eclipse.xtext.tests/src/org/eclipse/xtext/resource/XtextResourceTest.java @@ -141,4 +141,12 @@ public void unloadRoot(EObject root) { assertEquals(1, diag.getLength()); } + @Test public void testClearCache() throws Exception { + resource.update(0, 0, simpleModel); + Object cv1 = resource.getCache().get("justatest", resource, ()->new Object()); + resource.update(0, 0, ""); + Object cv2 = resource.getCache().get("justatest", resource, ()->new Object()); + assertNotSame(cv1, cv2); + } + } diff --git a/org.eclipse.xtext.util/src/org/eclipse/xtext/util/OnChangeEvictingCache.java b/org.eclipse.xtext.util/src/org/eclipse/xtext/util/OnChangeEvictingCache.java index 3be096d2d0..08b4216a82 100644 --- a/org.eclipse.xtext.util/src/org/eclipse/xtext/util/OnChangeEvictingCache.java +++ b/org.eclipse.xtext.util/src/org/eclipse/xtext/util/OnChangeEvictingCache.java @@ -55,7 +55,10 @@ public static interface Listener { */ @Override public void clear(Resource resource) { - getOrCreate(resource).clearValues(); + CacheAdapter cacheAdapter = findCacheAdapter(resource); + if (cacheAdapter != null) { + cacheAdapter.clearValues(); + } } /** @@ -108,7 +111,7 @@ protected void cacheHit(CacheAdapter adapter) { * @return the cache adapter for the given resource. Never null. */ public CacheAdapter getOrCreate(Resource resource) { - CacheAdapter adapter = (CacheAdapter) EcoreUtil.getAdapter(resource.eAdapters(), CacheAdapter.class); + CacheAdapter adapter = findCacheAdapter(resource); if (adapter == null) { adapter = createCacheAdapter(); resource.eAdapters().add(adapter); @@ -117,6 +120,13 @@ public CacheAdapter getOrCreate(Resource resource) { return adapter; } + /** + * @since 2.26 + */ + protected CacheAdapter findCacheAdapter(Resource resource) { + return (CacheAdapter) EcoreUtil.getAdapter(resource.eAdapters(), CacheAdapter.class); + } + /** * @since 2.23 */ diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/resource/XtextResource.java b/org.eclipse.xtext/src/org/eclipse/xtext/resource/XtextResource.java index 449cb394b3..96ccfa79f3 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/resource/XtextResource.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/resource/XtextResource.java @@ -43,6 +43,7 @@ import org.eclipse.xtext.service.OperationCanceledManager; import org.eclipse.xtext.util.IResourceScopeCache; import org.eclipse.xtext.util.LazyStringInputStream; +import org.eclipse.xtext.util.OnChangeEvictingCache; import org.eclipse.xtext.util.ReplaceRegion; import org.eclipse.xtext.util.TextRegion; import org.eclipse.xtext.validation.IConcreteSyntaxValidator; @@ -297,6 +298,7 @@ protected void updateInternalState(IParseResult newParseResult) { if (newRootASTElement != null && !containsRootElement(newRootASTElement)) getContents().add(0, newRootASTElement); reattachModificationTracker(newRootASTElement); + clearCache(); clearErrorsAndWarnings(); addSyntaxErrors(); doLinking(); @@ -308,7 +310,17 @@ protected void updateInternalState(IParseResult newParseResult) { boolean containsRootElement(EObject newRootASTElement) { return getContents().contains(newRootASTElement); } - + + /** + * @since 2.26 + */ + protected void clearCache() { + IResourceScopeCache cache = getCache(); + if (cache instanceof OnChangeEvictingCache) { + cache.clear(this); + } + } + protected void clearErrorsAndWarnings() { getWarnings().clear(); getErrors().clear();