mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
inline class to anonymous: replace varargs with array creation
This commit is contained in:
@@ -314,7 +314,27 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
public static void inlineConstructorCall(PsiCall constructorCall) {
|
||||
final PsiMethod oldConstructor = constructorCall.resolveMethod();
|
||||
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();
|
||||
LOG.assertTrue(statements.length == 1 && statements[0] instanceof PsiExpressionStatement);
|
||||
|
||||
@@ -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(){}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class A {
|
||||
private Object b = new Runnable() {
|
||||
{
|
||||
System.out.println(new String[]{"w"}[0]);
|
||||
}
|
||||
|
||||
public void run(){}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -144,6 +144,10 @@ public class InlineToAnonymousClassTest extends LightCodeInsightTestCase {
|
||||
doTest(false, false);
|
||||
}
|
||||
|
||||
public void testChainedVarargConstructors() throws Exception {
|
||||
doTest(false, false);
|
||||
}
|
||||
|
||||
public void testInlineThisOnly() throws Exception {
|
||||
doTest(true, false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user