2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 package com
.intellij
.refactoring
.replaceConstructorWithBuilder
.usageInfo
;
23 import com
.intellij
.openapi
.util
.Comparing
;
24 import com
.intellij
.openapi
.util
.text
.StringUtil
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
27 import com
.intellij
.psi
.codeStyle
.VariableKind
;
28 import com
.intellij
.refactoring
.replaceConstructorWithBuilder
.ParameterData
;
29 import com
.intellij
.refactoring
.util
.FixableUsageInfo
;
30 import com
.intellij
.util
.IncorrectOperationException
;
34 public class ReplaceConstructorWithSettersChainInfo
extends FixableUsageInfo
{
35 private final String myBuilderClass
;
36 private final Map
<String
, ParameterData
> myParametersMap
;
38 public ReplaceConstructorWithSettersChainInfo(PsiNewExpression constructorReference
, String builderClass
, Map
<String
, ParameterData
> parametersMap
) {
39 super(constructorReference
);
40 myBuilderClass
= builderClass
;
41 myParametersMap
= parametersMap
;
44 public void fixUsage() throws IncorrectOperationException
{
45 final PsiNewExpression expr
= (PsiNewExpression
)getElement();
47 final PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(expr
.getProject()).getElementFactory();
48 final PsiMethod constructor
= expr
.resolveConstructor();
49 if (constructor
!= null) {
50 StringBuffer buf
= new StringBuffer();
51 final PsiExpressionList argumentList
= expr
.getArgumentList();
52 if (argumentList
!= null) {
53 final PsiExpression
[] args
= argumentList
.getExpressions();
54 final PsiParameter
[] parameters
= constructor
.getParameterList().getParameters();
56 final JavaCodeStyleManager styleManager
= JavaCodeStyleManager
.getInstance(constructor
.getProject());
57 for (int i
= 0; i
< Math
.min(constructor
.getParameterList().getParametersCount(), args
.length
); i
++) {
58 String arg
= args
[i
].getText();
59 if (parameters
[i
].isVarArgs()) {
60 for(int ia
= i
+ 1; ia
< args
.length
; ia
++) {
61 arg
+= ", " + args
[ia
].getText();
65 final String pureParamName
= styleManager
.variableNameToPropertyName(parameters
[i
].getName(), VariableKind
.PARAMETER
);
66 final ParameterData data
= myParametersMap
.get(pureParamName
);
67 if (!Comparing
.strEqual(arg
, data
.getDefaultValue()) || data
.isInsertSetter()) {
68 buf
.append(data
.getSetterName()).append("(").append(arg
).append(").");
72 final PsiExpression settersChain
= elementFactory
.createExpressionFromText(
73 "new " + myBuilderClass
+ "()." + buf
.toString() + "create" + StringUtil
.capitalize(constructor
.getName()) + "()",
76 expr
.replace(settersChain
);