protected InspectionTool[] getInspectionTools(PsiElement psiElement, InspectionManager manager) { final ModifiableModel model = InspectionProjectProfileManager.getInstance(manager.getProject()) .getInspectionProfile() .getModifiableModel(); InspectionProfileWrapper profile = new InspectionProfileWrapper((InspectionProfile) model); profile.init(manager.getProject()); return profile.getInspectionTools(psiElement); }
@Override public ProblemDescriptor[] checkClass( @NotNull PsiClass aClass, @NotNull InspectionManager manager, boolean isOnTheFly) { final Project project = aClass.getProject(); final PsiIdentifier nameIdentifier = aClass.getNameIdentifier(); final Module module = ModuleUtil.findModuleForPsiElement(aClass); if (nameIdentifier == null || module == null || !PsiUtil.isInstanciatable(aClass)) return null; final PsiClass base = JavaPsiFacade.getInstance(project) .findClass(INTENTION, GlobalSearchScope.allScope(project)); if (base == null || !aClass.isInheritor(base, true)) return null; String descriptionDir = getDescriptionDirName(aClass); if (StringUtil.isEmptyOrSpaces(descriptionDir)) { return null; } for (PsiDirectory description : getIntentionDescriptionsDirs(module)) { PsiDirectory dir = description.findSubdirectory(descriptionDir); if (dir == null) continue; final PsiFile descr = dir.findFile("description.html"); if (descr != null) { if (!hasBeforeAndAfterTemplate(dir.getVirtualFile())) { PsiElement problem = aClass.getNameIdentifier(); ProblemDescriptor problemDescriptor = manager.createProblemDescriptor( problem == null ? nameIdentifier : problem, "Intention must have 'before.*.template' and 'after.*.template' beside 'description.html'", isOnTheFly, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); return new ProblemDescriptor[] {problemDescriptor}; } return null; } } final PsiElement problem = aClass.getNameIdentifier(); final ProblemDescriptor problemDescriptor = manager.createProblemDescriptor( problem == null ? nameIdentifier : problem, "Intention does not have a description", isOnTheFly, new LocalQuickFix[] {new CreateHtmlDescriptionFix(descriptionDir, module, true)}, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); return new ProblemDescriptor[] {problemDescriptor}; }
private void reportMissingOnClickProblem( OnClickConverter.MyReference reference, PsiClass activity, String methodName, boolean incorrectSignature) { String activityName = activity.getName(); if (activityName == null) { activityName = ""; } final String message = incorrectSignature ? AndroidBundle.message( "android.inspections.on.click.missing.incorrect.signature", methodName, activityName) : AndroidBundle.message( "android.inspections.on.click.missing.problem", methodName, activityName); final LocalQuickFix[] fixes = StringUtil.isJavaIdentifier(methodName) ? new LocalQuickFix[] {new MyQuickFix(methodName, reference.getConverter(), activity)} : LocalQuickFix.EMPTY_ARRAY; myResult.add( myInspectionManager.createProblemDescriptor( reference.getElement(), reference.getRangeInElement(), message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myOnTheFly, fixes)); }
@Nullable private ProblemDescriptor[] checkMember( final PsiDocCommentOwner docCommentOwner, final InspectionManager manager, final boolean isOnTheFly) { final ArrayList<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>(); final PsiDocComment docComment = docCommentOwner.getDocComment(); if (docComment == null) return null; final Set<PsiJavaCodeReferenceElement> references = new HashSet<PsiJavaCodeReferenceElement>(); docComment.accept(getVisitor(references, docCommentOwner, problems, manager, isOnTheFly)); for (PsiJavaCodeReferenceElement reference : references) { final List<PsiClass> classesToImport = new ImportClassFix(reference).getClassesToImport(); final PsiElement referenceNameElement = reference.getReferenceNameElement(); problems.add( manager.createProblemDescriptor( referenceNameElement != null ? referenceNameElement : reference, cannotResolveSymbolMessage("<code>" + reference.getText() + "</code>"), !isOnTheFly || classesToImport.isEmpty() ? null : new AddImportFix(classesToImport), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, isOnTheFly)); } return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]); }
private static void checkMethodReference( PsiMethodReferenceExpression expression, InspectionManager inspectionManager, List<ProblemDescriptor> problems) { final PsiTypeElement qualifierTypeElement = expression.getQualifierType(); if (qualifierTypeElement != null) { final PsiType psiType = qualifierTypeElement.getType(); if (psiType instanceof PsiClassType && !(((PsiClassType) psiType).isRaw())) { final JavaResolveResult result = expression.advancedResolve(false); final PsiElement element = result.getElement(); if (element instanceof PsiTypeParameterListOwner) { final PsiMethodReferenceExpression copy = createMethodReference(expression, qualifierTypeElement); final JavaResolveResult simplifiedResolve = copy.advancedResolve(false); final PsiElement candidate = simplifiedResolve.getElement(); if (candidate == element) { final PsiJavaCodeReferenceElement referenceElement = qualifierTypeElement.getInnermostComponentReferenceElement(); LOG.assertTrue(referenceElement != null, qualifierTypeElement); final PsiReferenceParameterList parameterList = referenceElement.getParameterList(); LOG.assertTrue(parameterList != null); final ProblemDescriptor descriptor = inspectionManager.createProblemDescriptor( parameterList, InspectionsBundle.message("inspection.redundant.type.problem.descriptor"), new MyMethodReferenceFixAction(), ProblemHighlightType.LIKE_UNUSED_SYMBOL, false); problems.add(descriptor); } } } } }
@Nullable private ProblemDescriptor createDescription( @NotNull PsiTypeCastExpression cast, @NotNull InspectionManager manager, boolean onTheFly) { PsiExpression operand = cast.getOperand(); PsiTypeElement castType = cast.getCastType(); if (operand == null || castType == null) return null; PsiElement parent = cast.getParent(); while (parent instanceof PsiParenthesizedExpression) { parent = parent.getParent(); } if (parent instanceof PsiReferenceExpression) { if (IGNORE_ANNOTATED_METHODS) { final PsiElement gParent = parent.getParent(); if (gParent instanceof PsiMethodCallExpression) { final PsiMethod psiMethod = ((PsiMethodCallExpression) gParent).resolveMethod(); if (psiMethod != null && NullableNotNullManager.isNotNull(psiMethod)) { final PsiClass superClass = PsiUtil.resolveClassInType(operand.getType()); final PsiClass containingClass = psiMethod.getContainingClass(); if (containingClass != null && superClass != null && containingClass.isInheritor(superClass, true)) { for (PsiMethod method : psiMethod.findSuperMethods(superClass)) { if (NullableNotNullManager.isNullable(method)) { return null; } } } } } } } else if (parent instanceof PsiExpressionList) { final PsiElement gParent = parent.getParent(); if (gParent instanceof PsiMethodCallExpression && IGNORE_SUSPICIOUS_METHOD_CALLS) { final String message = SuspiciousCollectionsMethodCallsInspection.getSuspiciousMethodCallMessage( (PsiMethodCallExpression) gParent, operand.getType(), true, new ArrayList<PsiMethod>(), new IntArrayList()); if (message != null) { return null; } } } String message = InspectionsBundle.message( "inspection.redundant.cast.problem.descriptor", "<code>" + operand.getText() + "</code>", "<code>#ref</code> #loc"); return manager.createProblemDescriptor( castType, message, myQuickFixAction, ProblemHighlightType.LIKE_UNUSED_SYMBOL, onTheFly); }
@Nullable private static CommonProblemDescriptor createDescriptor( @Nullable RefEntity element, @NotNull OfflineProblemDescriptor offlineDescriptor, @NotNull InspectionToolWrapper toolWrapper, @NotNull InspectionToolPresentation presentation) { if (!(toolWrapper instanceof LocalInspectionToolWrapper)) return null; final InspectionManager inspectionManager = InspectionManager.getInstance(presentation.getContext().getProject()); final OfflineProblemDescriptor offlineProblemDescriptor = offlineDescriptor; if (element instanceof RefElement) { final PsiElement psiElement = ((RefElement) element).getElement(); if (psiElement != null) { ProblemDescriptor descriptor = ProgressManager.getInstance() .runProcess( () -> runLocalTool( psiElement, inspectionManager, offlineProblemDescriptor, (LocalInspectionToolWrapper) toolWrapper), new DaemonProgressIndicator()); if (descriptor != null) return descriptor; } return null; } final List<String> hints = offlineProblemDescriptor.getHints(); CommonProblemDescriptor descriptor = inspectionManager.createProblemDescriptor( offlineProblemDescriptor.getDescription(), (QuickFix) null); final QuickFix[] quickFixes = getFixes(descriptor, hints, presentation); if (quickFixes != null) { descriptor = inspectionManager.createProblemDescriptor( offlineProblemDescriptor.getDescription(), quickFixes); } return descriptor; }
@Override @Nullable public CommonProblemDescriptor[] checkElement( @NotNull final RefEntity refEntity, @NotNull final AnalysisScope scope, @NotNull final InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull final ProblemDescriptionsProcessor processor) { if (refEntity instanceof RefJavaElement) { final RefJavaElement refElement = (RefJavaElement) refEntity; if (refElement instanceof RefParameter) return null; if (!refElement.isReferenced()) return null; if (refElement.isSyntheticJSP()) return null; if (refElement.isFinal()) return null; if (!((RefElementImpl) refElement).checkFlag(CanBeFinalAnnotator.CAN_BE_FINAL_MASK)) return null; final PsiMember psiMember = (PsiMember) refElement.getElement(); if (psiMember == null || !CanBeFinalHandler.allowToBeFinal(psiMember)) return null; PsiIdentifier psiIdentifier = null; if (refElement instanceof RefClass) { RefClass refClass = (RefClass) refElement; if (refClass.isInterface() || refClass.isAnonymous() || refClass.isAbstract()) return null; if (!isReportClasses()) return null; psiIdentifier = ((PsiClass) psiMember).getNameIdentifier(); } else if (refElement instanceof RefMethod) { RefMethod refMethod = (RefMethod) refElement; if (refMethod.getOwnerClass().isFinal()) return null; if (!isReportMethods()) return null; psiIdentifier = ((PsiMethod) psiMember).getNameIdentifier(); } else if (refElement instanceof RefField) { if (!isReportFields()) return null; psiIdentifier = ((PsiField) psiMember).getNameIdentifier(); } if (psiIdentifier != null) { return new ProblemDescriptor[] { manager.createProblemDescriptor( psiIdentifier, InspectionsBundle.message("inspection.export.results.can.be.final.description"), new AcceptSuggested(globalContext.getRefManager()), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) }; } } return null; }
@Nullable public CommonProblemDescriptor[] checkElement( final RefEntity refEntity, final AnalysisScope scope, final InspectionManager manager, final GlobalInspectionContext globalContext, final ProblemDescriptionsProcessor processor) { if (refEntity instanceof RefMethod) { final RefMethod refMethod = (RefMethod) refEntity; if (refMethod.isSyntheticJSP()) return null; if (refMethod.isExternalOverride()) return null; if (!(refMethod.isStatic() || refMethod.isConstructor()) && !refMethod.getSuperMethods().isEmpty()) return null; if ((refMethod.isAbstract() || refMethod.getOwnerClass().isInterface()) && refMethod.getDerivedMethods().isEmpty()) return null; if (RefUtil.isEntryPoint(refMethod)) return null; if (refMethod.isAppMain()) return null; final ArrayList<RefParameter> unusedParameters = getUnusedParameters(refMethod); if (unusedParameters.isEmpty()) return null; final List<ProblemDescriptor> result = new ArrayList<ProblemDescriptor>(); for (RefParameter refParameter : unusedParameters) { final PsiIdentifier psiIdentifier = refParameter.getElement().getNameIdentifier(); if (psiIdentifier != null) { result.add( manager.createProblemDescriptor( psiIdentifier, refMethod.isAbstract() ? InspectionsBundle.message("inspection.unused.parameter.composer") : InspectionsBundle.message("inspection.unused.parameter.composer1"), new AcceptSuggested( globalContext.getRefManager(), processor, refParameter.toString()), ProblemHighlightType.LIKE_UNUSED_SYMBOL, false)); } } return result.toArray(new CommonProblemDescriptor[result.size()]); } return null; }
@Override public CommonProblemDescriptor[] checkElement( @NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull final InspectionManager manager, @NotNull final GlobalInspectionContext globalContext) { if (refEntity instanceof RefMethod) { RefMethod refMethod = (RefMethod) refEntity; if (!refMethod.isReferenced()) return null; if (hasNonInvertedCalls(refMethod)) return null; if (!refMethod.getSuperMethods().isEmpty()) return null; final PsiMethod psiMethod = (PsiMethod) refMethod.getElement(); final PsiIdentifier psiIdentifier = psiMethod.getNameIdentifier(); if (psiIdentifier != null) { final Collection<RefElement> inReferences = refMethod.getInReferences(); if (inReferences.size() == 1) { final RefElement refElement = inReferences.iterator().next(); final PsiElement usagesContainer = refElement.getElement(); if (usagesContainer == null) return null; if (ReferencesSearch.search(psiMethod, new LocalSearchScope(usagesContainer)) .forEach( new Processor<PsiReference>() { private final Set<PsiReference> myFoundRefs = new HashSet<>(); @Override public boolean process(PsiReference reference) { myFoundRefs.add(reference); return myFoundRefs.size() < 2; } })) return null; } return new ProblemDescriptor[] { manager.createProblemDescriptor( psiIdentifier, InspectionsBundle.message("boolean.method.is.always.inverted.problem.descriptor"), (LocalQuickFix) getQuickFix(null), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) }; } } return null; }
@Nullable public CommonProblemDescriptor[] checkElement( RefEntity refEntity, AnalysisScope scope, InspectionManager manager, GlobalInspectionContext globalContext, ProblemDescriptionsProcessor processor) { ArrayList<ProblemDescriptor> problems = null; if (refEntity instanceof RefMethod) { final RefMethod refMethod = (RefMethod) refEntity; if (refMethod.hasSuperMethods()) return null; if (refMethod.isEntry()) return null; RefParameter[] parameters = refMethod.getParameters(); for (RefParameter refParameter : parameters) { String value = refParameter.getActualValueIfSame(); if (value != null) { if (problems == null) problems = new ArrayList<ProblemDescriptor>(1); final String paramName = refParameter.getName(); problems.add( manager.createProblemDescriptor( refParameter.getElement(), InspectionsBundle.message( "inspection.same.parameter.problem.descriptor", "<code>" + paramName + "</code>", "<code>" + value + "</code>"), new InlineParameterValueFix(paramName, value), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false)); } } } return problems == null ? null : problems.toArray(new CommonProblemDescriptor[problems.size()]); }
@Nullable public ProblemDescriptor[] checkFile( @NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { // does not work in tests since CodeInsightTestCase copies file into temporary location if (ApplicationManager.getApplication().isUnitTestMode()) return null; if (file instanceof PsiJavaFile) { if (JspPsiUtil.isInJspFile(file)) return null; PsiJavaFile javaFile = (PsiJavaFile) file; PsiDirectory directory = javaFile.getContainingDirectory(); if (directory == null) return null; PsiPackage dirPackage = JavaDirectoryService.getInstance().getPackage(directory); if (dirPackage == null) return null; PsiPackageStatement packageStatement = javaFile.getPackageStatement(); // highlight the first class in the file only PsiClass[] classes = javaFile.getClasses(); if (classes.length == 0 && packageStatement == null) return null; String packageName = dirPackage.getQualifiedName(); if (!Comparing.strEqual(packageName, "", true) && packageStatement == null) { String description = JavaErrorMessages.message("missing.package.statement", packageName); return new ProblemDescriptor[] { manager.createProblemDescriptor( classes[0].getNameIdentifier(), description, new AdjustPackageNameFix(javaFile, null, dirPackage), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly) }; } if (packageStatement != null) { final PsiJavaCodeReferenceElement packageReference = packageStatement.getPackageReference(); PsiPackage classPackage = (PsiPackage) packageReference.resolve(); List<LocalQuickFix> availableFixes = new ArrayList<LocalQuickFix>(); if (classPackage == null) { availableFixes.add(new AdjustPackageNameFix(javaFile, packageStatement, dirPackage)); } else if (!Comparing.equal( dirPackage.getQualifiedName(), packageReference.getText(), true)) { availableFixes.add(new AdjustPackageNameFix(javaFile, packageStatement, dirPackage)); MoveToPackageFix moveToPackageFix = new MoveToPackageFix(file, classPackage); if (moveToPackageFix.isAvailable()) { availableFixes.add(moveToPackageFix); } } if (!availableFixes.isEmpty()) { String description = JavaErrorMessages.message( "package.name.file.path.mismatch", packageReference.getText(), dirPackage.getQualifiedName()); return new ProblemDescriptor[] { manager.createProblemDescriptor( packageStatement.getPackageReference(), description, isOnTheFly, availableFixes.toArray(new LocalQuickFix[availableFixes.size()]), ProblemHighlightType.GENERIC_ERROR_OR_WARNING) }; } } } return null; }
@Override @Nullable public CommonProblemDescriptor[] checkElement( @NotNull final RefEntity refEntity, @NotNull final AnalysisScope scope, @NotNull final InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull final ProblemDescriptionsProcessor processor) { if (refEntity instanceof RefJavaElement) { final RefJavaElement refElement = (RefJavaElement) refEntity; if (refElement instanceof RefParameter) return null; if (refElement.isSyntheticJSP()) return null; // ignore entry points. if (refElement.isEntry()) return null; // ignore implicit constructors. User should not be able to see them. if (refElement instanceof RefImplicitConstructor) return null; if (refElement instanceof RefField && ((RefField) refElement).getElement() instanceof PsiEnumConstant) return null; // ignore library override methods. if (refElement instanceof RefMethod) { RefMethod refMethod = (RefMethod) refElement; if (refMethod.isExternalOverride()) return null; if (refMethod.isEntry()) return null; } // ignore anonymous classes. They do not have access modifiers. if (refElement instanceof RefClass) { RefClass refClass = (RefClass) refElement; if (refClass.isAnonymous() || refClass.isEntry() || refClass.isTestCase() || refClass.isServlet() || refClass.isApplet() || refClass.isLocalClass()) return null; if (isTopLevelClass(refClass) && !SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES) return null; } // ignore unreferenced code. They could be a potential entry points. if (refElement.getInReferences().isEmpty()) return null; // ignore interface members. They always have public access modifier. if (refElement.getOwner() instanceof RefClass) { RefClass refClass = (RefClass) refElement.getOwner(); if (refClass.isInterface()) return null; } String access = getPossibleAccess(refElement); if (access != refElement.getAccessModifier() && access != null) { final PsiElement element = refElement.getElement(); final PsiElement nameIdentifier = element != null ? HighlightUsagesHandler.getNameIdentifier(element) : null; if (nameIdentifier != null) { return new ProblemDescriptor[] { manager.createProblemDescriptor( nameIdentifier, access.equals(PsiModifier.PRIVATE) ? CAN_BE_PRIVATE : access.equals(PsiModifier.PACKAGE_LOCAL) ? CAN_BE_PACKAGE_LOCAL : CAN_BE_PROTECTED, new AcceptSuggestedAccess(globalContext.getRefManager(), access), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) }; } } } return null; }
private static ProblemDescriptor createDescriptor( @NotNull PsiElement element, String template, InspectionManager manager, boolean onTheFly) { return manager.createProblemDescriptor( element, template, onTheFly, null, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL); }
public static void visitRefInDocTag( final PsiDocTag tag, final JavadocManager manager, final PsiElement context, final ArrayList<ProblemDescriptor> problems, final InspectionManager inspectionManager, final boolean onTheFly) { final String tagName = tag.getName(); final PsiDocTagValue value = tag.getValueElement(); if (value == null) return; final JavadocTagInfo info = manager.getTagInfo(tagName); if (info != null && !info.isValidInContext(context)) return; final String message = info == null || !info.isInline() ? null : info.checkTagValue(value); if (message != null) { problems.add(createDescriptor(value, message, inspectionManager, onTheFly)); } final PsiReference reference = value.getReference(); if (reference == null) return; final PsiElement element = reference.resolve(); if (element != null) return; final int textOffset = value.getTextOffset(); if (textOffset == value.getTextRange().getEndOffset()) return; final PsiDocTagValue valueElement = tag.getValueElement(); if (valueElement == null) return; final CharSequence paramName = value .getContainingFile() .getViewProvider() .getContents() .subSequence(textOffset, value.getTextRange().getEndOffset()); final String params = "<code>" + paramName + "</code>"; final List<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>(); if (onTheFly && "param".equals(tagName)) { final PsiDocCommentOwner commentOwner = PsiTreeUtil.getParentOfType(tag, PsiDocCommentOwner.class); if (commentOwner instanceof PsiMethod) { final PsiMethod method = (PsiMethod) commentOwner; final PsiParameter[] parameters = method.getParameterList().getParameters(); final PsiDocTag[] tags = tag.getContainingComment().getTags(); final Set<String> unboundParams = new HashSet<String>(); for (PsiParameter parameter : parameters) { if (!JavaDocLocalInspection.isFound(tags, parameter)) { unboundParams.add(parameter.getName()); } } if (!unboundParams.isEmpty()) { fixes.add(new RenameReferenceQuickFix(unboundParams)); } } } fixes.add(new RemoveTagFix(tagName, paramName)); problems.add( inspectionManager.createProblemDescriptor( valueElement, reference.getRangeInElement(), cannotResolveSymbolMessage(params), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, onTheFly, fixes.toArray(new LocalQuickFix[fixes.size()]))); }
public CommonProblemDescriptor[] checkElement( @NotNull final PsiElement psiElement, InspectionManager manager, Project project) { final Map<PsiElement, Collection<String>> suppressedScopes = new THashMap<PsiElement, Collection<String>>(); psiElement.accept( new JavaRecursiveElementWalkingVisitor() { @Override public void visitModifierList(PsiModifierList list) { super.visitModifierList(list); final PsiElement parent = list.getParent(); if (parent instanceof PsiModifierListOwner && !(parent instanceof PsiClass)) { checkElement(parent); } } @Override public void visitComment(PsiComment comment) { checkElement(comment); } @Override public void visitClass(PsiClass aClass) { if (aClass == psiElement) { super.visitClass(aClass); checkElement(aClass); } } private void checkElement(final PsiElement owner) { String idsString = SuppressManager.getInstance().getSuppressedInspectionIdsIn(owner); if (idsString != null && idsString.length() != 0) { List<String> ids = StringUtil.split(idsString, ","); if (IGNORE_ALL && (ids.contains(SuppressionUtil.ALL) || ids.contains(SuppressionUtil.ALL.toLowerCase()))) return; Collection<String> suppressed = suppressedScopes.get(owner); if (suppressed == null) { suppressed = ids; } else { for (String id : ids) { if (!suppressed.contains(id)) { suppressed.add(id); } } } suppressedScopes.put(owner, suppressed); } } }); if (suppressedScopes.values().isEmpty()) return null; // have to visit all file from scratch since inspections can be written in any perversive way // including checkFile() overriding Collection<InspectionTool> suppressedTools = new THashSet<InspectionTool>(); InspectionTool[] tools = getInspectionTools(psiElement, manager); for (Collection<String> ids : suppressedScopes.values()) { for (Iterator<String> iterator = ids.iterator(); iterator.hasNext(); ) { final String shortName = iterator.next().trim(); for (InspectionTool tool : tools) { if (tool instanceof LocalInspectionToolWrapper && ((LocalInspectionToolWrapper) tool).getTool().getID().equals(shortName)) { if (!((LocalInspectionToolWrapper) tool).isUnfair()) { suppressedTools.add(tool); } else { iterator.remove(); break; } } else if (tool.getShortName().equals(shortName)) { // ignore global unused as it won't be checked anyway if (!(tool instanceof LocalInspectionToolWrapper) && !(tool instanceof GlobalInspectionToolWrapper)) { iterator.remove(); break; } else { suppressedTools.add(tool); } } } } } final AnalysisScope scope = new AnalysisScope(psiElement.getContainingFile()); final InspectionManagerEx inspectionManagerEx = ((InspectionManagerEx) InspectionManager.getInstance(project)); GlobalInspectionContextImpl globalContext = inspectionManagerEx.createNewGlobalContext(false); globalContext.setCurrentScope(scope); final RefManagerImpl refManager = ((RefManagerImpl) globalContext.getRefManager()); refManager.inspectionReadActionStarted(); final List<ProblemDescriptor> result; try { result = new ArrayList<ProblemDescriptor>(); for (InspectionTool tool : suppressedTools) { String toolId = tool instanceof LocalInspectionToolWrapper ? ((LocalInspectionToolWrapper) tool).getTool().getID() : tool.getShortName(); tool.initialize(globalContext); Collection<CommonProblemDescriptor> descriptors; if (tool instanceof LocalInspectionToolWrapper) { LocalInspectionToolWrapper local = (LocalInspectionToolWrapper) tool; if (local.isUnfair()) continue; // cant't work with passes other than LocalInspectionPass local.processFile(psiElement.getContainingFile(), false, manager); descriptors = local.getProblemDescriptors(); } else if (tool instanceof GlobalInspectionToolWrapper) { GlobalInspectionToolWrapper global = (GlobalInspectionToolWrapper) tool; if (global.getTool().isGraphNeeded()) { refManager.findAllDeclarations(); } global.processFile(scope, manager, globalContext, false); descriptors = global.getProblemDescriptors(); } else { continue; } for (PsiElement suppressedScope : suppressedScopes.keySet()) { Collection<String> suppressedIds = suppressedScopes.get(suppressedScope); if (!suppressedIds.contains(toolId)) continue; for (CommonProblemDescriptor descriptor : descriptors) { if (!(descriptor instanceof ProblemDescriptor)) continue; PsiElement element = ((ProblemDescriptor) descriptor).getPsiElement(); if (element == null) continue; PsiElement annotation = SuppressManager.getInstance().getElementToolSuppressedIn(element, toolId); if (annotation != null && PsiTreeUtil.isAncestor(suppressedScope, annotation, false) || annotation == null && !PsiTreeUtil.isAncestor(suppressedScope, element, false)) { suppressedIds.remove(toolId); break; } } } } for (PsiElement suppressedScope : suppressedScopes.keySet()) { Collection<String> suppressedIds = suppressedScopes.get(suppressedScope); for (String toolId : suppressedIds) { PsiMember psiMember; String problemLine = null; if (suppressedScope instanceof PsiMember) { psiMember = (PsiMember) suppressedScope; } else { psiMember = PsiTreeUtil.getParentOfType(suppressedScope, PsiDocCommentOwner.class); final PsiStatement statement = PsiTreeUtil.getNextSiblingOfType(suppressedScope, PsiStatement.class); problemLine = statement != null ? statement.getText() : null; } if (psiMember != null && psiMember.isValid()) { String description = InspectionsBundle.message("inspection.redundant.suppression.description"); if (myQuickFixes == null) myQuickFixes = new BidirectionalMap<String, QuickFix>(); final String key = toolId + (problemLine != null ? ";" + problemLine : ""); QuickFix fix = myQuickFixes.get(key); if (fix == null) { fix = new RemoveSuppressWarningAction(toolId, problemLine); myQuickFixes.put(key, fix); } PsiElement identifier = null; if (psiMember instanceof PsiMethod) { identifier = ((PsiMethod) psiMember).getNameIdentifier(); } else if (psiMember instanceof PsiField) { identifier = ((PsiField) psiMember).getNameIdentifier(); } else if (psiMember instanceof PsiClass) { identifier = ((PsiClass) psiMember).getNameIdentifier(); } if (identifier == null) { identifier = psiMember; } result.add( manager.createProblemDescriptor( identifier, description, (LocalQuickFix) fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false)); } } } } finally { refManager.inspectionReadActionFinished(); globalContext.close(true); } return result.toArray(new ProblemDescriptor[result.size()]); }
@Override public void visitXmlAttributeValue(XmlAttributeValue value) { for (PsiReference reference : value.getReferences()) { if (!(reference instanceof OnClickConverter.MyReference)) { continue; } final OnClickConverter.MyReference ref = (OnClickConverter.MyReference) reference; final String methodName = ref.getValue(); if (methodName.isEmpty()) { continue; } final ResolveResult[] results = ref.multiResolve(false); final Set<PsiClass> resolvedClasses = new HashSet<PsiClass>(); final Set<PsiClass> resolvedClassesWithMistake = new HashSet<PsiClass>(); for (ResolveResult result : results) { if (result instanceof OnClickConverter.MyResolveResult) { final PsiElement element = result.getElement(); if (element != null) { final PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); if (aClass != null) { resolvedClasses.add(aClass); if (!((OnClickConverter.MyResolveResult) result).hasCorrectSignature()) { resolvedClassesWithMistake.add(aClass); } } } } } PsiClass activity = null; for (PsiClass relatedActivity : myRelatedActivities) { if (!containsOrExtends(resolvedClasses, relatedActivity)) { activity = relatedActivity; break; } else if (activity == null && containsOrExtends(resolvedClassesWithMistake, relatedActivity)) { activity = relatedActivity; } } if (activity != null) { reportMissingOnClickProblem( ref, activity, methodName, resolvedClassesWithMistake.contains(activity)); } else if (results.length == 0) { myResult.add( myInspectionManager.createProblemDescriptor( value, reference.getRangeInElement(), ProblemsHolder.unresolvedReferenceMessage(reference), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myOnTheFly)); } else if (resolvedClassesWithMistake.size() > 0) { reportMissingOnClickProblem( ref, resolvedClassesWithMistake.iterator().next(), methodName, true); } } }