use of com.intellij.refactoring.listeners.impl.JavaRefactoringListenerManagerImpl in project intellij-community by JetBrains.
the class JavaPushDownDelegate method pushDownToClass.
@Override
public void pushDownToClass(PsiElement targetElement, PushDownData<MemberInfo, PsiMember> pushDownData) {
final PsiElementFactory factory = JavaPsiFacade.getInstance(pushDownData.getSourceClass().getProject()).getElementFactory();
final PsiClass targetClass = targetElement instanceof PsiClass ? (PsiClass) targetElement : null;
if (targetClass == null) {
return;
}
final PsiClass sourceClass = (PsiClass) pushDownData.getSourceClass();
final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(sourceClass, targetClass, PsiSubstitutor.EMPTY);
for (MemberInfoBase<? extends PsiElement> memberInfo : pushDownData.getMembersToMove()) {
PsiMember member = (PsiMember) memberInfo.getMember();
final List<PsiReference> refsToRebind = new ArrayList<>();
final PsiModifierList list = member.getModifierList();
LOG.assertTrue(list != null);
if (list.hasModifierProperty(PsiModifier.STATIC)) {
for (final PsiReference reference : ReferencesSearch.search(member)) {
final PsiElement element = reference.getElement();
if (element instanceof PsiReferenceExpression) {
final PsiExpression qualifierExpression = ((PsiReferenceExpression) element).getQualifierExpression();
if (qualifierExpression instanceof PsiReferenceExpression && !(((PsiReferenceExpression) qualifierExpression).resolve() instanceof PsiClass)) {
continue;
}
}
refsToRebind.add(reference);
}
}
member = (PsiMember) member.copy();
RefactoringUtil.replaceMovedMemberTypeParameters(member, PsiUtil.typeParametersIterable(sourceClass), substitutor, factory);
PsiMember newMember = null;
if (member instanceof PsiField) {
((PsiField) member).normalizeDeclaration();
if (sourceClass.isInterface() && !targetClass.isInterface()) {
PsiUtil.setModifierProperty(member, PsiModifier.PUBLIC, true);
PsiUtil.setModifierProperty(member, PsiModifier.STATIC, true);
PsiUtil.setModifierProperty(member, PsiModifier.FINAL, true);
}
newMember = (PsiMember) targetClass.add(member);
} else if (member instanceof PsiMethod) {
PsiMethod method = (PsiMethod) member;
PsiMethod methodBySignature = MethodSignatureUtil.findMethodBySuperSignature(targetClass, method.getSignature(substitutor), false);
if (methodBySignature == null) {
newMember = (PsiMethod) targetClass.add(method);
final PsiMethod oldMethod = (PsiMethod) memberInfo.getMember();
if (sourceClass.isInterface() && !targetClass.isInterface()) {
PsiUtil.setModifierProperty(newMember, PsiModifier.PUBLIC, true);
if (oldMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
RefactoringUtil.makeMethodAbstract(targetClass, (PsiMethod) newMember);
} else {
PsiUtil.setModifierProperty(newMember, PsiModifier.DEFAULT, false);
}
}
if (memberInfo.isToAbstract()) {
if (newMember.hasModifierProperty(PsiModifier.PRIVATE)) {
PsiUtil.setModifierProperty(newMember, PsiModifier.PROTECTED, true);
}
pushDownData.getCommentPolicy().processNewJavaDoc(((PsiMethod) newMember).getDocComment());
OverrideImplementUtil.annotateOnOverrideImplement((PsiMethod) newMember, targetClass, (PsiMethod) memberInfo.getMember());
}
} else {
//abstract method: remove @Override
if (!memberInfo.isToAbstract()) {
final PsiAnnotation annotation = AnnotationUtil.findAnnotation(methodBySignature, "java.lang.Override");
if (annotation != null && !leaveOverrideAnnotation(sourceClass, substitutor, method)) {
annotation.delete();
}
}
final PsiDocComment oldDocComment = method.getDocComment();
if (oldDocComment != null) {
final PsiDocComment docComment = methodBySignature.getDocComment();
final int policy = pushDownData.getCommentPolicy().getJavaDocPolicy();
if (policy == DocCommentPolicy.COPY || policy == DocCommentPolicy.MOVE) {
if (docComment != null) {
docComment.replace(oldDocComment);
} else {
methodBySignature.getParent().addBefore(oldDocComment, methodBySignature);
}
}
}
}
} else if (member instanceof PsiClass) {
if (sourceClass.isInterface() && !targetClass.isInterface()) {
PsiUtil.setModifierProperty(member, PsiModifier.PUBLIC, true);
}
if (Boolean.FALSE.equals(memberInfo.getOverrides())) {
final PsiClass psiClass = (PsiClass) memberInfo.getMember();
PsiClassType classType = null;
if (!targetClass.isInheritor(psiClass, false)) {
final PsiClassType[] types = ((MemberInfo) memberInfo).getSourceReferenceList().getReferencedTypes();
for (PsiClassType type : types) {
if (type.resolve() == psiClass) {
classType = (PsiClassType) substitutor.substitute(type);
}
}
PsiJavaCodeReferenceElement classRef = classType != null ? factory.createReferenceElementByType(classType) : factory.createClassReferenceElement(psiClass);
if (psiClass.isInterface()) {
targetClass.getImplementsList().add(classRef);
} else {
targetClass.getExtendsList().add(classRef);
}
}
} else {
newMember = (PsiMember) targetClass.add(member);
}
}
if (newMember != null) {
decodeRefs(sourceClass, newMember, targetClass);
//rebind imports first
Collections.sort(refsToRebind, Comparator.comparing(PsiReference::getElement, PsiUtil.BY_POSITION));
for (PsiReference psiReference : refsToRebind) {
JavaCodeStyleManager.getInstance(sourceClass.getProject()).shortenClassReferences(psiReference.bindToElement(newMember));
}
final JavaRefactoringListenerManager listenerManager = JavaRefactoringListenerManager.getInstance(newMember.getProject());
((JavaRefactoringListenerManagerImpl) listenerManager).fireMemberMoved(sourceClass, newMember);
}
}
}
use of com.intellij.refactoring.listeners.impl.JavaRefactoringListenerManagerImpl in project intellij-community by JetBrains.
the class PullUpProcessor method moveMembersToBase.
public void moveMembersToBase() throws IncorrectOperationException {
myMovedMembers = ContainerUtil.newHashSet();
myMembersAfterMove = ContainerUtil.newHashSet();
// build aux sets
for (MemberInfo info : myMembersToMove) {
myMovedMembers.add(info.getMember());
}
final PsiSubstitutor substitutor = upDownSuperClassSubstitutor();
for (MemberInfo info : myMembersToMove) {
PullUpHelper<MemberInfo> processor = getProcessor(info);
LOG.assertTrue(processor != null, info.getMember());
if (!(info.getMember() instanceof PsiClass) || info.getOverrides() == null) {
processor.setCorrectVisibility(info);
processor.encodeContextInfo(info);
}
processor.move(info, substitutor);
}
for (PsiMember member : myMembersAfterMove) {
PullUpHelper<MemberInfo> processor = getProcessor(member);
LOG.assertTrue(processor != null, member);
processor.postProcessMember(member);
final JavaRefactoringListenerManager listenerManager = JavaRefactoringListenerManager.getInstance(myProject);
((JavaRefactoringListenerManagerImpl) listenerManager).fireMemberMoved(mySourceClass, member);
}
}
Aggregations