mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
stateful EP inspection: don't warn about project services
This commit is contained in:
@@ -26,9 +26,11 @@ import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.idea.devkit.util.ExtensionCandidate;
|
||||
import org.jetbrains.idea.devkit.util.ExtensionLocator;
|
||||
import org.jetbrains.uast.UClass;
|
||||
|
||||
@@ -42,13 +44,20 @@ public class StatefulEpInspection extends DevKitUastInspectionBase {
|
||||
if (fields.length == 0) return super.checkClass(psiClass, manager, isOnTheFly);
|
||||
|
||||
final boolean isQuickFix = InheritanceUtil.isInheritor(psiClass, LocalQuickFix.class.getCanonicalName());
|
||||
if (isQuickFix || ExtensionLocator.isRegisteredExtension(psiClass)) {
|
||||
final boolean isProjectComponent = InheritanceUtil.isInheritor(psiClass, ProjectComponent.class.getCanonicalName());
|
||||
ExtensionLocator locator = new ExtensionLocator(psiClass);
|
||||
List<ExtensionCandidate> targets = locator.findCandidates();
|
||||
if (isQuickFix || !targets.isEmpty()) {
|
||||
boolean isProjectComponent = InheritanceUtil.isInheritor(psiClass, ProjectComponent.class.getCanonicalName());
|
||||
boolean projectService = ContainerUtil.find(targets, candidate -> {
|
||||
XmlTag element = candidate.pointer.getElement();
|
||||
String name = element != null ? element.getName() : null;
|
||||
return "projectService".equals(name);
|
||||
}) != null;
|
||||
|
||||
List<ProblemDescriptor> result = ContainerUtil.newArrayList();
|
||||
for (final PsiField field : fields) {
|
||||
for (PsiField field : fields) {
|
||||
for (Class c : new Class[]{PsiElement.class, PsiReference.class, Project.class}) {
|
||||
if (c == Project.class && (field.hasModifierProperty(PsiModifier.FINAL) || isProjectComponent)) continue;
|
||||
if (c == Project.class && (field.hasModifierProperty(PsiModifier.FINAL) || isProjectComponent || projectService)) continue;
|
||||
String message = c == PsiElement.class
|
||||
? "Potential memory leak: don't hold PsiElement, use SmartPsiElementPointer instead" +
|
||||
(isQuickFix ? "; also see LocalQuickFixOnPsiElement" : "")
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import com.intellij.openapi.project.Project;
|
||||
|
||||
public class ProjectService {
|
||||
<warning descr="Potential memory leak: don't hold PsiElement, use SmartPsiElementPointer instead">final com.intellij.psi.PsiElement pe;</warning>
|
||||
<warning descr="Don't use PsiReference as a field in extension">final com.intellij.psi.PsiReference r;</warning>
|
||||
Project p;
|
||||
final Project pf;
|
||||
public ProjectService(Project project) {
|
||||
super();
|
||||
pe = null;
|
||||
r = null;
|
||||
p = pf = project;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
<idea-plugin>
|
||||
<extensionPoints>
|
||||
<extensionPoint name="ep" beanClass="EP"/>
|
||||
<extensionPoint name="projectService" beanClass="SD"/>
|
||||
</extensionPoints>
|
||||
<extensions>
|
||||
<ep implementation="Ext"/>
|
||||
<ep implementation="NonFix$Ext2"/>
|
||||
<ep implementation="ProjectComp"/>
|
||||
<ep implementation="ProjectComp"/>
|
||||
<projectService serviceImplementation="ProjectService"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -54,4 +54,9 @@ public class StatefulEpInspectionTest extends PluginModuleTestCase {
|
||||
setPluginXml("plugin.xml");
|
||||
myFixture.testHighlighting("ProjectComp.java");
|
||||
}
|
||||
|
||||
public void testProjectService() {
|
||||
setPluginXml("plugin.xml");
|
||||
myFixture.testHighlighting("ProjectService.java");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user