extract method: copy parameters annotations to the extracted method when applicable (IDEA-40617)

This commit is contained in:
Anna Kozlova
2014-12-01 20:54:13 +01:00
parent 900afda0d7
commit 4ea62b333e
4 changed files with 69 additions and 0 deletions
@@ -1137,6 +1137,7 @@ public class ExtractMethodProcessor implements MatchProvider {
for (VariableData data : myVariableDatum) {
if (data.passAsParameter) {
PsiParameter parm = myElementFactory.createParameter(data.name, data.type);
copyParamAnnotations(parm);
if (isFinal) {
PsiUtil.setModifierProperty(parm, PsiModifier.FINAL, true);
}
@@ -1171,6 +1172,21 @@ public class ExtractMethodProcessor implements MatchProvider {
return (PsiMethod)myStyleManager.reformat(newMethod);
}
private void copyParamAnnotations(PsiParameter parm) {
final PsiVariable variable = PsiResolveHelper.SERVICE.getInstance(myProject).resolveReferencedVariable(parm.getName(), myElements[0]);
if (variable instanceof PsiParameter) {
final PsiModifierList modifierList = variable.getModifierList();
if (modifierList != null) {
for (PsiAnnotation annotation : modifierList.getAnnotations()) {
if (SuppressWarnings.class.getName().equals(annotation.getQualifiedName())) continue;
final PsiModifierList parmModifierList = parm.getModifierList();
LOG.assertTrue(parmModifierList != null, parm);
parmModifierList.add(annotation);
}
}
}
}
@NotNull
protected PsiMethodCallExpression generateMethodCall(PsiExpression instanceQualifier, final boolean generateArgs) throws IncorrectOperationException {
@NonNls StringBuilder buffer = new StringBuilder();
@@ -0,0 +1,21 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class X {
@NotNull
public X fun1(int x) {
return this;
}
public X fun2(@Nullable @SuppressWarnings("unused") String b) {
<selection>
if (b != null) {
int x = 1;
return fun1(x);
}
</selection>
int x = 0;
return null;
}
}
@@ -0,0 +1,28 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class X {
@NotNull
public X fun1(int x) {
return this;
}
public X fun2(@Nullable @SuppressWarnings("unused") String b) {
X x1 = newMethod(b);
if (x1 != null) return x1;
int x = 0;
return null;
}
@Nullable
private X newMethod(@Nullable String b) {
if (b != null) {
int x = 1;
return fun1(x);
}
return null;
}
}
@@ -611,6 +611,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testCopyParamAnnotations() throws Exception {
doTest();
}
private void doTestDisabledParam() throws PrepareFailedException {
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
settings.ELSE_ON_NEW_LINE = true;