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));
}
}
}
}
};
}
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();
}
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();
}
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;
}
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()]));
}
}
}
Aggregations