Skip to content

Commit

Permalink
Merge pull request #1081 from Iamwade/1051-fix-already-disposed-exeption
Browse files Browse the repository at this point in the history
1051 fix already disposed exception
  • Loading branch information
bohdan-harniuk authored Apr 18, 2022
2 parents f94c9f0 + 79b5aad commit de9b5db
Showing 1 changed file with 99 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.magento.packages;

import com.intellij.json.psi.JsonFile;
import com.intellij.json.psi.JsonObject;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
Expand All @@ -18,40 +20,48 @@
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.indexing.FileBasedIndex;
import com.magento.idea.magento2plugin.stubs.indexes.ModulePackageIndex;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Service
public final class MagentoComponentManager {

public class MagentoComponentManager {
private Map<String, MagentoComponent> components = new HashMap<>();
private long cacheStartTime;
private static final int CACHE_LIFE_TIME = 20000;
private static MagentoComponentManager magentoComponentManager;
private Project project;
private static final int CACHE_LIFE_TIME = 20000;//NOPMD
private final Project project;

private MagentoComponentManager(Project project){
public MagentoComponentManager(final Project project) {
this.project = project;
}

public static MagentoComponentManager getInstance(@NotNull Project project) {
if (magentoComponentManager == null) {
magentoComponentManager = new MagentoComponentManager(project);
}
return magentoComponentManager;
public static MagentoComponentManager getInstance(final @NotNull Project project) {
return project.getService(MagentoComponentManager.class);
}

public Collection<MagentoComponent> getAllComponents() {
return getComponents().values();
}

/**
* Get all components of the specified type.
*
* @param type Class
*
* @return Collection
*/
@SuppressWarnings("unchecked")
public <T extends MagentoComponent> Collection<T> getAllComponentsOfType(@NotNull Class<T> type) {
Collection<T> result = new ArrayList<>();
Map<String, MagentoComponent> components = getComponents();
for (String key: components.keySet()) {
public <T extends MagentoComponent> Collection<T> getAllComponentsOfType(
final @NotNull Class<T> type
) {
final Collection<T> result = new ArrayList<>();
final Map<String, MagentoComponent> components = getComponents();

for (final String key: components.keySet()) {
if (type.isInstance(components.get(key))) {
result.add((T)components.get(key));
}
Expand All @@ -60,8 +70,9 @@ public <T extends MagentoComponent> Collection<T> getAllComponentsOfType(@NotNul
return result;
}

synchronized private Map<String, MagentoComponent> getComponents() {
if (DumbService.getInstance(project).isDumb() || project.isDisposed()) {
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
private synchronized Map<String, MagentoComponent> getComponents() {
if (project.isDisposed() || DumbService.getInstance(project).isDumb()) {
return new HashMap<>();
}

Expand All @@ -74,9 +85,16 @@ synchronized private Map<String, MagentoComponent> getComponents() {
return components;
}

/**
* Get component for the specified file.
*
* @param psiFile PsiFile
*
* @return MagentoComponent
*/
@Nullable
public MagentoComponent getComponentForFile(@NotNull PsiFile psiFile) {
for (MagentoComponent magentoComponent: this.getAllComponents()) {
public MagentoComponent getComponentForFile(final @NotNull PsiFile psiFile) {
for (final MagentoComponent magentoComponent: this.getAllComponents()) {
if (magentoComponent.isFileInContext(psiFile)) {
return magentoComponent;
}
Expand All @@ -85,10 +103,21 @@ public MagentoComponent getComponentForFile(@NotNull PsiFile psiFile) {
return null;
}

/**
* Get component of type for the specified file.
*
* @param psiFile PsiFile
* @param type Class
*
* @return T
*/
@Nullable
@SuppressWarnings("unchecked")
public <T extends MagentoComponent> T getComponentOfTypeForFile(@NotNull PsiFile psiFile, @NotNull Class<T> type) {
for (MagentoComponent magentoComponent: this.getAllComponents()) {
public <T extends MagentoComponent> T getComponentOfTypeForFile(
final @NotNull PsiFile psiFile,
final @NotNull Class<T> type
) {
for (final MagentoComponent magentoComponent: this.getAllComponents()) {
if (type.isInstance(magentoComponent) && magentoComponent.isFileInContext(psiFile)) {
return (T)magentoComponent;
}
Expand All @@ -97,72 +126,97 @@ public <T extends MagentoComponent> T getComponentOfTypeForFile(@NotNull PsiFile
return null;
}

synchronized public void flushModules() {
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
public synchronized void flushModules() {
components = new HashMap<>();
}

@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.AvoidDeeplyNestedIfStmts"})
private void loadModules() {
Collection<String> packages = FileBasedIndex.getInstance().getAllKeys(ModulePackageIndex.KEY, this.project);
PsiManager psiManager = PsiManager.getInstance(this.project);
for (String packageName: packages) {
final Collection<String> packages = FileBasedIndex
.getInstance()
.getAllKeys(ModulePackageIndex.KEY, this.project);
final PsiManager psiManager = PsiManager.getInstance(this.project);

for (final String packageName: packages) {
if (components.containsKey(packageName)) {
continue;
}

Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance()
.getContainingFiles(ModulePackageIndex.KEY, packageName, GlobalSearchScope.allScope(this.project));
final Collection<VirtualFile> containingFiles = FileBasedIndex
.getInstance()
.getContainingFiles(
ModulePackageIndex.KEY,
packageName,
GlobalSearchScope.allScope(this.project)
);

if (containingFiles.size() > 0) {
VirtualFile configurationFile = containingFiles.iterator().next();
if (!containingFiles.isEmpty()) {
final VirtualFile configurationFile = containingFiles.iterator().next();
final PsiFile psiFile = psiManager.findFile(configurationFile);

PsiFile psiFile = psiManager.findFile(configurationFile);
if (psiFile != null && psiFile instanceof JsonFile) {
JsonObject jsonObject = PsiTreeUtil.getChildOfType((JsonFile) psiFile, JsonObject.class);
if (psiFile instanceof JsonFile) {
final JsonObject jsonObject = PsiTreeUtil
.getChildOfType((JsonFile) psiFile, JsonObject.class);
if (jsonObject == null) {
continue;
}

MagentoComponent magentoComponent;
ComposerPackageModel composerPackageModel = new ComposerPackageModelImpl(jsonObject);
final ComposerPackageModel composerPackageModel = new ComposerPackageModelImpl(
jsonObject
);

if ("magento2-module".equals(composerPackageModel.getType())) {
magentoComponent = new MagentoModuleImpl(new ComposerPackageModelImpl(jsonObject), psiFile.getContainingDirectory());
magentoComponent = new MagentoModuleImpl(
new ComposerPackageModelImpl(jsonObject),
psiFile.getContainingDirectory()
);
} else {
magentoComponent = new MagentoComponentImp(new ComposerPackageModelImpl(jsonObject), psiFile.getContainingDirectory());
magentoComponent = new MagentoComponentImp(
new ComposerPackageModelImpl(jsonObject),
psiFile.getContainingDirectory()
);
}

components.put(
packageName,
magentoComponent
packageName,
magentoComponent
);
}
}
}
}
}

@SuppressWarnings("checkstyle:OneTopLevelClass")
class MagentoModuleImpl extends MagentoComponentImp implements MagentoModule {
private static final String DEFAULT_MODULE_NAME = "Undefined module";
private static final String CONFIGURATION_PATH = "etc";
private String moduleName;

public MagentoModuleImpl(@NotNull ComposerPackageModel composerPackageModel, @NotNull PsiDirectory directory) {
public MagentoModuleImpl(
final @NotNull ComposerPackageModel composerPackageModel,
final @NotNull PsiDirectory directory
) {
super(composerPackageModel, directory);
}

@SuppressWarnings({"PMD.AvoidDeeplyNestedIfStmts"})
@Override
public String getMagentoName() {
if (moduleName != null) {
return moduleName;
}

PsiDirectory configurationDir = directory.findSubdirectory(CONFIGURATION_PATH);
final PsiDirectory configurationDir = directory.findSubdirectory(CONFIGURATION_PATH);
if (configurationDir != null) {
PsiFile configurationFile = configurationDir.findFile("module.xml");
final PsiFile configurationFile = configurationDir.findFile("module.xml");

if (configurationFile != null && configurationFile instanceof XmlFile) {
XmlTag rootTag = ((XmlFile) configurationFile).getRootTag();
if (configurationFile instanceof XmlFile) {
final XmlTag rootTag = ((XmlFile) configurationFile).getRootTag();
if (rootTag != null) {
XmlTag module = rootTag.findFirstSubTag("module");
final XmlTag module = rootTag.findFirstSubTag("module");
if (module != null && module.getAttributeValue("name") != null) {
moduleName = module.getAttributeValue("name");
return moduleName;
Expand Down

0 comments on commit de9b5db

Please sign in to comment.