Search in sources :

Example 6 with GrField

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

the class GrPullUpHelper method doMoveField.

private void doMoveField(PsiSubstitutor substitutor, MemberInfo info) {
    GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
    GrField field = (GrField) info.getMember();
    field.normalizeDeclaration();
    replaceMovedMemberTypeParameters(field, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
    fixReferencesToStatic(field);
    if (myTargetSuperClass.isInterface()) {
        PsiUtil.setModifierProperty(field, PsiModifier.PUBLIC, true);
    }
    final PsiMember movedElement = (PsiMember) myTargetSuperClass.add(convertFieldToLanguage(field, myTargetSuperClass.getLanguage()));
    myMembersAfterMove.add(movedElement);
    deleteMemberWithDocComment(field);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)

Example 7 with GrField

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

the class GrPullUpHelper method replaceMovedMemberTypeParameters.

public static void replaceMovedMemberTypeParameters(final PsiElement member, final Iterable<PsiTypeParameter> parametersIterable, final PsiSubstitutor substitutor, final GroovyPsiElementFactory factory) {
    final Map<PsiElement, PsiElement> replacement = new LinkedHashMap<>();
    for (PsiTypeParameter parameter : parametersIterable) {
        PsiType substitutedType = substitutor.substitute(parameter);
        PsiType type = substitutedType != null ? substitutedType : TypeConversionUtil.erasure(factory.createType(parameter));
        PsiElement scopeElement = member instanceof GrField ? member.getParent() : member;
        for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(scopeElement))) {
            final PsiElement element = reference.getElement();
            final PsiElement parent = element.getParent();
            if (parent instanceof PsiTypeElement) {
                replacement.put(parent, factory.createTypeElement(type));
            } else if (element instanceof GrCodeReferenceElement && type instanceof PsiClassType) {
                replacement.put(element, factory.createReferenceElementByType((PsiClassType) type));
            }
        }
    }
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject());
    for (PsiElement element : replacement.keySet()) {
        if (element.isValid()) {
            final PsiElement replaced = element.replace(replacement.get(element));
            codeStyleManager.shortenClassReferences(replaced);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 8 with GrField

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

the class RenameGrFieldProcessor method findReferences.

@NotNull
@Override
public Collection<PsiReference> findReferences(PsiElement element) {
    assert element instanceof GrField;
    ArrayList<PsiReference> refs = new ArrayList<>();
    GrField field = (GrField) element;
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(element.getProject());
    PsiMethod setter = field.getSetter();
    if (setter != null) {
        refs.addAll(RenameAliasedUsagesUtil.filterAliasedRefs(MethodReferencesSearch.search(setter, projectScope, true).findAll(), setter));
    }
    GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        refs.addAll(RenameAliasedUsagesUtil.filterAliasedRefs(MethodReferencesSearch.search(getter, projectScope, true).findAll(), getter));
    }
    refs.addAll(RenameAliasedUsagesUtil.filterAliasedRefs(ReferencesSearch.search(field, projectScope, false).findAll(), field));
    return refs;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GrField

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

the class RenameGrFieldProcessor method renameElement.

@Override
public void renameElement(final PsiElement psiElement, String newName, final UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    GrField field = (GrField) psiElement;
    Map<GrReferenceExpression, PsiElement> handled = ContainerUtil.newHashMap();
    for (UsageInfo usage : usages) {
        final PsiReference ref = usage.getReference();
        if (ref instanceof GrReferenceExpression) {
            PsiElement resolved = ref.resolve();
            ref.handleElementRename(getNewNameFromTransformations(resolved, newName));
            handled.put((GrReferenceExpression) ref, resolved);
        } else if (ref != null) {
            handleElementRename(newName, ref, field.getName());
        }
    }
    field.setName(newName);
    PsiManager manager = psiElement.getManager();
    for (GrReferenceExpression expression : handled.keySet()) {
        PsiElement oldResolved = handled.get(expression);
        if (oldResolved == null)
            continue;
        PsiElement resolved = expression.resolve();
        if (resolved == null)
            continue;
        if (manager.areElementsEquivalent(oldResolved, resolved))
            continue;
        if (oldResolved.equals(field) || isQualificationNeeded(manager, oldResolved, resolved)) {
            qualify(field, expression);
        }
    }
    if (listener != null) {
        listener.elementRenamed(psiElement);
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 10 with GrField

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField 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)

Aggregations

GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)55 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)13 GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)11 PsiElement (com.intellij.psi.PsiElement)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)9 NotNull (org.jetbrains.annotations.NotNull)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 Nullable (org.jetbrains.annotations.Nullable)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)7 ArrayList (java.util.ArrayList)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 Project (com.intellij.openapi.project.Project)4 PsiClass (com.intellij.psi.PsiClass)4 UsageInfo (com.intellij.usageView.UsageInfo)4