@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 static Collection<PsiClass> getClassesByAnnotation( String annotationName, Project project, GlobalSearchScope scope) { Collection<PsiClass> classes = Sets.newHashSet(); Collection<PsiAnnotation> annotations = JavaAnnotationIndex.getInstance().get(annotationName, project, scope); for (PsiAnnotation annotation : annotations) { PsiModifierList modifierList = (PsiModifierList) annotation.getParent(); PsiElement owner = modifierList.getParent(); if (owner instanceof PsiClass) { classes.add((PsiClass) owner); } } return classes; }
@Nullable @Override protected InspectionGadgetsFix buildFix(Object... infos) { if (infos.length == 1 && infos[0] instanceof PsiAnnotation) { final PsiAnnotation annotation = (PsiAnnotation) infos[0]; final Collection<String> ids = JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation( (PsiModifierList) annotation.getParent()); if (!ids.isEmpty()) { return new InspectionGadgetsFix() { @Override protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement psiElement = descriptor.getPsiElement(); if (psiElement instanceof PsiAnnotation) { final Collection<String> ids = JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation( (PsiModifierList) psiElement.getParent()); for (String id : ids) { if (!myAllowedSuppressions.contains(id)) { myAllowedSuppressions.add(id); } } saveProfile(project); } } private void saveProfile(Project project) { final InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile(); InspectionProfileManager.getInstance().fireProfileChanged(inspectionProfile); } @NotNull @Override public String getName() { return "Allow these suppressions"; } @NotNull @Override public String getFamilyName() { return "Allow suppressions"; } }; } } return null; }
@PsiUtil.AccessLevel private static int getEffectiveLevel( @NotNull PsiElement element, @NotNull PsiFile file, @NotNull PsiFile memberFile, PsiClass memberClass, PsiPackage memberPackage) { PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); if (memberClass != null && PsiTreeUtil.isAncestor(aClass, memberClass, false) || aClass != null && PsiTreeUtil.isAncestor(memberClass, aClass, false)) { // access from the same file can be via private // except when used in annotation: // @Ann(value = C.VAL) class C { public static final String VAL = "xx"; } PsiAnnotation annotation = PsiTreeUtil.getParentOfType(element, PsiAnnotation.class); if (annotation != null && annotation.getParent() instanceof PsiModifierList && annotation.getParent().getParent() == aClass) { return PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL; } return PsiUtil.ACCESS_LEVEL_PRIVATE; } // if (file == memberFile) { // return PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL; // } PsiDirectory directory = file.getContainingDirectory(); PsiPackage aPackage = directory == null ? null : JavaDirectoryService.getInstance().getPackage(directory); if (aPackage == memberPackage || aPackage != null && memberPackage != null && Comparing.strEqual( aPackage.getQualifiedName(), memberPackage.getQualifiedName())) { return PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL; } if (aClass != null && memberClass != null && aClass.isInheritor(memberClass, true)) { // access from subclass can be via protected, except for constructors PsiElement resolved = element instanceof PsiReference ? ((PsiReference) element).resolve() : null; boolean isConstructor = resolved instanceof PsiClass && element.getParent() instanceof PsiNewExpression || resolved instanceof PsiMethod && ((PsiMethod) resolved).isConstructor(); if (!isConstructor) { return PsiUtil.ACCESS_LEVEL_PROTECTED; } } return PsiUtil.ACCESS_LEVEL_PUBLIC; }