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);
}
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);
}
}
}
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;
}
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);
}
}
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);
}
Aggregations