Search in sources :

Example 6 with MethodSignatureBackedByPsiMethod

use of com.intellij.psi.util.MethodSignatureBackedByPsiMethod in project intellij-community by JetBrains.

the class JavaLineMarkerProvider method getLineMarkerInfo.

@Override
@Nullable
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
    PsiElement parent;
    if (element instanceof PsiIdentifier && (parent = element.getParent()) instanceof PsiMethod) {
        if (!myOverridingOption.isEnabled() && !myImplementingOption.isEnabled())
            return null;
        PsiMethod method = (PsiMethod) parent;
        MethodSignatureBackedByPsiMethod superSignature = SuperMethodsSearch.search(method, null, true, false).findFirst();
        if (superSignature != null) {
            boolean overrides = method.hasModifierProperty(PsiModifier.ABSTRACT) == superSignature.getMethod().hasModifierProperty(PsiModifier.ABSTRACT);
            final Icon icon;
            if (overrides) {
                if (!myOverridingOption.isEnabled())
                    return null;
                icon = AllIcons.Gutter.OverridingMethod;
            } else {
                if (!myImplementingOption.isEnabled())
                    return null;
                icon = AllIcons.Gutter.ImplementingMethod;
            }
            return createSuperMethodLineMarkerInfo(element, icon, Pass.LINE_MARKERS);
        }
    }
    final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(element);
    final PsiElement firstChild = element.getFirstChild();
    if (interfaceMethod != null && firstChild != null && myLambdaOption.isEnabled()) {
        return createSuperMethodLineMarkerInfo(firstChild, AllIcons.Gutter.ImplementingFunctionalInterface, Pass.LINE_MARKERS);
    }
    if (myDaemonSettings.SHOW_METHOD_SEPARATORS && firstChild == null) {
        PsiElement element1 = element;
        boolean isMember = false;
        while (element1 != null && !(element1 instanceof PsiFile) && element1.getPrevSibling() == null) {
            element1 = element1.getParent();
            if (element1 instanceof PsiMember) {
                isMember = true;
                break;
            }
        }
        if (isMember && !(element1 instanceof PsiAnonymousClass || element1.getParent() instanceof PsiAnonymousClass)) {
            PsiFile file = element1.getContainingFile();
            Document document = file == null ? null : PsiDocumentManager.getInstance(file.getProject()).getLastCommittedDocument(file);
            boolean drawSeparator = false;
            if (document != null) {
                CharSequence documentChars = document.getCharsSequence();
                int category = getCategory(element1, documentChars);
                for (PsiElement child = element1.getPrevSibling(); child != null; child = child.getPrevSibling()) {
                    int category1 = getCategory(child, documentChars);
                    if (category1 == 0)
                        continue;
                    drawSeparator = category != 1 || category1 != 1;
                    break;
                }
            }
            if (drawSeparator) {
                LineMarkerInfo info = new LineMarkerInfo<>(element, element.getTextRange(), null, Pass.LINE_MARKERS, FunctionUtil.<Object, String>nullConstant(), null, GutterIconRenderer.Alignment.RIGHT);
                EditorColorsScheme scheme = myColorsManager.getGlobalScheme();
                info.separatorColor = scheme.getColor(CodeInsightColors.METHOD_SEPARATORS_COLOR);
                info.separatorPlacement = SeparatorPlacement.TOP;
                return info;
            }
        }
    }
    return null;
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) Document(com.intellij.openapi.editor.Document) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with MethodSignatureBackedByPsiMethod

use of com.intellij.psi.util.MethodSignatureBackedByPsiMethod in project intellij-community by JetBrains.

the class MethodSuperSearcher method addSuperMethods.

private static boolean addSuperMethods(final HierarchicalMethodSignature signature, final PsiMethod method, final PsiClass parentClass, final boolean allowStaticMethod, final boolean checkBases, final Processor<MethodSignatureBackedByPsiMethod> consumer) {
    PsiMethod signatureMethod = signature.getMethod();
    PsiClass hisClass = signatureMethod.getContainingClass();
    if (parentClass == null || InheritanceUtil.isInheritorOrSelf(parentClass, hisClass, true)) {
        if (isAcceptable(signatureMethod, method, allowStaticMethod)) {
            if (parentClass != null && !parentClass.equals(hisClass) && !checkBases) {
                return true;
            }
            // method != method.getsuper()
            LOG.assertTrue(signatureMethod != method, method);
            //no need to check super classes
            return consumer.process(signature);
        }
    }
    for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
        if (MethodSignatureUtil.isSubsignature(superSignature, signature)) {
            addSuperMethods(superSignature, method, parentClass, allowStaticMethod, checkBases, consumer);
        }
    }
    return true;
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod)

Example 8 with MethodSignatureBackedByPsiMethod

use of com.intellij.psi.util.MethodSignatureBackedByPsiMethod in project intellij-community by JetBrains.

the class MethodSuperSearcher method execute.

@Override
public boolean execute(@NotNull final SuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<MethodSignatureBackedByPsiMethod> consumer) {
    final PsiClass parentClass = queryParameters.getPsiClass();
    final PsiMethod method = queryParameters.getMethod();
    return ApplicationManager.getApplication().runReadAction((Computable<Boolean>) () -> {
        HierarchicalMethodSignature signature = method.getHierarchicalMethodSignature();
        final boolean checkBases = queryParameters.isCheckBases();
        final boolean allowStaticMethod = queryParameters.isAllowStaticMethod();
        final List<HierarchicalMethodSignature> supers = signature.getSuperSignatures();
        for (HierarchicalMethodSignature superSignature : supers) {
            if (MethodSignatureUtil.isSubsignature(superSignature, signature)) {
                if (!addSuperMethods(superSignature, method, parentClass, allowStaticMethod, checkBases, consumer))
                    return false;
            }
        }
        return true;
    });
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) List(java.util.List)

Example 9 with MethodSignatureBackedByPsiMethod

use of com.intellij.psi.util.MethodSignatureBackedByPsiMethod in project intellij-community by JetBrains.

the class AnnotateMethodFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    PsiMethod method = PsiTreeUtil.getParentOfType(psiElement, PsiMethod.class);
    if (method == null)
        return;
    final List<PsiMethod> toAnnotate = new ArrayList<>();
    toAnnotate.add(method);
    List<MethodSignatureBackedByPsiMethod> superMethodSignatures = method.findSuperMethodSignaturesIncludingStatic(true);
    for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
        PsiMethod superMethod = superMethodSignature.getMethod();
        if (!AnnotationUtil.isAnnotated(superMethod, myAnnotation, false, false) && superMethod.getManager().isInProject(superMethod)) {
            int ret = shouldAnnotateBaseMethod(method, superMethod, project);
            if (ret != 0 && ret != 1)
                return;
            if (ret == 0) {
                toAnnotate.add(superMethod);
            }
        }
    }
    if (annotateOverriddenMethods()) {
        PsiMethod[] methods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
        for (PsiMethod psiMethod : methods) {
            if (AnnotationUtil.isAnnotatingApplicable(psiMethod, myAnnotation) && !AnnotationUtil.isAnnotated(psiMethod, myAnnotation, false, false) && psiMethod.getManager().isInProject(psiMethod)) {
                toAnnotate.add(psiMethod);
            }
        }
    }
    FileModificationService.getInstance().preparePsiElementsForWrite(toAnnotate);
    for (PsiMethod psiMethod : toAnnotate) {
        annotateMethod(psiMethod);
    }
    UndoUtil.markPsiFileForUndo(method.getContainingFile());
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) PsiMethod(com.intellij.psi.PsiMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 10 with MethodSignatureBackedByPsiMethod

use of com.intellij.psi.util.MethodSignatureBackedByPsiMethod in project intellij-community by JetBrains.

the class JavaArrangementVisitor method visitMethod.

@Override
public void visitMethod(PsiMethod method) {
    boolean isSectionCommentsDetected = registerSectionComments(method);
    final TextRange range = isSectionCommentsDetected ? getElementRangeWithoutComments(method) : method.getTextRange();
    ArrangementSettingsToken type = method.isConstructor() ? CONSTRUCTOR : METHOD;
    JavaElementArrangementEntry entry = createNewEntry(method, range, type, method.getName(), true);
    if (entry == null) {
        return;
    }
    processEntry(entry, method, method.getBody());
    parseProperties(method, entry);
    myInfo.onMethodEntryCreated(method, entry);
    MethodSignatureBackedByPsiMethod overridden = SuperMethodsSearch.search(method, null, true, false).findFirst();
    if (overridden != null) {
        entry.addModifier(OVERRIDDEN);
        myInfo.onOverriddenMethod(overridden.getMethod(), method);
    }
    boolean reset = myMethodBodyProcessor.setBaseMethod(method);
    try {
        method.accept(myMethodBodyProcessor);
    } finally {
        if (reset) {
            myMethodBodyProcessor.setBaseMethod(null);
        }
    }
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) ArrangementSettingsToken(com.intellij.psi.codeStyle.arrangement.std.ArrangementSettingsToken) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

MethodSignatureBackedByPsiMethod (com.intellij.psi.util.MethodSignatureBackedByPsiMethod)11 Document (com.intellij.openapi.editor.Document)2 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)2 MethodSignature (com.intellij.psi.util.MethodSignature)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Nullable (org.jetbrains.annotations.Nullable)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)1 MarkerType (com.intellij.codeInsight.daemon.impl.MarkerType)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1 PsiElement (com.intellij.psi.PsiElement)1 PsiMethod (com.intellij.psi.PsiMethod)1 ArrangementSettingsToken (com.intellij.psi.codeStyle.arrangement.std.ArrangementSettingsToken)1 SuperMethodsSearch (com.intellij.psi.search.searches.SuperMethodsSearch)1 TypeConversionUtil (com.intellij.psi.util.TypeConversionUtil)1 Processor (com.intellij.util.Processor)1