private void fixReferencesToStatic(GroovyPsiElement classMember, Set<PsiMember> movedMembers) throws IncorrectOperationException { final StaticReferencesCollector collector = new StaticReferencesCollector(movedMembers); classMember.accept(collector); ArrayList<GrReferenceElement> refs = collector.getReferences(); ArrayList<PsiElement> members = collector.getReferees(); ArrayList<PsiClass> classes = collector.getRefereeClasses(); GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject); for (int i = 0; i < refs.size(); i++) { GrReferenceElement ref = refs.get(i); PsiElement namedElement = members.get(i); PsiClass aClass = classes.get(i); if (namedElement instanceof PsiNamedElement) { GrReferenceExpression newRef = (GrReferenceExpression) factory.createExpressionFromText( "a." + ((PsiNamedElement) namedElement).getName(), null); GrExpression qualifier = newRef.getQualifierExpression(); assert qualifier != null; qualifier = (GrExpression) qualifier.replace( factory.createReferenceExpressionFromText(aClass.getQualifiedName())); qualifier.putCopyableUserData(PRESERVE_QUALIFIER, ref.isQualified()); PsiElement replaced = ref.replace(newRef); JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(replaced); } } }
private static boolean canBeSimplified(PsiElement element) { if (PsiTreeUtil.getParentOfType(element, PsiComment.class) != null) return false; if (element instanceof GrCodeReferenceElement) { if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null) return false; } else if (element instanceof GrReferenceExpression) { if (!PsiImplUtil.seemsToBeQualifiedClassName((GrReferenceExpression) element)) return false; } else { return false; } final GrReferenceElement ref = (GrReferenceElement) element; if (ref.getQualifier() == null) return false; if (!(ref.getContainingFile() instanceof GroovyFileBase)) return false; final PsiElement resolved = ref.resolve(); if (!(resolved instanceof PsiClass)) return false; final String name = ((PsiClass) resolved).getName(); if (name == null) return false; final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject()); final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element); final GroovyResolveResult resolveResult = shortedRef.advancedResolve(); if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) { return true; } final PsiClass containingClass = ((PsiClass) resolved).getContainingClass(); if (containingClass != null && !GroovyCodeStyleSettingsFacade.getInstance(containingClass.getProject()) .insertInnerClassImports()) { return false; } return resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK(); }
public static GrReferenceExpression qualifyReference( GrReferenceExpression referenceExpression, final PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException { PsiManager manager = referenceExpression.getManager(); GrReferenceExpression expressionFromText; final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(referenceExpression.getProject()); if (qualifyingClass == null) { PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class); final PsiClass containingClass = member.getContainingClass(); if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) { while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) { parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true); } LOG.assertTrue(parentClass != null); expressionFromText = factory.createReferenceExpressionFromText("A.this." + member.getName()); //noinspection ConstantConditions ((GrReferenceExpression) expressionFromText.getQualifier()) .getQualifier() .replace(factory.createReferenceElementForClass(parentClass)); } else { expressionFromText = (GrReferenceExpression) factory.createExpressionFromText("this." + member.getName()); } } else { expressionFromText = (GrReferenceExpression) factory.createExpressionFromText("A." + member.getName()); expressionFromText.setQualifier(factory.createReferenceElementForClass(qualifyingClass)); } CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); expressionFromText = (GrReferenceExpression) codeStyleManager.reformat(expressionFromText); return (GrReferenceExpression) referenceExpression.replace(expressionFromText); }