From 62f01ea9a0ab77846de5a55d82f7542e02aefe60 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 16 Apr 2018 13:19:44 +0700 Subject: [PATCH] ObjectUtils#sentinel introduced --- .../dataFlow/DataFlowInstructionVisitor.java | 2 +- .../psi/impl/PsiDocumentManagerBase.java | 8 +----- .../psi/impl/source/resolve/ResolveCache.java | 3 ++- .../find/impl/livePreview/LivePreview.java | 7 ++--- .../ExternalToolsCheckinHandlerFactory.java | 3 ++- .../progress/util/BackgroundTaskUtil.java | 9 +++---- .../com/intellij/openapi/util/Computable.java | 3 ++- .../openapi/util/RecursionManager.java | 3 ++- .../src/com/intellij/util/ObjectUtils.java | 26 ++++++++++++++++++- .../containers/IntKeyWeakValueHashMap.java | 2 +- .../containers/WeakKeyIntValueHashMap.java | 2 +- .../streams/resolve/IdentityResolver.java | 3 ++- .../streams/ui/impl/CollectionTree.java | 3 ++- 13 files changed, 48 insertions(+), 26 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInstructionVisitor.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInstructionVisitor.java index ad661c823713..a7253da88774 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInstructionVisitor.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInstructionVisitor.java @@ -21,7 +21,7 @@ import java.util.stream.Stream; final class DataFlowInstructionVisitor extends StandardInstructionVisitor { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.dataFlow.DataFlowInstructionVisitor"); - private static final Object ANY_VALUE = new Object(); + private static final Object ANY_VALUE = ObjectUtils.sentinel("ANY_VALUE"); private final Map, StateInfo> myStateInfos = new LinkedHashMap<>(); private final Set myCCEInstructions = ContainerUtil.newHashSet(); private final Map myFailingCalls = new HashMap<>(); diff --git a/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java b/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java index 232edff6a435..d94d72d2afb8 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java +++ b/platform/core-impl/src/com/intellij/psi/impl/PsiDocumentManagerBase.java @@ -235,13 +235,7 @@ public abstract class PsiDocumentManagerBase extends PsiDocumentManager implemen } private final Map actionsWhenAllDocumentsAreCommitted = new LinkedHashMap<>(); //accessed from EDT only - private static final Object PERFORM_ALWAYS_KEY = new Object() { - @Override - @NonNls - public String toString() { - return "PERFORM_ALWAYS"; - } - }; + private static final Object PERFORM_ALWAYS_KEY = ObjectUtils.sentinel("PERFORM_ALWAYS"); /** * Cancel previously registered action and schedules (new) action to be executed when all documents are committed. diff --git a/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java b/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java index c9381a89e238..16bfcca1046c 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java +++ b/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java @@ -28,6 +28,7 @@ import com.intellij.psi.*; import com.intellij.psi.impl.AnyPsiChangeListener; import com.intellij.psi.impl.PsiManagerImpl; import com.intellij.psi.util.PsiUtilCore; +import com.intellij.util.ObjectUtils; import com.intellij.util.containers.ConcurrentWeakKeySoftValueHashMap; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.messages.MessageBus; @@ -257,7 +258,7 @@ public class ResolveCache { return (incompleteCode ? 0 : 1)*2 + (isPoly ? 0 : 1); } - private static final Object NULL_RESULT = new Object(); + private static final Object NULL_RESULT = ObjectUtils.sentinel("ResolveCache.NULL_RESULT"); private static void cache(@NotNull TRef ref, @NotNull ConcurrentMap map, TResult result) { diff --git a/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreview.java b/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreview.java index 7779e90aa5e4..b8a18b818c5b 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreview.java +++ b/platform/lang-impl/src/com/intellij/find/impl/livePreview/LivePreview.java @@ -28,6 +28,7 @@ import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; import com.intellij.ui.awt.RelativePoint; +import com.intellij.util.ObjectUtils; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.PositionTracker; import org.jetbrains.annotations.NotNull; @@ -42,8 +43,8 @@ import java.util.Set; public class LivePreview implements SearchResults.SearchResultsListener, SelectionListener, DocumentListener { private static final Key IN_SELECTION_KEY = Key.create("LivePreview.IN_SELECTION_KEY"); - private static final Object IN_SELECTION1 = new Object(); - private static final Object IN_SELECTION2 = new Object(); + private static final Object IN_SELECTION1 = ObjectUtils.sentinel("LivePreview.IN_SELECTION1"); + private static final Object IN_SELECTION2 = ObjectUtils.sentinel("LivePreview.IN_SELECTION2"); private static final String EMPTY_STRING_DISPLAY_TEXT = ""; private boolean myListeningSelection = false; @@ -51,7 +52,7 @@ public class LivePreview implements SearchResults.SearchResultsListener, Selecti private boolean myInSmartUpdate = false; private static final Key MARKER_USED = Key.create("LivePreview.MARKER_USED"); - private static final Object YES = new Object(); + private static final Object YES = ObjectUtils.sentinel("LivePreview.YES"); private static final Key SEARCH_MARKER = Key.create("LivePreview.SEARCH_MARKER"); public static PrintStream ourTestOutput; diff --git a/platform/lang-impl/src/com/intellij/tools/ExternalToolsCheckinHandlerFactory.java b/platform/lang-impl/src/com/intellij/tools/ExternalToolsCheckinHandlerFactory.java index 8f5f671ab210..d350c2b57ffd 100644 --- a/platform/lang-impl/src/com/intellij/tools/ExternalToolsCheckinHandlerFactory.java +++ b/platform/lang-impl/src/com/intellij/tools/ExternalToolsCheckinHandlerFactory.java @@ -12,6 +12,7 @@ import com.intellij.openapi.vcs.ui.RefreshableOnComponent; import com.intellij.ui.CollectionComboBoxModel; import com.intellij.ui.ComboboxWithBrowseButton; import com.intellij.ui.ListCellRendererWrapper; +import com.intellij.util.ObjectUtils; import com.intellij.util.SmartList; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; @@ -26,7 +27,7 @@ import java.util.List; * @author lene */ public class ExternalToolsCheckinHandlerFactory extends CheckinHandlerFactory { - public static final Object NONE_TOOL = new Object(); + public static final Object NONE_TOOL = ObjectUtils.sentinel("NONE_TOOL"); @NotNull @Override diff --git a/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java b/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java index 787c18820388..69b4f893af5b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java +++ b/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java @@ -30,10 +30,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.Pair; -import com.intellij.util.Consumer; -import com.intellij.util.Function; -import com.intellij.util.IncorrectOperationException; -import com.intellij.util.PairConsumer; +import com.intellij.util.*; import com.intellij.util.concurrency.AppExecutorUtil; import com.intellij.util.messages.MessageBus; import com.intellij.util.messages.Topic; @@ -318,8 +315,8 @@ public class BackgroundTaskUtil { private static class Helper { - private static final Object INITIAL_STATE = new Object(); - private static final Object SLOW_OPERATION_STATE = new Object(); + private static final Object INITIAL_STATE = ObjectUtils.sentinel("INITIAL_STATE"); + private static final Object SLOW_OPERATION_STATE = ObjectUtils.sentinel("SLOW_OPERATION_STATE"); private final Semaphore mySemaphore = new Semaphore(0); private final AtomicReference myResultRef = new AtomicReference<>(INITIAL_STATE); diff --git a/platform/util/src/com/intellij/openapi/util/Computable.java b/platform/util/src/com/intellij/openapi/util/Computable.java index 090cd7732aca..b6106f04bcc0 100644 --- a/platform/util/src/com/intellij/openapi/util/Computable.java +++ b/platform/util/src/com/intellij/openapi/util/Computable.java @@ -15,6 +15,7 @@ */ package com.intellij.openapi.util; +import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -64,7 +65,7 @@ public interface Computable { */ @Deprecated abstract class NullableCachedComputable implements NullableComputable { - private static final Object NULL_VALUE = new Object(); + private static final Object NULL_VALUE = ObjectUtils.sentinel("NullableCachedComputable.NULL_VALUE"); private Object myValue; @Nullable diff --git a/platform/util/src/com/intellij/openapi/util/RecursionManager.java b/platform/util/src/com/intellij/openapi/util/RecursionManager.java index 83319ad981e1..d2c5e2807d8b 100644 --- a/platform/util/src/com/intellij/openapi/util/RecursionManager.java +++ b/platform/util/src/com/intellij/openapi/util/RecursionManager.java @@ -17,6 +17,7 @@ package com.intellij.openapi.util; import com.intellij.openapi.Disposable; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.util.ObjectUtils; import com.intellij.util.containers.ContainerUtil; import gnu.trove.THashMap; import gnu.trove.THashSet; @@ -49,7 +50,7 @@ import java.util.*; @SuppressWarnings("UtilityClassWithoutPrivateConstructor") public class RecursionManager { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.RecursionManager"); - private static final Object NULL = new Object(); + private static final Object NULL = ObjectUtils.sentinel("RecursionManager.NULL"); private static final ThreadLocal ourStack = new ThreadLocal() { @Override protected CalculationStack initialValue() { diff --git a/platform/util/src/com/intellij/util/ObjectUtils.java b/platform/util/src/com/intellij/util/ObjectUtils.java index 97aa743548d5..f2d00cd6b521 100644 --- a/platform/util/src/com/intellij/util/ObjectUtils.java +++ b/platform/util/src/com/intellij/util/ObjectUtils.java @@ -29,7 +29,20 @@ public class ObjectUtils { private ObjectUtils() { } - public static final Object NULL = new Object(); + public static final Object NULL = sentinel("ObjectUtils.NULL"); + + /** + * Creates a new object which could be used as sentinel value (special value to distinguish from any other object). It does not equal + * to any other object. Usually should be assigned to the static final field. + * + * @param name an object name, returned from {@link #toString()} to simplify the debugging or heap dump analysis + * (guaranteed to be stored as sentinel object field). If sentinel is assigned to the static final field, + * it's recommended to supply that field name (possibly qualified with the class name). + * @return a new sentinel object + */ + public static Object sentinel(final String name) { + return new Sentinel(name); + } @NotNull public static T assertNotNull(@Nullable T t) { @@ -117,4 +130,15 @@ public class ObjectUtils { } return obj; } + + private static class Sentinel { + private final String myName; + + public Sentinel(String name) {myName = name;} + + @Override + public String toString() { + return myName; + } + } } diff --git a/platform/util/src/com/intellij/util/containers/IntKeyWeakValueHashMap.java b/platform/util/src/com/intellij/util/containers/IntKeyWeakValueHashMap.java index bb20975149ca..01542ac16781 100644 --- a/platform/util/src/com/intellij/util/containers/IntKeyWeakValueHashMap.java +++ b/platform/util/src/com/intellij/util/containers/IntKeyWeakValueHashMap.java @@ -124,7 +124,7 @@ class IntKeyWeakValueHashMap implements IntObjectMap { return values().contains(value); } - private static final Object GCED = new Object(); + private static final Object GCED = ObjectUtils.sentinel("GCED"); @NotNull @Override public Iterable> entries() { diff --git a/platform/util/src/com/intellij/util/containers/WeakKeyIntValueHashMap.java b/platform/util/src/com/intellij/util/containers/WeakKeyIntValueHashMap.java index 136761a1bd45..558c173a4165 100644 --- a/platform/util/src/com/intellij/util/containers/WeakKeyIntValueHashMap.java +++ b/platform/util/src/com/intellij/util/containers/WeakKeyIntValueHashMap.java @@ -130,7 +130,7 @@ class WeakKeyIntValueHashMap implements ObjectIntMap { throw RefValueHashMap.pointlessContainsValue(); } - private static final Object GCED = new Object(); + private static final Object GCED = ObjectUtils.sentinel("GCED"); @NotNull @Override public Iterable> entries() { diff --git a/plugins/stream-debugger/src/com/intellij/debugger/streams/resolve/IdentityResolver.java b/plugins/stream-debugger/src/com/intellij/debugger/streams/resolve/IdentityResolver.java index 4bf619a0a599..18fba250d006 100644 --- a/plugins/stream-debugger/src/com/intellij/debugger/streams/resolve/IdentityResolver.java +++ b/plugins/stream-debugger/src/com/intellij/debugger/streams/resolve/IdentityResolver.java @@ -4,6 +4,7 @@ package com.intellij.debugger.streams.resolve; import com.intellij.debugger.streams.trace.TraceElement; import com.intellij.debugger.streams.trace.TraceInfo; import com.intellij.debugger.streams.wrapper.TraceUtil; +import com.intellij.util.ObjectUtils; import one.util.streamex.StreamEx; import org.jetbrains.annotations.NotNull; @@ -17,7 +18,7 @@ import java.util.Map; * @author Vitaliy.Bibaev */ public class IdentityResolver implements ValuesOrderResolver { - private static final Object NULL_MARKER = new Object(); + private static final Object NULL_MARKER = ObjectUtils.sentinel("IdentityResolver.NULL_MARKER"); @NotNull @Override public Result resolve(@NotNull TraceInfo info) { diff --git a/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java b/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java index 91f623015c0c..882a4c59f4fb 100644 --- a/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java +++ b/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java @@ -19,6 +19,7 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.ui.JBColor; import com.intellij.util.EventDispatcher; +import com.intellij.util.ObjectUtils; import com.intellij.util.ui.UIUtil; import com.intellij.xdebugger.frame.*; import com.intellij.xdebugger.impl.actions.XDebuggerActions; @@ -46,7 +47,7 @@ import java.util.stream.Collectors; public class CollectionTree extends XDebuggerTree implements TraceContainer { private static final TreePath[] EMPTY_PATHS = new TreePath[0]; private static final Map COLORS_CACHE = new HashMap<>(); - private static final Object NULL_MARKER = new Object(); + private static final Object NULL_MARKER = ObjectUtils.sentinel("CollectionTree.NULL_MARKER"); private final NodeManagerImpl myNodeManager; private final Project myProject;