convert to vararg: avoid parameter change as long as possible (IDEA-114314)

This commit is contained in:
anna
2013-10-02 17:18:43 +02:00
parent a4eba6128f
commit 99ab7e92a4
6 changed files with 75 additions and 8 deletions
@@ -15,6 +15,7 @@
*/
package com.siyeh.ipp.varargs;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.util.IncorrectOperationException;
@@ -25,6 +26,8 @@ import org.jetbrains.annotations.NotNull;
public class MakeMethodVarargsIntention extends Intention {
private static final Logger LOG = Logger.getInstance("#" + MakeMethodVarargsIntention.class.getName());
@NotNull
protected PsiElementPredicate getElementPredicate() {
return new MakeMethodVarargsPredicate();
@@ -42,14 +45,11 @@ public class MakeMethodVarargsIntention extends Intention {
final PsiParameter[] parameters = parameterList.getParameters();
final PsiParameter lastParameter = parameters[parameters.length - 1];
final PsiType type = lastParameter.getType();
final PsiType componentType = type.getDeepComponentType();
final String text = componentType.getCanonicalText();
final PsiManager manager = element.getManager();
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
final PsiParameter newParameter =
factory.createParameterFromText(text + "... " +
lastParameter.getName(), element);
lastParameter.replace(newParameter);
final PsiElementFactory factory = JavaPsiFacade.getInstance(element.getProject()).getElementFactory();
final PsiTypeElement typeElement = lastParameter.getTypeElement();
LOG.assertTrue(typeElement != null);
final PsiType ellipsisType = PsiEllipsisType.createEllipsis(((PsiArrayType)type).getComponentType(), type.getAnnotations());
typeElement.replace(factory.createTypeElement(ellipsisType));
}
private static void makeMethodCallsVarargs(PsiElement element)
@@ -0,0 +1,8 @@
class Test {
public void foo(final String[][] a<caret>rg) {
}
{
foo(new String[][]{});
}
}
@@ -0,0 +1,8 @@
class Test {
public void foo(final String[]... arg) {
}
{
foo();
}
}
@@ -0,0 +1,8 @@
class Test {
public void foo(final String[] a<caret>rg) {
}
{
foo(new String[]{});
}
}
@@ -0,0 +1,8 @@
class Test {
public void foo(final String... arg) {
}
{
foo();
}
}
@@ -0,0 +1,35 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.varargs;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
public class MakeMethodVarargsIntentionTest extends IPPTestCase {
public void testFinal() { doTest(); }
public void testDeepArray() { doTest(); }
@Override
protected String getRelativePath() {
return "varargs/make_method_varargs";
}
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("make.method.varargs.intention.name");
}
}