Search in sources :

Example 1 with MethodSignatureBackedByPsiMethod

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

the class GroovyLineMarkerProvider method getLineMarkerInfo.

@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
    final PsiElement parent = element.getParent();
    if (parent instanceof PsiNameIdentifierOwner) {
        if (parent instanceof GrField && element == ((GrField) parent).getNameIdentifierGroovy()) {
            for (GrAccessorMethod method : GroovyPropertyUtils.getFieldAccessors((GrField) parent)) {
                MethodSignatureBackedByPsiMethod superSignature = SuperMethodsSearch.search(method, null, true, false).findFirst();
                if (superSignature != null) {
                    PsiMethod superMethod = superSignature.getMethod();
                    boolean overrides = method.hasModifierProperty(PsiModifier.ABSTRACT) == superMethod.hasModifierProperty(PsiModifier.ABSTRACT) || superMethod.getBody() != null && GrTraitUtil.isTrait(superMethod.getContainingClass());
                    final Icon icon = overrides ? AllIcons.Gutter.OverridingMethod : AllIcons.Gutter.ImplementingMethod;
                    final MarkerType type = GroovyMarkerTypes.OVERRIDING_PROPERTY_TYPE;
                    return new LineMarkerInfo<>(element, element.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.LEFT);
                }
            }
        } else if (parent instanceof GrMethod && element == ((GrMethod) parent).getNameIdentifierGroovy() && hasSuperMethods((GrMethod) element.getParent())) {
            final Icon icon = AllIcons.Gutter.OverridingMethod;
            final MarkerType type = GroovyMarkerTypes.GR_OVERRIDING_METHOD;
            return new LineMarkerInfo<>(element, element.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.LEFT);
        }
    }
    //need to draw method separator above docComment
    if (myDaemonSettings.SHOW_METHOD_SEPARATORS && element.getFirstChild() == null) {
        PsiElement element1 = element;
        boolean isMember = false;
        while (element1 != null && !(element1 instanceof PsiFile) && element1.getPrevSibling() == null) {
            element1 = element1.getParent();
            if (element1 instanceof PsiMember || element1 instanceof GrVariableDeclarationImpl) {
                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 = getGroovyCategory(element1, documentChars);
                for (PsiElement child = element1.getPrevSibling(); child != null; child = child.getPrevSibling()) {
                    int category1 = getGroovyCategory(child, documentChars);
                    if (category1 == 0)
                        continue;
                    drawSeparator = category != 1 || category1 != 1;
                    break;
                }
            }
            if (drawSeparator) {
                GrDocComment comment = null;
                if (element1 instanceof GrDocCommentOwner) {
                    comment = ((GrDocCommentOwner) element1).getDocComment();
                }
                LineMarkerInfo info = new LineMarkerInfo<>(element, comment != null ? comment.getTextRange() : 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 : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Document(com.intellij.openapi.editor.Document) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) MarkerType(com.intellij.codeInsight.daemon.impl.MarkerType) GrVariableDeclarationImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme)

Example 2 with MethodSignatureBackedByPsiMethod

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

the class StubGenerator method writeMethod.

@Override
public void writeMethod(StringBuilder text, PsiMethod method) {
    if (method == null)
        return;
    String name = method.getName();
    if (!PsiNameHelper.getInstance(method.getProject()).isIdentifier(name)) {
        //does not have a java image
        return;
    }
    boolean isAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT);
    PsiModifierList modifierList = method.getModifierList();
    ModifierListGenerator.writeModifiers(text, modifierList, STUB_MODIFIERS, false);
    if (method.hasTypeParameters()) {
        GenerationUtil.writeTypeParameters(text, method, classNameProvider);
        text.append(' ');
    }
    //append return type
    PsiType retType = method.getReturnType();
    if (retType == null) {
        retType = TypesUtil.getJavaLangObject(method);
    }
    if (!method.hasModifierProperty(PsiModifier.STATIC)) {
        final List<MethodSignatureBackedByPsiMethod> superSignatures = method.findSuperMethodSignaturesIncludingStatic(true);
        for (MethodSignatureBackedByPsiMethod superSignature : superSignatures) {
            final PsiType superType = superSignature.getSubstitutor().substitute(superSignature.getMethod().getReturnType());
            if (superType != null && !superType.isAssignableFrom(retType) && !(PsiUtil.resolveClassInType(superType) instanceof PsiTypeParameter)) {
                retType = superType;
            }
        }
    }
    TypeWriter.writeType(text, retType, method, classNameProvider);
    text.append(' ');
    text.append(name);
    GenerationUtil.writeParameterList(text, method.getParameterList().getParameters(), classNameProvider, null);
    writeThrowsList(text, method);
    if (!isAbstract && !method.hasModifierProperty(PsiModifier.NATIVE)) {
        /************* body **********/
        text.append("{\nreturn ");
        text.append(GroovyToJavaGenerator.getDefaultValueText(retType.getCanonicalText()));
        text.append(";\n}");
    } else {
        text.append(';');
    }
    text.append('\n');
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod)

Example 3 with MethodSignatureBackedByPsiMethod

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

the class DeprecationInspection method checkMethodOverridesDeprecated.

private static void checkMethodOverridesDeprecated(MethodSignatureBackedByPsiMethod methodSignature, List<MethodSignatureBackedByPsiMethod> superMethodSignatures, boolean ignoreAbstractDeprecatedOverrides, ProblemsHolder holder) {
    PsiMethod method = methodSignature.getMethod();
    PsiElement methodName = method.getNameIdentifier();
    if (methodName == null)
        return;
    for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
        PsiMethod superMethod = superMethodSignature.getMethod();
        PsiClass aClass = superMethod.getContainingClass();
        if (aClass == null)
            continue;
        // do not show deprecated warning for class implementing deprecated methods
        if (ignoreAbstractDeprecatedOverrides && !aClass.isDeprecated() && superMethod.hasModifierProperty(PsiModifier.ABSTRACT))
            continue;
        if (superMethod.isDeprecated()) {
            String description = JavaErrorMessages.message("overrides.deprecated.method", HighlightMessageUtil.getSymbolName(aClass));
            holder.registerProblem(methodName, description, ProblemHighlightType.LIKE_DEPRECATED);
        }
    }
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod)

Example 4 with MethodSignatureBackedByPsiMethod

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

the class PsiMethodTreeElement method getLocationString.

@Override
public String getLocationString() {
    if (!Registry.is("show.method.base.class.in.java.file.structure"))
        return null;
    final PsiMethod method = getElement();
    if (myLocation == null && method != null && !DumbService.isDumb(method.getProject())) {
        if (isInherited()) {
            return super.getLocationString();
        } else {
            try {
                final MethodSignatureBackedByPsiMethod baseMethod = SuperMethodsSearch.search(method, null, true, false).findFirst();
                if (baseMethod != null && !method.isEquivalentTo(baseMethod.getMethod())) {
                    PsiMethod base = baseMethod.getMethod();
                    PsiClass baseClass = base.getContainingClass();
                    if (baseClass != null) /*&& !CommonClassNames.JAVA_LANG_OBJECT.equals(baseClass.getQualifiedName())*/
                    {
                        if (baseClass.getMethods().length > 1) {
                            myLocation = baseClass.getName();
                        }
                    }
                }
            } catch (IndexNotReadyException e) {
            //some searchers (EJB) require indices. What shall we do?
            }
            if (StringUtil.isEmpty(myLocation)) {
                myLocation = "";
            } else {
                char upArrow = '↑';
                myLocation = UIUtil.getLabelFont().canDisplay(upArrow) ? upArrow + myLocation : myLocation;
            }
        }
    }
    return StringUtil.isEmpty(myLocation) ? null : myLocation;
}
Also used : MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException)

Example 5 with MethodSignatureBackedByPsiMethod

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

the class GDKSuperMethodSearcher method execute.

@Override
public boolean execute(@NotNull SuperMethodsSearch.SearchParameters queryParameters, @NotNull Processor<MethodSignatureBackedByPsiMethod> consumer) {
    final PsiMethod method = queryParameters.getMethod();
    if (!(method instanceof GrMethod)) {
        return true;
    }
    if (method.hasModifierProperty(PsiModifier.STATIC))
        return true;
    final PsiClass psiClass = method.getContainingClass();
    if (psiClass == null)
        return true;
    final HierarchicalMethodSignature hierarchicalSignature = method.getHierarchicalMethodSignature();
    if (!hierarchicalSignature.getSuperSignatures().isEmpty())
        return true;
    final Project project = method.getProject();
    final String name = method.getName();
    final MethodResolverProcessor processor = new MethodResolverProcessor(name, ((GrMethod) method), false, null, null, PsiType.EMPTY_ARRAY);
    ResolveUtil.processNonCodeMembers(JavaPsiFacade.getElementFactory(project).createType(psiClass), processor, (GrMethod) method, ResolveState.initial());
    final GroovyResolveResult[] candidates = processor.getCandidates();
    final PsiManager psiManager = PsiManager.getInstance(project);
    final MethodSignature signature = method.getHierarchicalMethodSignature();
    List<PsiMethod> goodSupers = new ArrayList<>();
    for (GroovyResolveResult candidate : candidates) {
        final PsiElement element = candidate.getElement();
        if (element instanceof PsiMethod) {
            final PsiMethod m = (PsiMethod) element;
            if (!isTheSameMethod(method, psiManager, m) && PsiImplUtil.isExtendsSignature(m.getHierarchicalMethodSignature(), signature)) {
                goodSupers.add(m);
            }
        }
    }
    if (goodSupers.isEmpty())
        return true;
    List<PsiMethod> result = new ArrayList<>(goodSupers.size());
    result.add(goodSupers.get(0));
    final Comparator<PsiMethod> comparator = (o1, o2) -> {
        //compare by first parameter type
        final PsiType type1 = getRealType(o1);
        final PsiType type2 = getRealType(o2);
        if (TypesUtil.isAssignableByMethodCallConversion(type1, type2, o1)) {
            return -1;
        } else if (TypesUtil.isAssignableByMethodCallConversion(type2, type1, o1)) {
            return 1;
        }
        return 0;
    };
    Outer: for (PsiMethod current : goodSupers) {
        for (Iterator<PsiMethod> i = result.iterator(); i.hasNext(); ) {
            PsiMethod m = i.next();
            final int res = comparator.compare(m, current);
            if (res > 0) {
                continue Outer;
            } else if (res < 0) {
                i.remove();
            }
        }
        result.add(current);
    }
    for (PsiMethod psiMethod : result) {
        if (!consumer.process(getRealMethod(psiMethod).getHierarchicalMethodSignature())) {
            return false;
        }
    }
    return true;
}
Also used : TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) Iterator(java.util.Iterator) PsiImplUtil(org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil) QueryExecutor(com.intellij.util.QueryExecutor) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) TypesUtil(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil) ResolveUtil(org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) MethodSignature(com.intellij.psi.util.MethodSignature) SuperMethodsSearch(com.intellij.psi.search.searches.SuperMethodsSearch) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) MethodSignature(com.intellij.psi.util.MethodSignature) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Iterator(java.util.Iterator) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor)

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