IJPL-159596 prefer SynchronizedClearableLazy

GitOrigin-RevId: 0409e7a776832a4f30e02106edc390dbddd8bf52
This commit is contained in:
Vladimir Krivosheev
2024-08-05 21:23:37 +02:00
committed by intellij-monorepo-bot
parent 24f3b86d83
commit 1ed0c32b06
10 changed files with 260 additions and 254 deletions

View File

@@ -132,6 +132,7 @@ f:com.intellij.openapi.extensions.ExtensionPointName$Companion
f:com.intellij.openapi.extensions.ExtensionPointUtil
- s:createKeyedExtensionDisposable(java.lang.Object,com.intellij.openapi.extensions.ExtensionPoint):com.intellij.openapi.Disposable
- s:dropLazyValueOnChange(com.intellij.openapi.util.ClearableLazyValue,com.intellij.openapi.extensions.ExtensionPointName,com.intellij.openapi.Disposable):com.intellij.openapi.util.ClearableLazyValue
- s:dropLazyValueOnChange(com.intellij.util.concurrency.SynchronizedClearableLazy,com.intellij.openapi.extensions.ExtensionPointName,com.intellij.openapi.Disposable):java.util.function.Supplier
f:com.intellij.openapi.extensions.Extensions
- s:findExtension(com.intellij.openapi.extensions.ExtensionPointName,java.lang.Class):java.lang.Object
- s:getArea(com.intellij.openapi.extensions.AreaInstance):com.intellij.openapi.extensions.ExtensionsArea

View File

@@ -7,14 +7,15 @@ import com.intellij.openapi.extensions.impl.ExtensionPointImpl;
import com.intellij.openapi.util.ClearableLazyValue;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.KeyedLazyInstance;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Predicate;
import java.util.function.Supplier;
public final class ExtensionPointUtil {
private ExtensionPointUtil() { }
public static @NotNull <V extends ClearableLazyValue<?>> V dropLazyValueOnChange(@NotNull V lazyValue,
@@ -24,6 +25,13 @@ public final class ExtensionPointUtil {
return lazyValue;
}
public static <T> @NotNull Supplier<T> dropLazyValueOnChange(@NotNull SynchronizedClearableLazy<T> lazyValue,
@NotNull ExtensionPointName<?> extensionPointName,
@Nullable Disposable parentDisposable) {
extensionPointName.addChangeListener(lazyValue::drop, parentDisposable);
return lazyValue;
}
@Internal
public static @NotNull <T> Disposable createExtensionDisposable(@NotNull T extensionObject,
@NotNull ExtensionPointName<T> extensionPointName) {

View File

@@ -4,7 +4,6 @@ package com.intellij.util.gist.storage;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream;
import com.intellij.openapi.util.io.FileUtil;
@@ -14,12 +13,14 @@ import com.intellij.openapi.vfs.VirtualFileWithId;
import com.intellij.openapi.vfs.newvfs.AttributeInputStream;
import com.intellij.openapi.vfs.newvfs.AttributeOutputStream;
import com.intellij.openapi.vfs.newvfs.FileAttribute;
import com.intellij.openapi.vfs.newvfs.persistent.VFSAttributesStorage;
import com.intellij.openapi.vfs.newvfs.persistent.FSRecords;
import com.intellij.openapi.vfs.newvfs.persistent.FSRecordsImpl;
import com.intellij.openapi.vfs.newvfs.persistent.VFSAttributesStorage;
import com.intellij.openapi.vfs.newvfs.persistent.log.VfsLog;
import com.intellij.serviceContainer.AlreadyDisposedException;
import com.intellij.util.SystemProperties;
import com.intellij.util.concurrency.AppExecutorUtil;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import com.intellij.util.containers.CollectionFactory;
import com.intellij.util.containers.FactoryMap;
import com.intellij.util.io.DataExternalizer;
@@ -33,13 +34,11 @@ import org.jetbrains.annotations.VisibleForTesting;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static com.intellij.util.SystemProperties.getIntProperty;
import static com.intellij.util.io.IOUtil.KiB;
import static java.nio.file.StandardOpenOption.*;
import static java.util.concurrent.TimeUnit.MINUTES;
import java.util.function.Supplier;
/**
* Implementation stores small gists (<= {@link #MAX_GIST_SIZE_TO_STORE_IN_ATTRIBUTES} in VFS file attributes,
@@ -54,12 +53,12 @@ public final class GistStorageImpl extends GistStorage {
* Value should be < {@link VFSAttributesStorage#MAX_ATTRIBUTE_VALUE_SIZE}
*/
@VisibleForTesting
public static final int MAX_GIST_SIZE_TO_STORE_IN_ATTRIBUTES = getIntProperty("idea.gist.max-size-to-store-in-attributes", 50 * KiB);
public static final int MAX_GIST_SIZE_TO_STORE_IN_ATTRIBUTES = SystemProperties.getIntProperty("idea.gist.max-size-to-store-in-attributes", 50 * IOUtil.KiB);
private static final String HUGE_GISTS_DIR_NAME = "huge-gists";
/** `{caches}/huge-gists/{FSRecords.createdTimestamp}/' */
private static final NotNullLazyValue<Path> DIR_FOR_HUGE_GISTS = NotNullLazyValue.atomicLazy(() -> {
private static final Supplier<Path> DIR_FOR_HUGE_GISTS = new SynchronizedClearableLazy<>(() -> {
final String vfsStamp = Long.toString(FSRecords.getCreationTimestamp());
Path gistsDir = FSRecords.getCacheDir().resolve(HUGE_GISTS_DIR_NAME + "/" + vfsStamp);
try {
@@ -95,7 +94,7 @@ public final class GistStorageImpl extends GistStorage {
// remove {caches}/huge-gists/{fsrecords-timestamp} dirs there {fsrecords-timestamp} != FSRecords.getCreatedTimestamp()
AppExecutorUtil.getAppScheduledExecutorService().schedule(
GistStorageImpl::cleanupAncientGistsDirs,
1, MINUTES
1, TimeUnit.MINUTES
);
}
@@ -247,7 +246,7 @@ public final class GistStorageImpl extends GistStorage {
//looks like data corruption: if gist value was indeed null, we would have stored it as VALUE_KIND_NULL
throw new IOException("Gist file [" + gistPath + "] doesn't exist -> looks like data corruption?");
}
try (DataInputStream gistStream = new DataInputStream(Files.newInputStream(gistPath, READ))) {
try (DataInputStream gistStream = new DataInputStream(Files.newInputStream(gistPath, StandardOpenOption.READ))) {
return GistData.valid(
externalizer.read(gistStream),
gistRecord.gistStamp
@@ -468,7 +467,7 @@ public final class GistStorageImpl extends GistStorage {
IOUtil.writeUTF(attributeStream, gistFileSuffix);
Path gistPath = dedicatedGistFilePath(file, gistFileSuffix);
try (DataOutputStream gistFileStream = new DataOutputStream(Files.newOutputStream(gistPath, WRITE, CREATE))) {
try (DataOutputStream gistFileStream = new DataOutputStream(Files.newOutputStream(gistPath, StandardOpenOption.WRITE, StandardOpenOption.CREATE))) {
gistFileStream.write(outputStream.getInternalBuffer(), 0, outputStream.size());
}
}

View File

@@ -21869,7 +21869,6 @@ a:com.intellij.remote.ui.CreateRemoteSdkDialog
- com.intellij.remote.ui.RemoteSdkEditorContainer
- pf:myExistingSdks:java.util.Collection
- pf:myProject:com.intellij.openapi.project.Project
- pf:mySdkFactoryProvider:com.intellij.openapi.util.NotNullLazyValue
- <init>(com.intellij.openapi.project.Project,java.util.Collection):V
- <init>(java.awt.Component,java.util.Collection):V
- p:createCenterPanel():javax.swing.JComponent

View File

@@ -12,12 +12,12 @@ import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.remote.RemoteSdkAdditionalData;
import com.intellij.remote.RemoteSdkException;
import com.intellij.remote.RemoteSdkFactoryImpl;
import com.intellij.util.ExceptionUtil;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
@@ -25,13 +25,14 @@ import org.jetbrains.annotations.TestOnly;
import javax.swing.*;
import java.awt.*;
import java.util.Collection;
import java.util.function.Supplier;
public abstract class CreateRemoteSdkDialog<T extends RemoteSdkAdditionalData> extends DialogWrapper implements RemoteSdkEditorContainer {
private static final Logger LOG = Logger.getInstance(CreateRemoteSdkDialog.class);
protected final @Nullable Project myProject;
private CreateRemoteSdkForm<T> myInterpreterForm;
private Sdk mySdk;
protected final NotNullLazyValue<RemoteSdkFactoryImpl<T>> mySdkFactoryProvider = NotNullLazyValue.atomicLazy(this::createRemoteSdkFactory);
private final Supplier<RemoteSdkFactoryImpl<T>> sdkFactoryProvider = new SynchronizedClearableLazy<>(this::createRemoteSdkFactory);
private @Nullable T myOriginalData;
protected final Collection<Sdk> myExistingSdks;
@@ -50,7 +51,7 @@ public abstract class CreateRemoteSdkDialog<T extends RemoteSdkAdditionalData> e
protected abstract @NotNull RemoteSdkFactoryImpl<T> createRemoteSdkFactory();
protected RemoteSdkFactoryImpl<T> getSdkFactory() {
return mySdkFactoryProvider.getValue();
return sdkFactoryProvider.get();
}
private @NotNull CreateRemoteSdkForm<T> getInterpreterForm() {
@@ -103,7 +104,6 @@ public abstract class CreateRemoteSdkDialog<T extends RemoteSdkAdditionalData> e
return getSdkFactory().createRemoteSdk(myProject, data, getInterpreterForm().getSdkName(), myExistingSdks);
}
private @Nullable Sdk saveUnfinished() {
final T data;
try {

View File

@@ -1,10 +1,10 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.util;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import java.io.File;
import java.io.IOException;
@@ -13,21 +13,12 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
public final class MemTester {
private MemTester() { }
/**
* Checks if userspace memtester is supported on this platform and can be launched.
*
* @return true if memtester can be launched, otherwise false
*/
public static boolean isSupported() {
return ourMemTesterSupported.getValue();
}
private static Boolean isRunning = false;
private static final NotNullLazyValue<Boolean> ourMemTesterSupported = NotNullLazyValue.atomicLazy(() -> {
private static final Supplier<Boolean> ourMemTesterSupported = new SynchronizedClearableLazy<>(() -> {
String problem;
if (SystemInfo.isWindows) {
@@ -52,6 +43,17 @@ public final class MemTester {
}
});
private static Boolean isRunning = false;
/**
* Checks if userspace memtester is supported on this platform and can be launched.
*
* @return true if memtester can be launched, otherwise false
*/
public static boolean isSupported() {
return ourMemTesterSupported.get();
}
private static String checkMemTester(String memtesterName) {
Path memtester = PathManager.findBinFile(memtesterName);
return memtester != null && Files.isExecutable(memtester) ? null : "not an executable file: " + memtester;

View File

@@ -1,20 +1,22 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.platform.workspace.jps;
import com.intellij.openapi.util.ClearableLazyValue;
import com.intellij.platform.diagnostic.telemetry.PlatformScopesKt;
import com.intellij.platform.diagnostic.telemetry.helpers.SharedMetrics;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
@ApiStatus.Internal
public final class JpsMetrics extends SharedMetrics {
private static final @NotNull ClearableLazyValue<JpsMetrics> _instance = ClearableLazyValue.createAtomic(() -> new JpsMetrics());
private static final @NotNull Supplier<JpsMetrics> _instance = new SynchronizedClearableLazy<>(() -> new JpsMetrics());
private JpsMetrics() { super(PlatformScopesKt.JPS); }
public static JpsMetrics getInstance() {
return _instance.getValue();
return _instance.get();
}
public static final String jpsSyncSpanName = "jps.sync";

View File

@@ -1,14 +1,13 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.idea.maven.plugins.groovy;
import com.intellij.codeInsight.completion.CompletionUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.NullableLazyValue;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
@@ -24,14 +23,10 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
import static com.intellij.openapi.util.NullableLazyValue.atomicLazyNullable;
public final class MavenGroovyPolyglotPomMemberContributor extends NonCodeMembersContributor {
private static final NotNullLazyValue<Collection<Contributor>> contributors =
NotNullLazyValue.atomicLazy(() -> {
final class MavenGroovyPolyglotPomMemberContributor extends NonCodeMembersContributor {
private static final Supplier<Collection<Contributor>> contributors = new SynchronizedClearableLazy<>(() -> {
List<Contributor> list = new ArrayList<>();
list.add(new Contributor("/maven/dsl/groovy/pom.groovy", ""));
list.add(new Contributor("/maven/dsl/groovy/model.groovy", "project"));
@@ -249,7 +244,7 @@ public final class MavenGroovyPolyglotPomMemberContributor extends NonCodeMember
MultiMap<String, String> leafMap = MultiMap.createLinked();
String key = StringUtil.join(methodCallInfo, "->");
for (Contributor contributor : contributors.getValue()) {
for (Contributor contributor : contributors.get()) {
contributor.populate(place.getProject(), multiMap, leafMap);
}
@@ -272,15 +267,15 @@ public final class MavenGroovyPolyglotPomMemberContributor extends NonCodeMember
}
}
private static class Contributor {
private static final class Contributor {
private final String myClassSourcePath;
private final String[] myPaths;
private final NullableLazyValue<String> myClassSourceValue;
private final Supplier<String> myClassSourceValue;
Contributor(@NotNull String classSourcePath, String... paths) {
myClassSourcePath = classSourcePath;
myPaths = paths;
myClassSourceValue = atomicLazyNullable(() -> {
myClassSourceValue = new SynchronizedClearableLazy<>(() -> {
try (InputStream stream = MavenGroovyPolyglotPomMemberContributor.class.getResourceAsStream(myClassSourcePath)) {
if (stream != null) {
return StreamUtil.readText(new InputStreamReader(stream, StandardCharsets.UTF_8));
@@ -292,7 +287,7 @@ public final class MavenGroovyPolyglotPomMemberContributor extends NonCodeMember
}
public void populate(@NotNull Project project, @NotNull MultiMap<String, String> map, @NotNull MultiMap<String, String> leafMap) {
String myClassSource = myClassSourceValue.getValue();
String myClassSource = myClassSourceValue.get();
if (myClassSource == null) return;
for (String path : myPaths) {

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.lang.html;
import com.intellij.lang.Language;
@@ -7,8 +7,8 @@ import com.intellij.openapi.components.ComponentManager;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.extensions.ExtensionPointUtil;
import com.intellij.openapi.extensions.PluginDescriptor;
import com.intellij.openapi.util.ClearableLazyValue;
import com.intellij.serviceContainer.BaseKeyedLazyInstance;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.annotations.Attribute;
import org.jetbrains.annotations.ApiStatus;
@@ -16,14 +16,13 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
import java.util.function.Supplier;
public class HtmlCompatibleMetaLanguage extends MetaLanguage {
public final class HtmlCompatibleMetaLanguage extends MetaLanguage {
private static final ExtensionPointName<HtmlCompatibleLanguageEP> EP_NAME =
new ExtensionPointName<>("com.intellij.html.compatibleLanguage");
private static final ClearableLazyValue<Set<String>> LANGS = ExtensionPointUtil.dropLazyValueOnChange(
ClearableLazyValue.create(
() -> ContainerUtil.map2Set(EP_NAME.getExtensionList(), e -> e.language)
), EP_NAME, null);
private static final Supplier<Set<String>> LANGS = ExtensionPointUtil.dropLazyValueOnChange(
new SynchronizedClearableLazy<>(() -> ContainerUtil.map2Set(EP_NAME.getExtensionList(), e -> e.language)), EP_NAME, null);
private HtmlCompatibleMetaLanguage() {
super("HtmlCompatible");
@@ -31,7 +30,7 @@ public class HtmlCompatibleMetaLanguage extends MetaLanguage {
@Override
public boolean matchesLanguage(@NotNull Language language) {
Set<String> langs = LANGS.getValue();
Set<String> langs = LANGS.get();
while (language != null) {
if (langs.contains(language.getID())) return true;
language = language.getBaseLanguage();
@@ -40,8 +39,7 @@ public class HtmlCompatibleMetaLanguage extends MetaLanguage {
}
@ApiStatus.Experimental
public static class HtmlCompatibleLanguageEP extends BaseKeyedLazyInstance<String> {
public static final class HtmlCompatibleLanguageEP extends BaseKeyedLazyInstance<String> {
@Attribute("language")
public String language;

View File

@@ -1,20 +1,22 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.xml;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.util.concurrency.SynchronizedClearableLazy;
import com.intellij.util.io.URLUtil;
import org.jetbrains.annotations.NotNull;
import java.net.URL;
import java.util.function.Supplier;
public abstract class Html5SchemaProvider {
private static final Logger LOG = Logger.getInstance(Html5SchemaProvider.class);
private static final NotNullLazyValue<String> HTML5_SCHEMA_LOCATION = NotNullLazyValue.atomicLazy(() -> loadLocation(getInstance().getHtmlSchemaLocation(), "HTML5_SCHEMA"));
private static final NotNullLazyValue<String> XHTML5_SCHEMA_LOCATION = NotNullLazyValue.atomicLazy(() -> loadLocation(getInstance().getXhtmlSchemaLocation(), "XHTML5_SCHEMA"));
private static final NotNullLazyValue<String> CHARS_DTD_LOCATION = NotNullLazyValue.atomicLazy(() -> loadLocation(getInstance().getCharsLocation(), "CHARS_DTD"));
private static final Supplier<String>
HTML5_SCHEMA_LOCATION = new SynchronizedClearableLazy<>(() -> loadLocation(getInstance().getHtmlSchemaLocation(), "HTML5_SCHEMA"));
private static final Supplier<String> XHTML5_SCHEMA_LOCATION = new SynchronizedClearableLazy<>(() -> loadLocation(getInstance().getXhtmlSchemaLocation(), "XHTML5_SCHEMA"));
private static final Supplier<String> CHARS_DTD_LOCATION = new SynchronizedClearableLazy<>(() -> loadLocation(getInstance().getCharsLocation(), "CHARS_DTD"));
private static String loadLocation(URL url, String id) {
String location = VfsUtilCore.urlToPath(VfsUtilCore.fixURLforIDEA(
@@ -24,15 +26,15 @@ public abstract class Html5SchemaProvider {
}
public static String getHtml5SchemaLocation() {
return HTML5_SCHEMA_LOCATION.getValue();
return HTML5_SCHEMA_LOCATION.get();
}
public static String getXhtml5SchemaLocation() {
return XHTML5_SCHEMA_LOCATION.getValue();
return XHTML5_SCHEMA_LOCATION.get();
}
public static String getCharsDtdLocation() {
return CHARS_DTD_LOCATION.getValue();
return CHARS_DTD_LOCATION.get();
}
private static @NotNull Html5SchemaProvider getInstance() {