Search in sources :

Example 1 with MethodResolverProcessor

use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.

the class RenameGrFieldProcessor method findCollisions.

@Override
public void findCollisions(PsiElement element, String newName, Map<? extends PsiElement, String> allRenames, List<UsageInfo> result) {
    List<UsageInfo> collisions = new ArrayList<>();
    for (UsageInfo info : result) {
        if (!(info instanceof MoveRenameUsageInfo))
            continue;
        final PsiElement infoElement = info.getElement();
        final PsiElement referencedElement = ((MoveRenameUsageInfo) info).getReferencedElement();
        if (!(infoElement instanceof GrReferenceExpression))
            continue;
        final GrReferenceExpression refExpr = (GrReferenceExpression) infoElement;
        if (!(referencedElement instanceof GrField || refExpr.advancedResolve().isInvokedOnProperty()))
            continue;
        if (!(refExpr.getParent() instanceof GrCall))
            continue;
        final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
        final PsiType[] typeArguments = refExpr.getTypeArguments();
        final MethodResolverProcessor processor = new MethodResolverProcessor(newName, refExpr, false, null, argTypes, typeArguments);
        final PsiMethod resolved = ResolveUtil.resolveExistingElement(refExpr, processor, PsiMethod.class);
        if (resolved == null)
            continue;
        collisions.add(new UnresolvableCollisionUsageInfo(resolved, refExpr) {

            @Override
            public String getDescription() {
                return GroovyRefactoringBundle.message("usage.will.be.overriden.by.method", refExpr.getParent().getText(), PsiFormatUtil.formatMethod(resolved, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME, PsiFormatUtilBase.SHOW_TYPE));
            }
        });
    }
    result.addAll(collisions);
    super.findCollisions(element, newName, allRenames, result);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) ArrayList(java.util.ArrayList) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

Example 2 with MethodResolverProcessor

use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.

the class GenerationUtil method suggestMethodName.

public static String suggestMethodName(GroovyPsiElement place, String initialName, ExpressionContext context) {
    int count = 0;
    String name = initialName;
    Class[] classes = { PsiMethod.class };
    final Map<PsiMethod, String> setters = context.getSetters();
    while (setters.containsValue(name) || ResolveUtil.resolveExistingElement(place, new MethodResolverProcessor(name, place, false, null, null, null, true), classes) != null) {
        name = initialName + count;
        count++;
    }
    return name;
}
Also used : MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor)

Example 3 with MethodResolverProcessor

use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor 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)

Example 4 with MethodResolverProcessor

use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.

the class GrConstructorInvocationImpl method multiResolve.

@Override
@NotNull
public GroovyResolveResult[] multiResolve(boolean incompleteCode) {
    PsiClass clazz = getDelegatedClass();
    if (clazz != null) {
        PsiType[] argTypes = PsiUtil.getArgumentTypes(getFirstChild(), false);
        PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        PsiSubstitutor substitutor;
        if (isThisCall()) {
            substitutor = PsiSubstitutor.EMPTY;
        } else {
            PsiClass enclosing = PsiUtil.getContextClass(this);
            assert enclosing != null;
            substitutor = TypeConversionUtil.getSuperClassSubstitutor(clazz, enclosing, PsiSubstitutor.EMPTY);
        }
        PsiType thisType = factory.createType(clazz, substitutor);
        MethodResolverProcessor processor = new MethodResolverProcessor(clazz.getName(), this, true, thisType, argTypes, PsiType.EMPTY_ARRAY, incompleteCode);
        final ResolveState state = ResolveState.initial().put(PsiSubstitutor.KEY, substitutor);
        clazz.processDeclarations(processor, state, null, this);
        ResolveUtil.processNonCodeMembers(thisType, processor, getInvokedExpression(), state);
        return processor.getCandidates();
    }
    return GroovyResolveResult.EMPTY_ARRAY;
}
Also used : MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with MethodResolverProcessor

use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.

the class GrDocFieldReferenceImpl method multiResolveImpl.

@Override
protected ResolveResult[] multiResolveImpl() {
    String name = getReferenceName();
    GrDocReferenceElement holder = getReferenceHolder();
    PsiElement resolved;
    if (holder != null) {
        GrCodeReferenceElement referenceElement = holder.getReferenceElement();
        resolved = referenceElement != null ? referenceElement.resolve() : null;
    } else {
        resolved = PsiUtil.getContextClass(GrDocCommentUtil.findDocOwner(this));
    }
    if (resolved instanceof PsiClass) {
        PropertyResolverProcessor processor = new PropertyResolverProcessor(name, this);
        resolved.processDeclarations(processor, ResolveState.initial(), resolved, this);
        GroovyResolveResult[] candidates = processor.getCandidates();
        if (candidates.length == 0) {
            PsiType thisType = JavaPsiFacade.getInstance(getProject()).getElementFactory().createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
            MethodResolverProcessor methodProcessor = new MethodResolverProcessor(name, this, false, thisType, null, PsiType.EMPTY_ARRAY);
            MethodResolverProcessor constructorProcessor = new MethodResolverProcessor(name, this, true, thisType, null, PsiType.EMPTY_ARRAY);
            resolved.processDeclarations(methodProcessor, ResolveState.initial(), resolved, this);
            resolved.processDeclarations(constructorProcessor, ResolveState.initial(), resolved, this);
            candidates = ArrayUtil.mergeArrays(methodProcessor.getCandidates(), constructorProcessor.getCandidates());
            if (candidates.length > 0) {
                candidates = new GroovyResolveResult[] { candidates[0] };
            }
        }
        return candidates;
    }
    return ResolveResult.EMPTY_ARRAY;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) PropertyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.PropertyResolverProcessor)

Aggregations

MethodResolverProcessor (org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 Project (com.intellij.openapi.project.Project)1 com.intellij.psi (com.intellij.psi)1 SuperMethodsSearch (com.intellij.psi.search.searches.SuperMethodsSearch)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 MethodSignatureBackedByPsiMethod (com.intellij.psi.util.MethodSignatureBackedByPsiMethod)1 TypeConversionUtil (com.intellij.psi.util.TypeConversionUtil)1 UnresolvableCollisionUsageInfo (com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo)1 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)1 UsageInfo (com.intellij.usageView.UsageInfo)1 Processor (com.intellij.util.Processor)1 QueryExecutor (com.intellij.util.QueryExecutor)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Nullable (org.jetbrains.annotations.Nullable)1