inline class to anonymous: replace varargs with array creation

This commit is contained in:
anna
2010-02-27 20:19:21 +03:00
parent 85fdbe5309
commit 50de08af4a
4 changed files with 51 additions and 1 deletions

View File

@@ -314,7 +314,27 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
public static void inlineConstructorCall(PsiCall constructorCall) { public static void inlineConstructorCall(PsiCall constructorCall) {
final PsiMethod oldConstructor = constructorCall.resolveMethod(); final PsiMethod oldConstructor = constructorCall.resolveMethod();
LOG.assertTrue(oldConstructor != null); LOG.assertTrue(oldConstructor != null);
final PsiExpression[] instanceCreationArguments = constructorCall.getArgumentList().getExpressions(); PsiExpression[] instanceCreationArguments = constructorCall.getArgumentList().getExpressions();
if (oldConstructor.isVarArgs()) { //wrap with explicit array
final PsiParameter[] parameters = oldConstructor.getParameterList().getParameters();
final PsiType varargType = parameters[parameters.length - 1].getType();
if (varargType instanceof PsiEllipsisType) {
final PsiType arrayType =
constructorCall.resolveMethodGenerics().getSubstitutor().substitute(((PsiEllipsisType)varargType).getComponentType());
final PsiExpression[] exprs = new PsiExpression[parameters.length];
System.arraycopy(instanceCreationArguments, 0, exprs, 0, parameters.length - 1);
StringBuffer varargs = new StringBuffer();
for (int i = parameters.length - 1; i < instanceCreationArguments.length; i++) {
if (varargs.length() > 0) varargs.append(", ");
varargs.append(instanceCreationArguments[i].getText());
}
exprs[parameters.length - 1] = JavaPsiFacade.getElementFactory(constructorCall.getProject())
.createExpressionFromText("new " + arrayType.getCanonicalText() + "[]{" + varargs.toString() + "}", constructorCall);
instanceCreationArguments = exprs;
}
}
PsiStatement[] statements = oldConstructor.getBody().getStatements(); PsiStatement[] statements = oldConstructor.getBody().getStatements();
LOG.assertTrue(statements.length == 1 && statements[0] instanceof PsiExpressionStatement); LOG.assertTrue(statements.length == 1 && statements[0] instanceof PsiExpressionStatement);

View File

@@ -0,0 +1,16 @@
class A {
private Object b = new MyException("w");
private class <caret>MyException implements Runnable {
public MyException(String...msg){
this(new Throwable(), msg[0]);
}
public MyException(Throwable t, String msg)
{
System.out.println(msg);
}
public void run(){}
}
}

View File

@@ -0,0 +1,10 @@
class A {
private Object b = new Runnable() {
{
System.out.println(new String[]{"w"}[0]);
}
public void run(){}
};
}

View File

@@ -144,6 +144,10 @@ public class InlineToAnonymousClassTest extends LightCodeInsightTestCase {
doTest(false, false); doTest(false, false);
} }
public void testChainedVarargConstructors() throws Exception {
doTest(false, false);
}
public void testInlineThisOnly() throws Exception { public void testInlineThisOnly() throws Exception {
doTest(true, false); doTest(true, false);
} }