Search in sources :

Example 21 with MethodSignature

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

the class LambdaHighlightingUtil method checkInterfaceFunctional.

@Nullable
public static String checkInterfaceFunctional(PsiType functionalInterfaceType) {
    if (functionalInterfaceType instanceof PsiIntersectionType) {
        final Set<MethodSignature> signatures = new HashSet<>();
        for (PsiType type : ((PsiIntersectionType) functionalInterfaceType).getConjuncts()) {
            if (checkInterfaceFunctional(type) == null) {
                final MethodSignature signature = LambdaUtil.getFunction(PsiUtil.resolveClassInType(type));
                LOG.assertTrue(signature != null, type.getCanonicalText());
                signatures.add(signature);
            }
        }
        if (signatures.size() > 1) {
            return "Multiple non-overriding abstract methods found in " + functionalInterfaceType.getPresentableText();
        }
        return null;
    }
    final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
    final PsiClass aClass = resolveResult.getElement();
    if (aClass != null) {
        //should be logged as cyclic inference
        if (aClass instanceof PsiTypeParameter)
            return null;
        MethodSignature functionalMethod = LambdaUtil.getFunction(aClass);
        if (functionalMethod != null && functionalMethod.getTypeParameters().length > 0)
            return "Target method is generic";
        if (checkReturnTypeApplicable(resolveResult, aClass)) {
            return "No instance of type " + functionalInterfaceType.getPresentableText() + " exists so that lambda expression can be type-checked";
        }
        return checkInterfaceFunctional(aClass);
    }
    return functionalInterfaceType.getPresentableText() + " is not a functional interface";
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with MethodSignature

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

the class PsiNameValuePairImpl method getReference.

@Override
public PsiReference getReference() {
    return new PsiReference() {

        @Nullable
        private PsiClass getReferencedClass() {
            LOG.assertTrue(getParent() instanceof PsiAnnotationParameterList && getParent().getParent() instanceof PsiAnnotation);
            PsiAnnotation annotation = (PsiAnnotation) getParent().getParent();
            PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
            if (nameRef == null)
                return null;
            PsiElement target = nameRef.resolve();
            return target instanceof PsiClass ? (PsiClass) target : null;
        }

        @Override
        public PsiElement getElement() {
            PsiIdentifier nameIdentifier = getNameIdentifier();
            if (nameIdentifier != null) {
                return nameIdentifier;
            }
            return PsiNameValuePairImpl.this;
        }

        @Override
        public TextRange getRangeInElement() {
            PsiIdentifier id = getNameIdentifier();
            if (id != null) {
                return new TextRange(0, id.getTextLength());
            }
            return TextRange.EMPTY_RANGE;
        }

        @Override
        public PsiElement resolve() {
            PsiClass refClass = getReferencedClass();
            if (refClass == null)
                return null;
            String name = getName();
            if (name == null)
                name = PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME;
            MethodSignature signature = MethodSignatureUtil.createMethodSignature(name, PsiType.EMPTY_ARRAY, PsiTypeParameter.EMPTY_ARRAY, PsiSubstitutor.EMPTY);
            return MethodSignatureUtil.findMethodBySignature(refClass, signature, false);
        }

        @Override
        @NotNull
        public String getCanonicalText() {
            String name = getName();
            return name != null ? name : PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME;
        }

        @Override
        public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
            PsiIdentifier nameIdentifier = getNameIdentifier();
            if (nameIdentifier != null) {
                PsiImplUtil.setName(nameIdentifier, newElementName);
            } else if (ElementType.ANNOTATION_MEMBER_VALUE_BIT_SET.contains(getNode().getFirstChildNode().getElementType())) {
                PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
                nameIdentifier = factory.createIdentifier(newElementName);
                addBefore(nameIdentifier, SourceTreeToPsiMap.treeElementToPsi(getNode().getFirstChildNode()));
            }
            return PsiNameValuePairImpl.this;
        }

        @Override
        public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
            throw new IncorrectOperationException("Not implemented");
        }

        @Override
        public boolean isReferenceTo(PsiElement element) {
            return element instanceof PsiMethod && element.equals(resolve());
        }

        @Override
        @NotNull
        public Object[] getVariants() {
            return ArrayUtil.EMPTY_OBJECT_ARRAY;
        }

        @Override
        public boolean isSoft() {
            return false;
        }
    };
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) IncorrectOperationException(com.intellij.util.IncorrectOperationException) JavaStubPsiElement(com.intellij.psi.impl.source.JavaStubPsiElement)

Example 23 with MethodSignature

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

the class StubGenerator method collectMethods.

@Override
public Collection<PsiMethod> collectMethods(PsiClass typeDefinition) {
    List<PsiMethod> methods = new ArrayList<>();
    for (PsiMethod method : typeDefinition.getMethods()) {
        if (method instanceof DelegatedMethod) {
            PsiMethod prototype = ((DelegatedMethod) method).getPrototype();
            PsiClass aClass = prototype.getContainingClass();
            if (prototype.hasModifierProperty(PsiModifier.FINAL) && aClass != null && typeDefinition.isInheritor(aClass, true)) {
                //skip final super methods
                continue;
            }
        }
        methods.add(method);
    }
    boolean isClass = !typeDefinition.isInterface() && !typeDefinition.isAnnotationType() && !typeDefinition.isEnum() && !(typeDefinition instanceof GroovyScriptClass);
    if (isClass) {
        final Collection<MethodSignature> toOverride = OverrideImplementExploreUtil.getMethodSignaturesToOverride(typeDefinition);
        for (MethodSignature signature : toOverride) {
            if (!(signature instanceof MethodSignatureBackedByPsiMethod))
                continue;
            final PsiMethod method = ((MethodSignatureBackedByPsiMethod) signature).getMethod();
            final PsiClass baseClass = method.getContainingClass();
            if (baseClass == null)
                continue;
            final String qname = baseClass.getQualifiedName();
            if (GroovyCommonClassNames.GROOVY_OBJECT.equals(qname) || GroovyCommonClassNames.GROOVY_OBJECT_SUPPORT.equals(qname) || method.hasModifierProperty(PsiModifier.ABSTRACT) && typeDefinition.isInheritor(baseClass, true)) {
                if (method.isConstructor())
                    continue;
                methods.add(mirrorMethod(typeDefinition, method, baseClass, signature.getSubstitutor()));
            }
        }
        final Collection<MethodSignature> toImplement = OverrideImplementExploreUtil.getMethodSignaturesToImplement(typeDefinition);
        for (MethodSignature signature : toImplement) {
            if (!(signature instanceof MethodSignatureBackedByPsiMethod))
                continue;
            final PsiMethod resolved = ((MethodSignatureBackedByPsiMethod) signature).getMethod();
            final PsiClass baseClass = resolved.getContainingClass();
            if (baseClass == null)
                continue;
            if (!GroovyCommonClassNames.GROOVY_OBJECT.equals(baseClass.getQualifiedName()))
                continue;
            methods.add(mirrorMethod(typeDefinition, resolved, baseClass, signature.getSubstitutor()));
        }
    }
    return methods;
}
Also used : DelegatedMethod(org.jetbrains.plugins.groovy.lang.resolve.ast.DelegatedMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) MethodSignature(com.intellij.psi.util.MethodSignature) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod)

Example 24 with MethodSignature

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

the class TransformationContextImpl method doAddMethod.

private void doAddMethod(@NotNull PsiMethod method, boolean prepend) {
    if (method instanceof GrLightMethodBuilder) {
        ((GrLightMethodBuilder) method).setContainingClass(myCodeClass);
    } else if (method instanceof LightMethodBuilder) {
        ((LightMethodBuilder) method).setContainingClass(myCodeClass);
    }
    MethodSignature signature = method.getSignature(PsiSubstitutor.EMPTY);
    Set<MethodSignature> signatures = mySignaturesCache.get(method.getName());
    if (signatures.add(signature)) {
        if (prepend) {
            myMethods.addFirst(method);
        } else {
            myMethods.addLast(method);
        }
    }
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) LightMethodBuilder(com.intellij.psi.impl.light.LightMethodBuilder) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder)

Example 25 with MethodSignature

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

the class GrMethodConflictUtil method checkForClosurePropertySignatureOverload.

private static void checkForClosurePropertySignatureOverload(PsiClass clazz, GrMethod prototype, final GrMethod refactoredMethod, final MultiMap<PsiElement, String> conflicts, final List<MethodSignature> prototypeSignatures) {
    final boolean isStatic = prototype.hasModifierProperty(PsiModifier.STATIC);
    final String name = prototype.getName();
    if (!GroovyPropertyUtils.isProperty(clazz, name, isStatic))
        return;
    final PsiMethod getter = GroovyPropertyUtils.findPropertyGetter(clazz, name, isStatic, true);
    final PsiType returnType;
    if (getter instanceof GrMethod) {
        returnType = ((GrMethod) getter).getInferredReturnType();
    } else if (getter instanceof GrAccessorMethod) {
        returnType = ((GrAccessorMethod) getter).getInferredReturnType();
    } else {
        return;
    }
    if (!(returnType instanceof GrClosureType))
        return;
    final GrSignature signature = ((GrClosureType) returnType).getSignature();
    signature.accept(new GrRecursiveSignatureVisitor() {

        @Override
        public void visitClosureSignature(GrClosureSignature signature) {
            NextSignature: for (MethodSignature prototypeSignature : prototypeSignatures) {
                final GrClosureParameter[] params = signature.getParameters();
                final PsiType[] types = prototypeSignature.getParameterTypes();
                if (types.length != params.length)
                    continue;
                for (int i = 0; i < types.length; i++) {
                    if (!TypesUtil.isAssignableByMethodCallConversion(types[i], params[i].getType(), refactoredMethod.getParameterList())) {
                        continue NextSignature;
                    }
                }
                conflicts.putValue(getter, GroovyRefactoringBundle.message("refactored.method.will.cover.closure.property", name, RefactoringUIUtil.getDescription(getter.getContainingClass(), false)));
            }
        }
    });
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrRecursiveSignatureVisitor(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Aggregations

MethodSignature (com.intellij.psi.util.MethodSignature)31 Nullable (org.jetbrains.annotations.Nullable)8 NotNull (org.jetbrains.annotations.NotNull)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 com.intellij.psi (com.intellij.psi)3 List (java.util.List)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 TextRange (com.intellij.openapi.util.TextRange)2 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)2 Processor (com.intellij.util.Processor)2 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)2 GrClosureType (org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)2 OverrideImplementExploreUtil (com.intellij.codeInsight.generation.OverrideImplementExploreUtil)1 RedundantLambdaCodeBlockInspection (com.intellij.codeInspection.RedundantLambdaCodeBlockInspection)1 FindBundle (com.intellij.find.FindBundle)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1