From b089c53cae7821751b937b0e3e678d99d13854e6 Mon Sep 17 00:00:00 2001 From: Alexandr Evstigneev Date: Mon, 18 Dec 2023 18:37:38 +0400 Subject: [PATCH] Moved version management from static field to service Class initialization pathways may cause classloading errors --- .../lang/perl/psi/PerlSubCallHandler.java | 27 +++------- .../psi/PerlSubCallHandlerVersionService.java | 53 +++++++++++++++++++ .../perl/psi/stubs/PerlFileElementType.java | 4 +- .../perl/psi/stubs/PerlStubIndexBase.java | 6 +-- 4 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandlerVersionService.java diff --git a/plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandler.java b/plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandler.java index d21197253c..12e699e27d 100644 --- a/plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandler.java +++ b/plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 Alexandr Evstigneev + * Copyright 2015-2023 Alexandr Evstigneev * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,7 @@ package com.perl5.lang.perl.psi; -import com.intellij.openapi.util.AtomicClearableLazyValue; -import com.intellij.openapi.util.ClearableLazyValue; +import com.intellij.openapi.extensions.ExtensionPoint; import com.intellij.openapi.util.KeyedExtensionCollector; import com.intellij.psi.stubs.StubInputStream; import com.intellij.psi.stubs.StubOutputStream; @@ -25,7 +24,6 @@ import com.perl5.lang.perl.psi.impl.PerlSubCallElement; import com.perl5.lang.perl.psi.stubs.calls.PerlSubCallElementData; import com.perl5.lang.perl.psi.stubs.calls.PerlSubCallElementStub; -import com.perl5.lang.perl.util.PerlPluginUtil; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,23 +36,6 @@ public abstract class PerlSubCallHandler, String> EP = new KeyedExtensionCollector<>("com.perl5.subCallHandler"); - private static final ClearableLazyValue VERSION_PROVIDER = AtomicClearableLazyValue.createAtomic(() -> { - int version = 0; - //noinspection UnstableApiUsage - for (KeyedLazyInstance> instance : EP.getPoint().getExtensions()) { - version += instance.getInstance().getVersion(); - } - return version; - }); - - static { - //noinspection UnstableApiUsage - Objects.requireNonNull(EP.getPoint()).addChangeListener(VERSION_PROVIDER::drop, PerlPluginUtil.getUnloadAwareDisposable()); - } - - public static int getHandlersVersion() { - return VERSION_PROVIDER.getValue(); - } public final @NotNull CallData getCallData(@NotNull PerlSubCallElement subCallElement) { PerlSubCallElementStub stub = subCallElement.getGreenStub(); @@ -79,4 +60,8 @@ public void serialize(@NotNull PerlSubCallElementData callData, @NotNull StubOut } return EP.findSingle(subName); } + + static @NotNull ExtensionPoint<@NotNull KeyedLazyInstance>> getExtensionPoint() { + return Objects.requireNonNull(EP.getPoint()); + } } diff --git a/plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandlerVersionService.java b/plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandlerVersionService.java new file mode 100644 index 0000000000..158b3f0b32 --- /dev/null +++ b/plugin/core/src/main/java/com/perl5/lang/perl/psi/PerlSubCallHandlerVersionService.java @@ -0,0 +1,53 @@ +/* + * Copyright 2015-2023 Alexandr Evstigneev + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.perl5.lang.perl.psi; + +import com.intellij.openapi.Disposable; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.Service; +import com.intellij.openapi.util.AtomicClearableLazyValue; +import com.intellij.openapi.util.ClearableLazyValue; +import com.intellij.util.KeyedLazyInstance; +import org.jetbrains.annotations.NotNull; + +@Service +public final class PerlSubCallHandlerVersionService implements Disposable { + private final ClearableLazyValue myVersionProvider = AtomicClearableLazyValue.createAtomic(() -> { + int version = 0; + for (KeyedLazyInstance> instance : PerlSubCallHandler.getExtensionPoint().getExtensions()) { + version += instance.getInstance().getVersion(); + } + return version; + }); + + public PerlSubCallHandlerVersionService() { + PerlSubCallHandler.getExtensionPoint().addChangeListener(myVersionProvider::drop, this); + } + + public static int getHandlersVersion() { + return getInstance().myVersionProvider.getValue(); + } + + @Override + public void dispose() { + myVersionProvider.drop(); + } + + private static @NotNull PerlSubCallHandlerVersionService getInstance() { + return ApplicationManager.getApplication().getService(PerlSubCallHandlerVersionService.class); + } +} diff --git a/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlFileElementType.java b/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlFileElementType.java index 6e3d952f53..108ab64053 100644 --- a/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlFileElementType.java +++ b/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlFileElementType.java @@ -27,7 +27,7 @@ import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValuesManager; import com.perl5.lang.perl.parser.builder.PerlPsiBuilderFactory; import com.perl5.lang.perl.psi.PerlFile; -import com.perl5.lang.perl.psi.PerlSubCallHandler; +import com.perl5.lang.perl.psi.PerlSubCallHandlerVersionService; import com.perl5.lang.perl.psi.stubs.namespaces.PerlNamespaceDefinitionData; import com.perl5.lang.perl.util.PerlPackageUtil; import org.jetbrains.annotations.NotNull; @@ -48,7 +48,7 @@ public int getStubVersion() { return super.getStubVersion() + VERSION + PerlValuesManager.getVersion() + - PerlSubCallHandler.getHandlersVersion() + + PerlSubCallHandlerVersionService.getHandlersVersion() + PerlPackageProcessorEP.getVersion(); } diff --git a/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlStubIndexBase.java b/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlStubIndexBase.java index 77ebf9857c..709cf83e57 100644 --- a/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlStubIndexBase.java +++ b/plugin/core/src/main/java/com/perl5/lang/perl/psi/stubs/PerlStubIndexBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 Alexandr Evstigneev + * Copyright 2015-2023 Alexandr Evstigneev * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ import com.intellij.util.Processor; import com.perl5.lang.perl.idea.EP.PerlPackageProcessorEP; import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValuesManager; -import com.perl5.lang.perl.psi.PerlSubCallHandler; +import com.perl5.lang.perl.psi.PerlSubCallHandlerVersionService; import com.perl5.lang.perl.util.PerlStubUtil; import org.jetbrains.annotations.NotNull; @@ -40,7 +40,7 @@ public int getVersion() { return super.getVersion() + VERSION + PerlValuesManager.getVersion() + - PerlSubCallHandler.getHandlersVersion() + + PerlSubCallHandlerVersionService.getHandlersVersion() + PerlPackageProcessorEP.getVersion(); }