private static List<ProblemDescriptor> runInspectionOnFile( @NotNull PsiFile file, @NotNull LocalInspectionTool inspectionTool) { InspectionManager inspectionManager = InspectionManager.getInstance(file.getProject()); GlobalInspectionContext context = inspectionManager.createNewGlobalContext(false); return InspectionEngine.runInspectionOnFile( file, new LocalInspectionToolWrapper(inspectionTool), context); }
@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}; }
@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 = ModuleUtilCore.findModuleForPsiElement(aClass); if (nameIdentifier == null || module == null || !PsiUtil.isInstantiable(aClass)) return null; final PsiClass base = JavaPsiFacade.getInstance(project).findClass(getClassName(), aClass.getResolveScope()); if (base == null || !aClass.isInheritor(base, true)) return null; String descriptionDir = DescriptionCheckerUtil.getDescriptionDirName(aClass); if (StringUtil.isEmptyOrSpaces(descriptionDir)) { return null; } for (PsiDirectory description : getDescriptionsDirs(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, getHasNotBeforeAfterError(), isOnTheFly, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false); return new ProblemDescriptor[] {problemDescriptor}; } return null; } } final PsiElement problem = aClass.getNameIdentifier(); final ProblemDescriptor problemDescriptor = manager.createProblemDescriptor( problem == null ? nameIdentifier : problem, getHasNotDescriptionError(), isOnTheFly, new LocalQuickFix[] {getFix(module, descriptionDir)}, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); return new ProblemDescriptor[] {problemDescriptor}; }
@Nullable @Override public CommonProblemDescriptor[] checkElement( @NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) { if (!(refEntity instanceof RefPackage)) { return null; } final RefPackage refPackage = (RefPackage) refEntity; final String packageName = refPackage.getQualifiedName(); final Project project = globalContext.getProject(); final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName); if (aPackage == null) { return null; } final List<RefEntity> children = refPackage.getChildren(); boolean hasClasses = false; for (RefEntity child : children) { if (child instanceof RefClass) { hasClasses = true; break; } } if (!hasClasses) { return null; } final PsiDirectory[] directories = aPackage.getDirectories(); for (PsiDirectory directory : directories) { final boolean packageInfoFound = directory.findFile(PsiPackage.PACKAGE_INFO_FILE) != null; final boolean packageDotHtmlFound = directory.findFile("package.html") != null; if (packageInfoFound || packageDotHtmlFound) { return null; } } if (PsiUtil.isLanguageLevel5OrHigher(aPackage)) { return new CommonProblemDescriptor[] { manager.createProblemDescriptor( InspectionGadgetsBundle.message("missing.package.info.problem.descriptor", packageName)) }; } else { return new CommonProblemDescriptor[] { manager.createProblemDescriptor( InspectionGadgetsBundle.message("missing.package.html.problem.descriptor", packageName)) }; } }
protected void runTool( final String testDir, final String jdkName, boolean runDeadCodeFirst, final InspectionTool tool, InspectionTool... additional) { final VirtualFile[] sourceDir = new VirtualFile[1]; ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { try { setupRootModel(testDir, sourceDir, jdkName); } catch (Exception e) { LOG.error(e); } } }); AnalysisScope scope = createAnalysisScope(sourceDir[0].getParent()); InspectionManagerEx inspectionManager = (InspectionManagerEx) InspectionManager.getInstance(getProject()); InspectionTool[] tools = runDeadCodeFirst ? new InspectionTool[] {new UnusedDeclarationInspection(), tool} : new InspectionTool[] {tool}; tools = ArrayUtil.mergeArrays(tools, additional); final GlobalInspectionContextImpl globalContext = CodeInsightTestFixtureImpl.createGlobalContextForTool( scope, getProject(), inspectionManager, tools); InspectionTestUtil.runTool(tool, scope, globalContext, inspectionManager); }
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)); }
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[] 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()]); }
@Override public void computeUsages(List<PsiLiteralExpression> targets) { final Project project = myTarget.getProject(); final PsiElement parent = myTarget.getParent().getParent(); final LocalInspectionsPass pass = new LocalInspectionsPass( myFile, myFile.getViewProvider().getDocument(), parent.getTextRange().getStartOffset(), parent.getTextRange().getEndOffset(), LocalInspectionsPass.EMPTY_PRIORITY_RANGE, false, HighlightInfoProcessor.getEmpty()); final InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile(); for (PsiLiteralExpression target : targets) { final Object value = target.getValue(); if (!(value instanceof String)) { continue; } InspectionToolWrapper toolWrapperById = ((InspectionProfileImpl) inspectionProfile).getToolById((String) value, target); if (!(toolWrapperById instanceof LocalInspectionToolWrapper)) { continue; } final LocalInspectionToolWrapper toolWrapper = ((LocalInspectionToolWrapper) toolWrapperById).createCopy(); final InspectionManagerEx managerEx = (InspectionManagerEx) InspectionManager.getInstance(project); final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false); toolWrapper.initialize(context); ((RefManagerImpl) context.getRefManager()).inspectionReadActionStarted(); ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator(); Runnable inspect = new Runnable() { @Override public void run() { pass.doInspectInBatch( context, managerEx, Collections.<LocalInspectionToolWrapper>singletonList(toolWrapper)); } }; if (indicator == null) { ProgressManager.getInstance() .executeProcessUnderProgress(inspect, new ProgressIndicatorBase()); } else { inspect.run(); } for (HighlightInfo info : pass.getInfos()) { final PsiElement element = CollectHighlightsUtil.findCommonParent(myFile, info.startOffset, info.endOffset); if (element != null) { addOccurrence(element); } } } }
private ProblemDescriptor generateProblemDescriptor( RegExpChar ch, InspectionManager manager, boolean isOnTheFly) { Character value = ch.getValue(); String msg = "Character class already contains char '" + value + "'"; return manager.createProblemDescriptor( ch, msg, isOnTheFly, new LocalQuickFix[] {new DeleteCharFix(ch)}, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); }
@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 protected void findProblems( @NotNull SoyFile file, @NotNull InspectionManager manager, boolean isOnTheFly, @NotNull List<ProblemDescriptor> problems) { PsiElementCollection elements = PATH_TO_MSG_TAGS_WITHOUT_DESC.navigate(file); for (PsiElement element : elements) { checkCanceled(); problems.add( manager.createProblemDescriptor( element, getMessage(), (LocalQuickFix) null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true)); } }
@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; }
@NotNull public static InspectionResultsView showOfflineView( @NotNull Project project, @NotNull Map<String, Map<String, Set<OfflineProblemDescriptor>>> resMap, @NotNull InspectionProfileImpl inspectionProfile, @NotNull String title) { final AnalysisScope scope = new AnalysisScope(project); final InspectionManagerEx managerEx = (InspectionManagerEx) InspectionManager.getInstance(project); final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false); context.setExternalProfile(inspectionProfile); context.setCurrentScope(scope); context.initializeTools(new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); final InspectionResultsView view = new InspectionResultsView(context, new OfflineInspectionRVContentProvider(resMap, project)); ((RefManagerImpl) context.getRefManager()).startOfflineView(); context.addView(view, title, true); view.update(); return view; }
private Map<ProblemDescriptor, HighlightDisplayLevel> runXmlFileSchemaValidation( @NotNull XmlFile xmlFile) { final AnnotationHolderImpl holder = new AnnotationHolderImpl(new AnnotationSession(xmlFile)); final List<ExternalAnnotator> annotators = ExternalLanguageAnnotators.allForFile(StdLanguages.XML, xmlFile); for (ExternalAnnotator<?, ?> annotator : annotators) { processAnnotator(xmlFile, holder, annotator); } if (!holder.hasAnnotations()) return Collections.emptyMap(); Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap = new LinkedHashMap<ProblemDescriptor, HighlightDisplayLevel>(); for (final Annotation annotation : holder) { final HighlightInfo info = HighlightInfo.fromAnnotation(annotation); if (info.getSeverity() == HighlightSeverity.INFORMATION) continue; final PsiElement startElement = xmlFile.findElementAt(info.startOffset); final PsiElement endElement = info.startOffset == info.endOffset ? startElement : xmlFile.findElementAt(info.endOffset - 1); if (startElement == null || endElement == null) continue; final ProblemDescriptor descriptor = myInspectionManager.createProblemDescriptor( startElement, endElement, info.getDescription(), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false); final HighlightDisplayLevel level = info.getSeverity() == HighlightSeverity.ERROR ? HighlightDisplayLevel.ERROR : HighlightDisplayLevel.WARNING; problemsMap.put(descriptor, level); } return problemsMap; }
@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; }
public InspectionManagerEx getManager() { return (InspectionManagerEx) InspectionManager.getInstance(myProject); }
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()]))); }
@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; }
@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); } } }
private static void runInspection( @NotNull Project project, @NotNull InspectionToolWrapper toolWrapper, @Nullable VirtualFile virtualFile, PsiElement psiElement, PsiFile psiFile) { final InspectionManagerEx managerEx = (InspectionManagerEx) InspectionManager.getInstance(project); final Module module = virtualFile != null ? ModuleUtilCore.findModuleForFile(virtualFile, project) : null; AnalysisScope analysisScope = null; if (psiFile != null) { analysisScope = new AnalysisScope(psiFile); } else { if (virtualFile != null && virtualFile.isDirectory()) { final PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(virtualFile); if (psiDirectory != null) { analysisScope = new AnalysisScope(psiDirectory); } } if (analysisScope == null && virtualFile != null) { analysisScope = new AnalysisScope(project, Arrays.asList(virtualFile)); } if (analysisScope == null) { analysisScope = new AnalysisScope(project); } } final FileFilterPanel fileFilterPanel = new FileFilterPanel(); fileFilterPanel.init(); final BaseAnalysisActionDialog dlg = new BaseAnalysisActionDialog( AnalysisScopeBundle.message( "specify.analysis.scope", InspectionsBundle.message("inspection.action.title")), AnalysisScopeBundle.message( "analysis.scope.title", InspectionsBundle.message("inspection.action.noun")), project, analysisScope, module != null ? module.getName() : null, true, AnalysisUIOptions.getInstance(project), psiElement) { @Override protected JComponent getAdditionalActionSettings(Project project) { return fileFilterPanel.getPanel(); } @Override public SearchScope getCustomScope() { return PsiSearchScopeUtil.union( fileFilterPanel.getSearchScope(), super.getCustomScope()); } }; AnalysisScope scope = analysisScope; dlg.show(); if (!dlg.isOK()) return; final AnalysisUIOptions uiOptions = AnalysisUIOptions.getInstance(project); scope = dlg.getScope(uiOptions, scope, project, module); RunInspectionIntention.rerunInspection(toolWrapper, managerEx, scope, psiFile); }