Search in sources :

Example 71 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GroovyTypeCheckVisitor method checkConstructorApplicability.

private <T extends GroovyPsiElement> boolean checkConstructorApplicability(@NotNull GroovyResolveResult constructorResolveResult, @NotNull CallInfo<T> info, boolean checkUnknownArgs) {
    final PsiElement element = constructorResolveResult.getElement();
    LOG.assertTrue(element instanceof PsiMethod && ((PsiMethod) element).isConstructor(), element);
    final PsiMethod constructor = (PsiMethod) element;
    final GrArgumentList argList = info.getArgumentList();
    if (argList != null) {
        final GrExpression[] exprArgs = argList.getExpressionArguments();
        if (exprArgs.length == 0 && !PsiUtil.isConstructorHasRequiredParameters(constructor))
            return true;
    }
    PsiType[] types = info.getArgumentTypes();
    PsiClass containingClass = constructor.getContainingClass();
    if (types != null && containingClass != null) {
        final PsiType[] newTypes = GrInnerClassConstructorUtil.addEnclosingArgIfNeeded(types, info.getCall(), containingClass);
        if (newTypes.length != types.length) {
            return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {

                @Nullable
                @Override
                public PsiType[] getArgumentTypes() {
                    return newTypes;
                }
            });
        }
    }
    return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, info);
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 72 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GrChangeSignatureConflictSearcher method addMethodConflicts.

private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) {
    try {
        GrMethod prototype;
        final PsiMethod method = myChangeInfo.getMethod();
        if (!(method instanceof GrMethod))
            return;
        PsiManager manager = method.getManager();
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
        final CanonicalTypes.Type returnType = myChangeInfo.getNewReturnType();
        String newMethodName = myChangeInfo.getNewName();
        if (method.isConstructor()) {
            prototype = factory.createConstructorFromText("foo", ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_STRING_ARRAY, "{}", method);
        } else {
            prototype = factory.createMethodFromText("", "foo", returnType != null ? returnType.getTypeText() : null, ArrayUtil.EMPTY_STRING_ARRAY, method);
        }
        prototype.setName(newMethodName);
        JavaParameterInfo[] parameters = myChangeInfo.getNewParameters();
        for (JavaParameterInfo info : parameters) {
            GrParameter param;
            if (info instanceof GrParameterInfo) {
                param = factory.createParameter(info.getName(), info.getTypeText(), ((GrParameterInfo) info).getDefaultInitializer(), (GroovyPsiElement) method);
            } else {
                param = factory.createParameter(info.getName(), info.getTypeText(), (GroovyPsiElement) method);
            }
            prototype.getParameterList().add(param);
        }
        ConflictsUtil.checkMethodConflicts(method.getContainingClass(), method, prototype, conflicts);
        GrMethodConflictUtil.checkMethodConflicts(method.getContainingClass(), prototype, ((GrMethod) method), conflicts, true);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : CanonicalTypes(com.intellij.refactoring.util.CanonicalTypes) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) IncorrectOperationException(com.intellij.util.IncorrectOperationException) JavaParameterInfo(com.intellij.refactoring.changeSignature.JavaParameterInfo)

Example 73 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GrRefactoringConflictsUtil method analyzeModuleConflicts.

public static void analyzeModuleConflicts(final Project project, final Collection<? extends PsiElement> scopes, final UsageInfo[] usages, final PsiElement target, final MultiMap<PsiElement, String> conflicts) {
    if (scopes == null)
        return;
    final VirtualFile vFile = PsiUtilCore.getVirtualFile(target);
    if (vFile == null)
        return;
    List<GroovyPsiElement> groovyScopes = ContainerUtil.collect(scopes.iterator(), new FilteringIterator.InstanceOf<>(GroovyPsiElement.class));
    analyzeModuleConflicts(project, groovyScopes, usages, vFile, conflicts);
    scopes.removeAll(groovyScopes);
    RefactoringConflictsUtil.analyzeModuleConflicts(project, scopes, usages, vFile, conflicts);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) FilteringIterator(com.intellij.util.containers.FilteringIterator)

Example 74 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GrRefactoringConflictsUtil method analyzeModuleConflicts.

public static void analyzeModuleConflicts(final Project project, final Collection<? extends GroovyPsiElement> scopes, final UsageInfo[] usages, final VirtualFile vFile, final MultiMap<PsiElement, String> conflicts) {
    if (scopes == null)
        return;
    for (final PsiElement scope : scopes) {
        if (scope instanceof PsiPackage)
            return;
    }
    final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
    if (targetModule == null)
        return;
    final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
    final HashSet<PsiElement> reported = new HashSet<>();
    for (final GroovyPsiElement scope : scopes) {
        scope.accept(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
                super.visitCodeReferenceElement(refElement);
                visit(refElement);
            }

            @Override
            public void visitReferenceExpression(@NotNull GrReferenceExpression reference) {
                super.visitReferenceExpression(reference);
                visit(reference);
            }

            private void visit(GrReferenceElement<? extends GroovyPsiElement> reference) {
                final PsiElement resolved = reference.resolve();
                if (resolved != null && !reported.contains(resolved) && !CommonRefactoringUtil.isAncestor(resolved, scopes) && !PsiSearchScopeUtil.isInScope(resolveScope, resolved) && !(resolved instanceof LightElement)) {
                    final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
                    final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", RefactoringUIUtil.getDescription(resolved, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(targetModule.getName()));
                    conflicts.putValue(resolved, CommonRefactoringUtil.capitalize(message));
                    reported.add(resolved);
                }
            }
        });
    }
    boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
    NextUsage: for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element != null && PsiTreeUtil.getParentOfType(element, GrImportStatement.class, false) == null) {
            for (PsiElement scope : scopes) {
                if (PsiTreeUtil.isAncestor(scope, element, false))
                    continue NextUsage;
            }
            final GlobalSearchScope resolveScope1 = element.getResolveScope();
            if (!resolveScope1.isSearchInModuleContent(targetModule, isInTestSources)) {
                final PsiFile usageFile = element.getContainingFile();
                PsiElement container;
                if (usageFile instanceof PsiJavaFile) {
                    container = ConflictsUtil.getContainer(element);
                } else {
                    container = usageFile;
                }
                final String scopeDescription = RefactoringUIUtil.getDescription(container, true);
                final VirtualFile usageVFile = usageFile.getVirtualFile();
                if (usageVFile != null) {
                    Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
                    if (module != null) {
                        final String message;
                        final PsiElement referencedElement;
                        if (usage instanceof MoveRenameUsageInfo) {
                            referencedElement = ((MoveRenameUsageInfo) usage).getReferencedElement();
                        } else {
                            referencedElement = usage.getElement();
                        }
                        assert referencedElement != null : usage;
                        if (module == targetModule && isInTestSources) {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        } else {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        }
                        conflicts.putValue(referencedElement, CommonRefactoringUtil.capitalize(message));
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) LightElement(com.intellij.psi.impl.light.LightElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 75 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GroovyChangeContextUtil method decodeContextInfo.

public static void decodeContextInfo(PsiElement element, @Nullable PsiClass thisClass, @Nullable GrExpression thisAccessExpr) {
    if (!(element instanceof GroovyPsiElement))
        return;
    for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        decodeContextInfo(child, thisClass, thisAccessExpr);
    }
    if (element.getCopyableUserData(KEY_ENCODED) != null) {
        element.putCopyableUserData(KEY_ENCODED, null);
        final PsiManager manager = element.getManager();
        if (PsiUtil.isThisReference(element)) {
            final PsiClass thisQualClass = element.getCopyableUserData(QUALIFIER_CLASS_KEY);
            element.putCopyableUserData(QUALIFIER_CLASS_KEY, null);
            if (thisAccessExpr != null && !manager.areElementsEquivalent(thisClass, thisQualClass)) {
                element.replace(thisAccessExpr);
                return;
            }
        } else if (element instanceof GrReferenceExpression) {
            final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
            final GrReferenceExpression refExpr = (GrReferenceExpression) element;
            final PsiElement resolvedElement = refExpr.resolve();
            final PsiMember memberRef = refExpr.getCopyableUserData(REF_TO_MEMBER);
            refExpr.putCopyableUserData(REF_TO_MEMBER, null);
            if (memberRef != null && memberRef.isValid()) {
                final PsiClass memberClass = memberRef.getContainingClass();
                if (memberClass != null) {
                    if (memberRef.hasModifierProperty(PsiModifier.STATIC)) {
                        if (!manager.areElementsEquivalent(memberRef, resolvedElement)) {
                            final PsiElement qualifier = refExpr.getQualifier();
                            if (!(qualifier instanceof GrReferenceExpression)) {
                                refExpr.setQualifier(factory.createReferenceExpressionFromText(memberClass.getQualifiedName()));
                                JavaCodeStyleManager.getInstance(manager.getProject()).shortenClassReferences(refExpr.getQualifier());
                                return;
                            }
                        }
                    } else if (thisAccessExpr instanceof GrReferenceExpression) {
                        final PsiElement qualifier = refExpr.getQualifier();
                        if (!(qualifier instanceof GrReferenceExpression)) {
                            refExpr.setQualifier(thisAccessExpr);
                            return;
                        }
                    }
                }
            }
        }
        PsiClass refClass = element.getCopyableUserData(REF_TO_CLASS);
        element.putCopyableUserData(REF_TO_CLASS, null);
        if (refClass != null && refClass.isValid()) {
            final PsiReference ref = element.getReference();
            if (ref != null) {
                ref.bindToElement(refClass);
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6