Search in sources :

Example 1 with ImplicitUsageProvider

use of com.intellij.codeInsight.daemon.ImplicitUsageProvider in project intellij-community by JetBrains.

the class XmlUnusedNamespaceInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            PsiFile file = holder.getFile();
            if (!(file instanceof XmlFile))
                return;
            XmlRefCountHolder refCountHolder = XmlRefCountHolder.getRefCountHolder((XmlFile) file);
            if (refCountHolder == null)
                return;
            if (!attribute.isNamespaceDeclaration()) {
                checkUnusedLocations(attribute, holder, refCountHolder);
                return;
            }
            String namespace = attribute.getValue();
            String declaredPrefix = getDeclaredPrefix(attribute);
            if (namespace != null && !refCountHolder.isInUse(declaredPrefix)) {
                ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
                for (ImplicitUsageProvider provider : implicitUsageProviders) {
                    if (provider.isImplicitUsage(attribute))
                        return;
                }
                XmlAttributeValue value = attribute.getValueElement();
                assert value != null;
                holder.registerProblem(attribute, XmlBundle.message("xml.inspections.unused.schema.declaration"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, false, !refCountHolder.isUsedNamespace(namespace)));
                XmlTag parent = attribute.getParent();
                if (declaredPrefix.isEmpty()) {
                    XmlAttribute location = getDefaultLocation(parent);
                    if (location != null) {
                        holder.registerProblem(location, XmlBundle.message("xml.inspections.unused.schema.location"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                } else if (!refCountHolder.isUsedNamespace(namespace)) {
                    for (PsiReference reference : getLocationReferences(namespace, parent)) {
                        if (!XmlHighlightVisitor.hasBadResolve(reference, false))
                            holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                }
            }
        }
    };
}
Also used : XmlRefCountHolder(com.intellij.xml.util.XmlRefCountHolder) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ImplicitUsageProvider

use of com.intellij.codeInsight.daemon.ImplicitUsageProvider in project intellij-community by JetBrains.

the class DataFlowInspectionTest method testImplicitlyInitializedField.

public void testImplicitlyInitializedField() {
    PlatformTestUtil.registerExtension(ImplicitUsageProvider.EP_NAME, new ImplicitUsageProvider() {

        @Override
        public boolean isImplicitUsage(PsiElement element) {
            return false;
        }

        @Override
        public boolean isImplicitRead(PsiElement element) {
            return false;
        }

        @Override
        public boolean isImplicitWrite(PsiElement element) {
            return element instanceof PsiField && ((PsiField) element).getName().startsWith("field");
        }
    }, myFixture.getTestRootDisposable());
    doTest();
}
Also used : PsiField(com.intellij.psi.PsiField) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) PsiElement(com.intellij.psi.PsiElement)

Example 3 with ImplicitUsageProvider

use of com.intellij.codeInsight.daemon.ImplicitUsageProvider in project intellij-community by JetBrains.

the class CanBeFinalTest method testfieldImplicitWrite.

public void testfieldImplicitWrite() throws Exception {
    PlatformTestUtil.registerExtension(ImplicitUsageProvider.EP_NAME, new ImplicitUsageProvider() {

        @Override
        public boolean isImplicitUsage(PsiElement element) {
            return isImplicitWrite(element);
        }

        @Override
        public boolean isImplicitRead(PsiElement element) {
            return false;
        }

        @Override
        public boolean isImplicitWrite(PsiElement element) {
            return element instanceof PsiField && "implicitWrite".equals(((PsiNamedElement) element).getName());
        }
    }, getTestRootDisposable());
    doTest();
}
Also used : PsiField(com.intellij.psi.PsiField) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) PsiElement(com.intellij.psi.PsiElement)

Example 4 with ImplicitUsageProvider

use of com.intellij.codeInsight.daemon.ImplicitUsageProvider in project intellij-community by JetBrains.

the class PostHighlightingVisitor method isUnusedImportEnabled.

private boolean isUnusedImportEnabled(HighlightDisplayKey unusedImportKey) {
    InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    if (profile.isToolEnabled(unusedImportKey, myFile) && myFile instanceof PsiJavaFile && HighlightingLevelManager.getInstance(myProject).shouldInspect(myFile)) {
        return true;
    }
    final ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
    for (ImplicitUsageProvider provider : implicitUsageProviders) {
        if (provider instanceof UnusedImportProvider && ((UnusedImportProvider) provider).isUnusedImportEnabled(myFile))
            return true;
    }
    return false;
}
Also used : UnusedImportProvider(com.intellij.codeInsight.daemon.UnusedImportProvider) InspectionProfile(com.intellij.codeInspection.InspectionProfile) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider)

Example 5 with ImplicitUsageProvider

use of com.intellij.codeInsight.daemon.ImplicitUsageProvider in project intellij-community by JetBrains.

the class FieldCanBeLocalInspectionBase method doCheckClass.

private void doCheckClass(final PsiClass aClass, ProblemsHolder holder, final List<String> excludeAnnos, boolean ignoreFieldsUsedInMultipleMethods) {
    if (aClass.isInterface())
        return;
    final PsiField[] fields = aClass.getFields();
    final Set<PsiField> candidates = new LinkedHashSet<>();
    for (PsiField field : fields) {
        if (AnnotationUtil.isAnnotated(field, excludeAnnos)) {
            continue;
        }
        if (field.hasModifierProperty(PsiModifier.PRIVATE) && !(field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL))) {
            candidates.add(field);
        }
    }
    removeFieldsReferencedFromInitializers(aClass, candidates);
    if (candidates.isEmpty())
        return;
    final Set<PsiField> usedFields = new THashSet<>();
    removeReadFields(aClass, candidates, usedFields, ignoreFieldsUsedInMultipleMethods);
    if (candidates.isEmpty())
        return;
    final ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
    for (final PsiField field : candidates) {
        if (usedFields.contains(field) && !hasImplicitReadOrWriteUsage(field, implicitUsageProviders)) {
            if (!ReferencesSearch.search(field, new LocalSearchScope(aClass)).forEach(reference -> {
                final PsiElement element = reference.getElement();
                if (element instanceof PsiReferenceExpression) {
                    final PsiElement qualifier = ((PsiReferenceExpression) element).getQualifier();
                    return qualifier == null || qualifier instanceof PsiThisExpression && ((PsiThisExpression) qualifier).getQualifier() == null;
                }
                return true;
            })) {
                continue;
            }
            final String message = InspectionsBundle.message("inspection.field.can.be.local.problem.descriptor");
            final ArrayList<LocalQuickFix> fixes = new ArrayList<>();
            SpecialAnnotationsUtilBase.createAddToSpecialAnnotationFixes(field, qualifiedName -> {
                final LocalQuickFix quickFix = SpecialAnnotationsUtilBase.createAddToSpecialAnnotationsListQuickFix(InspectionGadgetsBundle.message("add.0.to.ignore.if.annotated.by.list.quickfix", qualifiedName), QuickFixBundle.message("fix.add.special.annotation.family"), EXCLUDE_ANNOS, qualifiedName, field);
                fixes.add(quickFix);
                return true;
            });
            final LocalQuickFix fix = createFix();
            if (fix != null) {
                fixes.add(fix);
            }
            holder.registerProblem(field.getNameIdentifier(), message, fixes.toArray(new LocalQuickFix[fixes.size()]));
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SpecialAnnotationsUtilBase(com.intellij.codeInspection.util.SpecialAnnotationsUtilBase) GroupNames(com.intellij.codeInsight.daemon.GroupNames) JDOMExternalizableStringList(com.intellij.openapi.util.JDOMExternalizableStringList) java.util(java.util) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) NonNls(org.jetbrains.annotations.NonNls) THashSet(gnu.trove.THashSet) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) InspectionGadgetsBundle(com.siyeh.InspectionGadgetsBundle) Comparing(com.intellij.openapi.util.Comparing) PsiUtil(com.intellij.psi.util.PsiUtil) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) QuickFixBundle(com.intellij.codeInsight.daemon.QuickFixBundle) Extensions(com.intellij.openapi.extensions.Extensions) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) InspectionsBundle(com.intellij.codeInspection.InspectionsBundle) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) BaseJavaBatchLocalInspectionTool(com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) com.intellij.psi(com.intellij.psi) WriteExternalException(com.intellij.openapi.util.WriteExternalException) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) Element(org.jdom.Element) com.intellij.psi.controlFlow(com.intellij.psi.controlFlow) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) THashSet(gnu.trove.THashSet) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider)

Aggregations

ImplicitUsageProvider (com.intellij.codeInsight.daemon.ImplicitUsageProvider)5 PsiElement (com.intellij.psi.PsiElement)2 PsiField (com.intellij.psi.PsiField)2 NotNull (org.jetbrains.annotations.NotNull)2 AnnotationUtil (com.intellij.codeInsight.AnnotationUtil)1 GroupNames (com.intellij.codeInsight.daemon.GroupNames)1 QuickFixBundle (com.intellij.codeInsight.daemon.QuickFixBundle)1 UnusedImportProvider (com.intellij.codeInsight.daemon.UnusedImportProvider)1 BaseJavaBatchLocalInspectionTool (com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool)1 InspectionProfile (com.intellij.codeInspection.InspectionProfile)1 InspectionsBundle (com.intellij.codeInspection.InspectionsBundle)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 SpecialAnnotationsUtilBase (com.intellij.codeInspection.util.SpecialAnnotationsUtilBase)1 Extensions (com.intellij.openapi.extensions.Extensions)1 Comparing (com.intellij.openapi.util.Comparing)1 JDOMExternalizableStringList (com.intellij.openapi.util.JDOMExternalizableStringList)1 Ref (com.intellij.openapi.util.Ref)1 WriteExternalException (com.intellij.openapi.util.WriteExternalException)1 com.intellij.psi (com.intellij.psi)1