Skip to content

Commit

Permalink
Moved version management from static field to service
Browse files Browse the repository at this point in the history
Class initialization pathways may cause classloading errors
  • Loading branch information
hurricup committed Dec 18, 2023
1 parent c073198 commit b089c53
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,16 +16,14 @@

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;
import com.intellij.util.KeyedLazyInstance;
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;
Expand All @@ -38,23 +36,6 @@ public abstract class PerlSubCallHandler<CallData extends PerlSubCallElementData
PerlSelfHinter {
private static final KeyedExtensionCollector<PerlSubCallHandler<?>, String> EP =
new KeyedExtensionCollector<>("com.perl5.subCallHandler");
private static final ClearableLazyValue<Integer> VERSION_PROVIDER = AtomicClearableLazyValue.createAtomic(() -> {
int version = 0;
//noinspection UnstableApiUsage
for (KeyedLazyInstance<PerlSubCallHandler<?>> 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();
Expand All @@ -79,4 +60,8 @@ public void serialize(@NotNull PerlSubCallElementData callData, @NotNull StubOut
}
return EP.findSingle(subName);
}

static @NotNull ExtensionPoint<@NotNull KeyedLazyInstance<PerlSubCallHandler<?>>> getExtensionPoint() {
return Objects.requireNonNull(EP.getPoint());
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> myVersionProvider = AtomicClearableLazyValue.createAtomic(() -> {
int version = 0;
for (KeyedLazyInstance<PerlSubCallHandler<?>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,7 +48,7 @@ public int getStubVersion() {
return super.getStubVersion() +
VERSION +
PerlValuesManager.getVersion() +
PerlSubCallHandler.getHandlersVersion() +
PerlSubCallHandlerVersionService.getHandlersVersion() +
PerlPackageProcessorEP.getVersion();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand All @@ -40,7 +40,7 @@ public int getVersion() {
return super.getVersion() +
VERSION +
PerlValuesManager.getVersion() +
PerlSubCallHandler.getHandlersVersion() +
PerlSubCallHandlerVersionService.getHandlersVersion() +
PerlPackageProcessorEP.getVersion();
}

Expand Down

0 comments on commit b089c53

Please sign in to comment.