private static void removeException(final RefMethod refMethod, final PsiType exceptionType, final List<PsiJavaCodeReferenceElement> refsToDelete, final PsiMethod psiMethod) { PsiManager psiManager = psiMethod.getManager(); PsiJavaCodeReferenceElement[] refs = psiMethod.getThrowsList().getReferenceElements(); for (PsiJavaCodeReferenceElement ref : refs) { PsiType refType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(ref); if (exceptionType.isAssignableFrom(refType)) { refsToDelete.add(ref); } } if (refMethod != null) { for (RefMethod refDerived : refMethod.getDerivedMethods()) { PsiModifierListOwner method = refDerived.getElement(); if (method != null) { removeException(refDerived, exceptionType, refsToDelete, (PsiMethod)method); } } } else { final Query<Pair<PsiMethod,PsiMethod>> query = AllOverridingMethodsSearch.search(psiMethod.getContainingClass()); query.forEach(new Processor<Pair<PsiMethod, PsiMethod>>(){ @Override public boolean process(final Pair<PsiMethod, PsiMethod> pair) { if (pair.first == psiMethod) { removeException(null, exceptionType, refsToDelete, pair.second); } return true; } }); } }
@Override public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethod.class, false); if (psiMethod != null) { final List<PsiElement> psiElements = new ArrayList<PsiElement>(); psiElements.add(psiMethod); if (Boolean.valueOf(myHint).booleanValue()) { final Query<Pair<PsiMethod, PsiMethod>> query = AllOverridingMethodsSearch.search(psiMethod.getContainingClass()); query.forEach(new Processor<Pair<PsiMethod, PsiMethod>>() { @Override public boolean process(final Pair<PsiMethod, PsiMethod> pair) { if (pair.first == psiMethod) { psiElements.add(pair.second); } return true; } }); } ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { SafeDeleteHandler.invoke(project, PsiUtilCore.toPsiElementArray(psiElements), false); } }, project.getDisposed()); } }
private static void removeException(final RefMethod refMethod, final PsiType exceptionType, final List<PsiJavaCodeReferenceElement> refsToDelete, final PsiMethod psiMethod) { PsiManager psiManager = psiMethod.getManager(); PsiJavaCodeReferenceElement[] refs = psiMethod.getThrowsList().getReferenceElements(); for (PsiJavaCodeReferenceElement ref : refs) { PsiType refType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(ref); if (exceptionType.isAssignableFrom(refType)) { refsToDelete.add(ref); } } if (refMethod != null) { for (RefMethod refDerived : refMethod.getDerivedMethods()) { removeException(refDerived, exceptionType, refsToDelete, (PsiMethod)refDerived.getElement()); } } else { final Query<Pair<PsiMethod,PsiMethod>> query = AllOverridingMethodsSearch.search(psiMethod.getContainingClass()); query.forEach(new Processor<Pair<PsiMethod, PsiMethod>>(){ @Override public boolean process(final Pair<PsiMethod, PsiMethod> pair) { if (pair.first == psiMethod) { removeException(null, exceptionType, refsToDelete, pair.second); } return true; } }); } }
private static void collectOverridingMethods(@NotNull final Set<PsiMethod> methods, @NotNull Collection<LineMarkerInfo> result) { final Set<PsiElement> overridden = new HashSet<PsiElement>(); Set<PsiClass> classes = new THashSet<PsiClass>(); for (PsiMethod method : methods) { ProgressManager.checkCanceled(); final PsiClass parentClass = method.getContainingClass(); if (parentClass != null && !CommonClassNames.JAVA_LANG_OBJECT.equals(parentClass.getQualifiedName())) { classes.add(parentClass); } } for (final PsiClass aClass : classes) { AllOverridingMethodsSearch.search(aClass).forEach(new Processor<Pair<PsiMethod, PsiMethod>>() { @Override public boolean process(final Pair<PsiMethod, PsiMethod> pair) { ProgressManager.checkCanceled(); final PsiMethod superMethod = pair.getFirst(); if (isCorrectTarget(superMethod) && isCorrectTarget(pair.getSecond())) { if (methods.remove(superMethod)) { overridden.add(PsiImplUtil.handleMirror(superMethod)); } } return !methods.isEmpty(); } }); } for (PsiElement element : overridden) { final Icon icon = AllIcons.Gutter.OverridenMethod; element = PsiImplUtil.handleMirror(element); PsiElement range; if (element instanceof GrNamedElement) { range = ((GrNamedElement)element).getNameIdentifierGroovy(); } else { range = element; } final MarkerType type = element instanceof GrField ? GroovyMarkerTypes.OVERRIDEN_PROPERTY_TYPE : GroovyMarkerTypes.GR_OVERRIDEN_METHOD; LineMarkerInfo info = new LineMarkerInfo<PsiElement>(range, range.getTextRange(), icon, Pass.UPDATE_OVERRIDEN_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.RIGHT); result.add(info); } }
private static void collectOverridingMethods(final Set<PsiMethod> methods, Collection<LineMarkerInfo> result) { final Set<PsiElement> overridden = new HashSet<PsiElement>(); Set<PsiClass> classes = new THashSet<PsiClass>(); for (PsiMethod method : methods) { ProgressManager.checkCanceled(); final PsiClass parentClass = method.getContainingClass(); if (parentClass != null && !CommonClassNames.JAVA_LANG_OBJECT.equals(parentClass.getQualifiedName())) { classes.add(parentClass); } } for (final PsiClass aClass : classes) { try { AllOverridingMethodsSearch.search(aClass).forEach(new Processor<Pair<PsiMethod, PsiMethod>>() { @Override public boolean process(final Pair<PsiMethod, PsiMethod> pair) { ProgressManager.checkCanceled(); final PsiMethod superMethod = pair.getFirst(); if (isCorrectTarget(superMethod) && isCorrectTarget(pair.getSecond())) { if (methods.remove(superMethod)) { if (superMethod instanceof PsiMirrorElement) { } overridden.add(PsiImplUtil.handleMirror(superMethod)); } } return !methods.isEmpty(); } }); } catch (IndexNotReadyException ignored) { } } for (PsiElement element : overridden) { final Icon icon = AllIcons.Gutter.OverridenMethod; element = PsiImplUtil.handleMirror(element); PsiElement range; if (element instanceof GrNamedElement) { range = ((GrNamedElement)element).getNameIdentifierGroovy(); } else { range = element; } final MarkerType type; if (element instanceof GrField) { type = GroovyMarkerTypes.OVERRIDEN_PROPERTY_TYPE; } else { type = GroovyMarkerTypes.OVERRIDEN_METHOD; } LineMarkerInfo info = new LineMarkerInfo<PsiElement>(range, range.getTextRange(), icon, Pass.UPDATE_OVERRIDEN_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.RIGHT); result.add(info); } }
private List<LineMarkerInfo> collectOverridingMethods(@NotNull final Iterable<PsiMethod> _methods, @NotNull PsiClass containingClass) { if(!myOverriddenOption.isEnabled() && !myImplementedOption.isEnabled()) { return Collections.emptyList(); } final Set<PsiMethod> overridden = new HashSet<>(); Set<PsiMethod> methodSet = ContainerUtil.newHashSet(_methods); AllOverridingMethodsSearch.search(containingClass).forEach(pair -> { ProgressManager.checkCanceled(); final PsiMethod superMethod = pair.getFirst(); if(methodSet.remove(superMethod)) { overridden.add(superMethod); } return !methodSet.isEmpty(); }); if(!methodSet.isEmpty()) { final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(containingClass); if(interfaceMethod != null && FunctionalExpressionSearch.search(containingClass).findFirst() != null) { overridden.add(interfaceMethod); } } List<LineMarkerInfo> result = new ArrayList<>(); for(PsiMethod method : overridden) { ProgressManager.checkCanceled(); boolean overrides = !method.hasModifierProperty(PsiModifier.ABSTRACT); if(overrides) { if(!myOverriddenOption.isEnabled()) { return Collections.emptyList(); } } else { if(!myImplementedOption.isEnabled()) { return Collections.emptyList(); } } PsiElement range = getMethodRange(method); final MarkerType type = MarkerType.OVERRIDDEN_METHOD; final Icon icon = overrides ? AllIcons.Gutter.OverridenMethod : AllIcons.Gutter.ImplementedMethod; LineMarkerInfo<PsiElement> info = new LineMarkerInfo<>(range, range.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment .RIGHT); NavigateAction.setNavigateAction(info, overrides ? "Go to overriding methods" : "Go to implementation(s)", IdeActions.ACTION_GOTO_IMPLEMENTATION); result.add(info); } return result; }