From bb712072adb1701664e1c07a1d0e763cd2387ede Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Wed, 29 Sep 2021 14:02:11 +0300 Subject: [PATCH 1/2] Bug-659: Fixed NullPointerException for a name attribute of the event tag and code style fixes --- .../xml/ObserverDeclarationInspection.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java index 71e5dc2ee..b38a80a9c 100644 --- a/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java @@ -44,7 +44,7 @@ public class ObserverDeclarationInspection extends PhpInspection { @NotNull @Override - @SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops"}) + @SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"}) public PsiElementVisitor buildVisitor( final @NotNull ProblemsHolder problemsHolder, final boolean isOnTheFly @@ -56,8 +56,8 @@ public PsiElementVisitor buildVisitor( private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING; @Override - public void visitFile(final PsiFile file) { - if (!file.getName().equals(ModuleEventsXml.FILE_NAME)) { + public void visitFile(final @NotNull PsiFile file) { + if (!ModuleEventsXml.FILE_NAME.equals(file.getName())) { return; } @@ -72,13 +72,17 @@ public void visitFile(final PsiFile file) { for (final XmlTag eventXmlTag: xmlTags) { final HashMap eventProblems = new HashMap<>(); - if (!eventXmlTag.getName().equals(ModuleEventsXml.EVENT_TAG)) { + if (!ModuleEventsXml.EVENT_TAG.equals(eventXmlTag.getName())) { continue; } final XmlAttribute eventNameAttribute = eventXmlTag.getAttribute(Observer.NAME_ATTRIBUTE); + if (eventNameAttribute == null) { + continue; + } + final String eventNameAttributeValue = eventNameAttribute.getValue(); if (eventNameAttributeValue == null) { continue; @@ -133,13 +137,13 @@ public void visitFile(final PsiFile file) { @Nullable final XmlAttributeValue valueElement = observerNameAttribute.getValueElement(); if (modulesWithSameObserverName.isEmpty() && valueElement != null) { - problemsHolder.registerProblem( - valueElement, - inspectionBundle.message( + problemsHolder.registerProblem( + valueElement, + inspectionBundle.message( "inspection.observer.disabledObserverDoesNotExist" - ), - errorSeverity - ); + ), + errorSeverity + ); } else { continue; } @@ -159,11 +163,11 @@ public void visitFile(final PsiFile file) { problemsHolder.registerProblem( observerNameAttribute.getValueElement(), inspectionBundle.message( - "inspection.observer.duplicateInOtherPlaces", - observerName, - eventNameAttributeValue, - moduleName, - scope + "inspection.observer.duplicateInOtherPlaces", + observerName, + eventNameAttributeValue, + moduleName, + scope ), errorSeverity ); @@ -188,9 +192,9 @@ private List> fetchModuleNamesWhereSameObserverNameUsed( final Collection indexedEvents = eventIndex.getEventElements( eventNameAttributeValue, GlobalSearchScope.getScopeRestrictedByFileTypes( - GlobalSearchScope.allScope(file.getProject()), - XmlFileType.INSTANCE - )); + GlobalSearchScope.allScope(file.getProject()), + XmlFileType.INSTANCE + )); for (final PsiElement indexedEvent: indexedEvents) { final PsiFile indexedAttributeParent = @@ -247,7 +251,7 @@ private List fetchObserverTagsFromEventTag(final XmlTag eventXmlTag) { } for (final XmlTag observerXmlTag: observerXmlTags) { - if (!observerXmlTag.getName().equals(ModuleEventsXml.OBSERVER_TAG)) { + if (!ModuleEventsXml.OBSERVER_TAG.equals(observerXmlTag.getName())) { continue; } @@ -268,7 +272,7 @@ private void addModuleNameWhereSameObserverUsed( return; } - if (!moduleDeclarationTag.getName().equals(ModuleEventsXml.MODULE_TAG)) { + if (!ModuleEventsXml.MODULE_TAG.equals(moduleDeclarationTag.getName())) { return; } final XmlAttribute moduleNameAttribute From 6492f45934e4ac0b1f2630dbbebcf3e8649e0440 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Wed, 29 Sep 2021 14:51:25 +0300 Subject: [PATCH 2/2] Bug-653: Fixed NullPointerException for com.magento.idea.magento2plugin.actions.generation.InjectAViewModelAction#getElement --- .../generation/InjectAViewModelAction.java | 72 +++++++++++++------ .../dialog/InjectAViewModelDialog.java | 4 +- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/InjectAViewModelAction.java b/src/com/magento/idea/magento2plugin/actions/generation/InjectAViewModelAction.java index cd0dfc677..682f50b04 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/InjectAViewModelAction.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/InjectAViewModelAction.java @@ -22,38 +22,45 @@ import org.jetbrains.annotations.NotNull; public class InjectAViewModelAction extends DumbAwareAction { - public static String actionName = "Inject a new View Model for this block"; - public static String actionDescription = "Inject a new Magento 2 View Model"; + + public static final String ACTION_NAME = "Inject a new View Model for this block"; + public static final String ACTION_DESCRIPTION = "Inject a new Magento 2 View Model"; private XmlTag targetXmlTag; + /** + * An action constructor. + */ public InjectAViewModelAction() { - super(actionName, actionDescription, MagentoIcons.MODULE); + super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE); } @Override - public void update(final AnActionEvent event) { + public void update(final @NotNull AnActionEvent event) { + this.setStatus(event, false); final Project project = event.getData(PlatformDataKeys.PROJECT); + + if (project == null) { + return; + } + if (Settings.isEnabled(project)) { final XmlTag element = getElement(event); - if (element == null) { - this.setStatus(event, false); - } else { + + if (element != null) { targetXmlTag = element; this.setStatus(event, true); } - } else { - this.setStatus(event, false); } } - private void setStatus(final AnActionEvent event, final boolean status) { - event.getPresentation().setVisible(status); - event.getPresentation().setEnabled(status); - } - @Override public void actionPerformed(final @NotNull AnActionEvent event) { - InjectAViewModelDialog.open(event.getProject(), this.targetXmlTag); + final Project project = event.getData(PlatformDataKeys.PROJECT); + + if (project == null || targetXmlTag == null) { + return; + } + InjectAViewModelDialog.open(project, targetXmlTag); } @Override @@ -61,39 +68,64 @@ public boolean isDumbAware() { return false; } + /** + * Get focused (target) element for the event. + * + * @param event AnActionEvent + * + * @return XmlTag + */ private XmlTag getElement(final @NotNull AnActionEvent event) { final Caret caret = event.getData(PlatformDataKeys.CARET); + if (caret == null) { return null; } - final int offset = caret.getOffset(); final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE); + + if (psiFile == null) { + return null; + } + final int offset = caret.getOffset(); final PsiElement element = psiFile.findElementAt(offset); + if (element == null) { return null; } - final XmlTag xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag.class); if (xmlTag == null) { return null; } XmlTag resultTag; - if (xmlTag.getName().equals(CommonXml.ATTRIBUTE_ARGUMENTS)) { + + if (CommonXml.ATTRIBUTE_ARGUMENTS.equals(xmlTag.getName())) { resultTag = PsiTreeUtil.getParentOfType(xmlTag, XmlTag.class); } else { resultTag = xmlTag; } + if (resultTag == null) { return null; } - if (!resultTag.getName().equals(LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME) - && !resultTag.getName().equals(LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME)) { + if (!LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME.equals(resultTag.getName()) + && !LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME.equals(resultTag.getName())) { return null; } return resultTag; } + + /** + * Set presentation status. + * + * @param event AnActionEvent + * @param status boolean + */ + private void setStatus(final AnActionEvent event, final boolean status) { + event.getPresentation().setVisible(status); + event.getPresentation().setEnabled(status); + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/InjectAViewModelDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/InjectAViewModelDialog.java index 28a25f1fc..8252be35c 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/InjectAViewModelDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/InjectAViewModelDialog.java @@ -104,7 +104,7 @@ protected void textChanged(final @NotNull DocumentEvent event) { setContentPane(contentPane); setModal(true); - setTitle(InjectAViewModelAction.actionDescription); + setTitle(InjectAViewModelAction.ACTION_DESCRIPTION); getRootPane().setDefaultButton(buttonOK); buttonOK.addActionListener((final ActionEvent event) -> onOK()); @@ -154,7 +154,7 @@ protected void onOK() { getViewModelClassName(), moduleName, namespaceBuilder.getNamespace() - ), project).generate(InjectAViewModelAction.actionName, true); + ), project).generate(InjectAViewModelAction.ACTION_NAME, true); if (viewModel == null) { final String errorMessage = validatorBundle.message( "validator.class.alreadyDeclared",