IDEA-112562 Pull up should allow to pull method body into interface as default method

This commit is contained in:
Anna Kozlova
2013-08-30 21:45:06 +04:00
parent 5426bcfd4d
commit 48205f5c1d
6 changed files with 59 additions and 13 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import com.intellij.psi.statistics.StatisticsInfo;
import com.intellij.psi.statistics.StatisticsManager;
import com.intellij.psi.util.MethodSignature;
import com.intellij.psi.util.MethodSignatureUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.JavaRefactoringSettings;
@@ -222,9 +223,12 @@ public class PullUpDialog extends RefactoringDialog {
boolean hasJavadoc = false;
for (MemberInfo info : myMemberInfos) {
final PsiMember member = info.getMember();
if (myMemberInfoModel.isAbstractEnabled(info) && member instanceof PsiDocCommentOwner && ((PsiDocCommentOwner)member).getDocComment() != null) {
hasJavadoc = true;
break;
if (myMemberInfoModel.isAbstractEnabled(info) && member instanceof PsiDocCommentOwner) {
info.setToAbstract(myMemberInfoModel.isAbstractWhenDisabled(info));
if (((PsiDocCommentOwner)member).getDocComment() != null) {
hasJavadoc = true;
break;
}
}
}
UIUtil.setEnabled(myJavaDocPanel, hasJavadoc, true);
@@ -270,6 +274,9 @@ public class PullUpDialog extends RefactoringDialog {
public boolean isAbstractEnabled(MemberInfo member) {
PsiClass currentSuperClass = getSuperClass();
if (currentSuperClass == null || !currentSuperClass.isInterface()) return true;
if (PsiUtil.isLanguageLevel8OrHigher(currentSuperClass)) {
return true;
}
return false;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -232,10 +232,18 @@ public class PullUpHelper extends BaseRefactoringProcessor{
if (method.findSuperMethods(myTargetSuperClass).length == 0) {
deleteOverrideAnnotationIfFound(methodCopy);
}
final boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
if (myIsTargetInterface || info.isToAbstract()) {
ChangeContextUtil.clearContextInfo(method);
RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
if (!info.isToAbstract() && !method.hasModifierProperty(PsiModifier.ABSTRACT)) {
//pull as default
RefactoringUtil.makeMethodDefault(methodCopy);
isOriginalMethodAbstract = true;
} else {
RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
}
RefactoringUtil.replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
myJavaDocPolicy.processCopiedJavaDoc(methodCopy.getDocComment(), method.getDocComment(), isOriginalMethodAbstract);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -763,14 +763,25 @@ public class RefactoringUtil {
PsiUtil.setModifierProperty(method, PsiModifier.ABSTRACT, true);
}
PsiUtil.setModifierProperty(method, PsiModifier.FINAL, false);
PsiUtil.setModifierProperty(method, PsiModifier.SYNCHRONIZED, false);
PsiUtil.setModifierProperty(method, PsiModifier.NATIVE, false);
prepareForInterface(method);
if (!targetClass.isInterface()) {
PsiUtil.setModifierProperty(targetClass, PsiModifier.ABSTRACT, true);
}
}
public static void makeMethodDefault(@NotNull PsiMethod method) throws IncorrectOperationException {
PsiUtil.setModifierProperty(method, PsiModifier.DEFAULT, true);
PsiUtil.setModifierProperty(method, PsiModifier.ABSTRACT, false);
prepareForInterface(method);
}
private static void prepareForInterface(PsiMethod method) {
PsiUtil.setModifierProperty(method, PsiModifier.FINAL, false);
PsiUtil.setModifierProperty(method, PsiModifier.SYNCHRONIZED, false);
PsiUtil.setModifierProperty(method, PsiModifier.NATIVE, false);
removeFinalParameters(method);
}
@@ -0,0 +1,7 @@
interface Bar<T> { }
interface Base<T> { }
class Foo<T,U> implements Base<U> {
void ge<caret>t(Bar<U> bar) { }
}
@@ -0,0 +1,8 @@
interface Bar<T> { }
interface Base<T> {
default void get(Bar<T> bar) { }
}
class Foo<T,U> implements Base<U> {
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -86,6 +86,11 @@ public class PullUpTest extends LightRefactoringTestCase {
doTest(new RefactoringTestUtil.MemberDescriptor("get", PsiMethod.class));
}
public void testAsDefault() {
final RefactoringTestUtil.MemberDescriptor descriptor = new RefactoringTestUtil.MemberDescriptor("get", PsiMethod.class);
doTest(descriptor);
}
public void testTypeParamErasure() {
doTest(new RefactoringTestUtil.MemberDescriptor("f", PsiField.class));
}
@@ -127,7 +132,7 @@ public class PullUpTest extends LightRefactoringTestCase {
}
public void testTypeParamsConflictingNames() {
doTest(false, new RefactoringTestUtil.MemberDescriptor("foo", PsiMethod.class));
doTest(false, new RefactoringTestUtil.MemberDescriptor("foo", PsiMethod.class, true));
}
public void testEscalateVisibility() {