From 079dca595c9ce86be3e4650898f75fe6d63b4673 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 11 Oct 2017 17:42:25 +0300 Subject: [PATCH] cleanup, synchronize fields properly --- .../openapi/util/KeyedExtensionCollector.java | 47 ++++++------------- .../BasePlatformRefactoringAction.java | 5 +- .../intellij/psi/tree/IElementTypeTest.java | 8 ++-- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/platform/core-api/src/com/intellij/openapi/util/KeyedExtensionCollector.java b/platform/core-api/src/com/intellij/openapi/util/KeyedExtensionCollector.java index e71aecde5749..aaa49abd5a66 100644 --- a/platform/core-api/src/com/intellij/openapi/util/KeyedExtensionCollector.java +++ b/platform/core-api/src/com/intellij/openapi/util/KeyedExtensionCollector.java @@ -39,14 +39,14 @@ import java.util.concurrent.ConcurrentMap; public class KeyedExtensionCollector { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.KeyedExtensionCollector"); - private final Map> myExplicitExtensions = new THashMap<>(); + private final Map> myExplicitExtensions = new THashMap<>(); // guarded by lock private final ConcurrentMap> myCache = ContainerUtil.newConcurrentMap(); @NonNls private final String lock; - private ExtensionPoint> myPoint; + private ExtensionPoint> myPoint; // guarded by lock private final String myEpName; - private ExtensionPointAndAreaListener> myListener; + private ExtensionPointAndAreaListener> myListener; // guarded by lock private final List> myListeners = ContainerUtil.createLockFreeCopyOnWriteList(); public KeyedExtensionCollector(@NonNls @NotNull String epName) { @@ -70,11 +70,7 @@ public class KeyedExtensionCollector { public void addExplicitExtension(@NotNull KeyT key, @NotNull T t) { synchronized (lock) { final String skey = keyToString(key); - List list = myExplicitExtensions.get(skey); - if (list == null) { - list = new ArrayList<>(); - myExplicitExtensions.put(skey, list); - } + List list = myExplicitExtensions.computeIfAbsent(skey, __ -> new ArrayList<>()); list.add(t); myCache.remove(skey); for (ExtensionPointListener listener : myListeners) { @@ -109,12 +105,11 @@ public class KeyedExtensionCollector { public List forKey(@NotNull KeyT key) { final String stringKey = keyToString(key); - boolean rebuild = myPoint == null && Extensions.getRootArea().hasExtensionPoint(myEpName); - List cached = rebuild ? null : myCache.get(stringKey); - if (cached != null) return cached; - - cached = buildExtensions(stringKey, key); - cached = ConcurrencyUtil.cacheOrGet(myCache, stringKey, Collections.unmodifiableList(cached)); + List cached = myCache.get(stringKey); + if (cached == null) { + cached = Collections.unmodifiableList(buildExtensions(stringKey, key)); + cached = ConcurrencyUtil.cacheOrGet(myCache, stringKey, cached); + } return cached; } @@ -157,11 +152,7 @@ public class KeyedExtensionCollector { catch (ProcessCanceledException e) { throw e; } - catch (Exception e) { - LOG.error(e); - continue; - } - catch (LinkageError e) { + catch (Exception | LinkageError e) { LOG.error(e); continue; } @@ -178,8 +169,7 @@ public class KeyedExtensionCollector { private ExtensionPoint> getPoint() { ExtensionPoint> point = myPoint; if (point == null && Extensions.getRootArea().hasExtensionPoint(myEpName)) { - ExtensionPointName> typesafe = ExtensionPointName.create(myEpName); - myPoint = point = Extensions.getRootArea().getExtensionPoint(typesafe); + myPoint = point = Extensions.getRootArea().getExtensionPoint(myEpName); myListener = new ExtensionPointAndAreaListener>() { @Override public void extensionAdded(@NotNull final KeyedLazyInstance bean, @Nullable final PluginDescriptor pluginDescriptor) { @@ -228,20 +218,13 @@ public class KeyedExtensionCollector { } } - public void addListener(@NotNull ExtensionPointListener listener) { - myListeners.add(listener); - } public void addListener(@NotNull final ExtensionPointListener listener, @NotNull Disposable parent) { myListeners.add(listener); - Disposer.register(parent, new Disposable() { - @Override - public void dispose() { - myListeners.remove(listener); - } - }); + Disposer.register(parent, () -> myListeners.remove(listener)); } - public void removeListener(@NotNull ExtensionPointListener listener) { - myListeners.remove(listener); + @NotNull + public String getName() { + return myEpName; } } diff --git a/platform/lang-impl/src/com/intellij/refactoring/actions/BasePlatformRefactoringAction.java b/platform/lang-impl/src/com/intellij/refactoring/actions/BasePlatformRefactoringAction.java index d060bbad86bb..f2ae14596929 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/actions/BasePlatformRefactoringAction.java +++ b/platform/lang-impl/src/com/intellij/refactoring/actions/BasePlatformRefactoringAction.java @@ -21,6 +21,7 @@ import com.intellij.lang.refactoring.RefactoringSupportProvider; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.extensions.ExtensionPointListener; import com.intellij.openapi.extensions.PluginDescriptor; @@ -39,7 +40,7 @@ import java.util.List; * @author yole */ public abstract class BasePlatformRefactoringAction extends BaseRefactoringAction { - private Boolean myHidden = null; + private Boolean myHidden; private final Condition myCondition = provider -> getRefactoringHandler(provider) != null; public BasePlatformRefactoringAction() { @@ -53,7 +54,7 @@ public abstract class BasePlatformRefactoringAction extends BaseRefactoringActio public void extensionRemoved(@NotNull RefactoringSupportProvider extension, @Nullable PluginDescriptor pluginDescriptor) { myHidden = null; } - }); + }, ApplicationManager.getApplication()); } @Override diff --git a/platform/platform-tests/testSrc/com/intellij/psi/tree/IElementTypeTest.java b/platform/platform-tests/testSrc/com/intellij/psi/tree/IElementTypeTest.java index c2e9131b8e05..c0e90f60c97e 100644 --- a/platform/platform-tests/testSrc/com/intellij/psi/tree/IElementTypeTest.java +++ b/platform/platform-tests/testSrc/com/intellij/psi/tree/IElementTypeTest.java @@ -17,7 +17,6 @@ package com.intellij.psi.tree; import com.intellij.lang.*; import com.intellij.lexer.Lexer; -import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.ElementManipulators; @@ -45,7 +44,7 @@ public class IElementTypeTest extends LightPlatformCodeInsightFixtureTestCase { public void testCount() { int count = IElementType.getAllocatedTypesCount(); LOG.debug("Preloaded: " + count +" element types"); - LanguageExtensionPoint[] extensions = Extensions.getExtensions(new ExtensionPointName("com.intellij.lang.parserDefinition")); + LanguageExtensionPoint[] extensions = Extensions.getExtensions(LanguageParserDefinitions.INSTANCE.getName(), null); LOG.debug("ParserDefinitions: " + extensions.length); THashMap languageMap = new THashMap<>(); @@ -65,7 +64,7 @@ public class IElementTypeTest extends LightPlatformCodeInsightFixtureTestCase { Lexer lexer = definition.createLexer(getProject()); PsiParser parser = definition.createParser(getProject()); } - catch (UnsupportedOperationException e1) { + catch (UnsupportedOperationException ignored) { } // language-based calculation: per-class-loading stuff commented @@ -120,8 +119,7 @@ public class IElementTypeTest extends LightPlatformCodeInsightFixtureTestCase { } public void testManipulatorRegistered() { - LanguageExtensionPoint[] extensions = - Extensions.getExtensions(new ExtensionPointName("com.intellij.lang.parserDefinition")); + LanguageExtensionPoint[] extensions = Extensions.getExtensions(LanguageParserDefinitions.INSTANCE.getName(), null); Set classes = new HashSet<>(); List failures = new ArrayList<>(); int total = 0;