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
.inlineSuperClass
.usageInfo
;
23 import com
.intellij
.openapi
.util
.text
.StringUtil
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.util
.TypeConversionUtil
;
26 import com
.intellij
.refactoring
.util
.FixableUsageInfo
;
27 import com
.intellij
.util
.Function
;
28 import com
.intellij
.util
.IncorrectOperationException
;
30 public class ReplaceConstructorUsageInfo
extends FixableUsageInfo
{
31 private final PsiType myNewType
;
32 private String myConflict
;
33 private static final String CONSTRUCTOR_MATCHING_SUPER_NOT_FOUND
= "Constructor matching super not found";
35 public ReplaceConstructorUsageInfo(PsiNewExpression element
, PsiType newType
, final PsiClass
[] targetClasses
) {
38 final PsiMethod
[] constructors
= targetClasses
[0].getConstructors();
39 final PsiMethod constructor
= element
.resolveConstructor();
40 if (constructor
== null) {
41 if (constructors
.length
== 1 && constructors
[0].getParameterList().getParametersCount() > 0 || constructors
.length
> 1) {
42 myConflict
= CONSTRUCTOR_MATCHING_SUPER_NOT_FOUND
;
45 final PsiParameter
[] superParameters
= constructor
.getParameterList().getParameters();
46 boolean foundMatchingConstructor
= constructors
.length
== 0 && superParameters
.length
== 0;
47 constr
: for (PsiMethod method
: constructors
) {
48 final PsiParameter
[] parameters
= method
.getParameterList().getParameters();
49 if (superParameters
.length
== parameters
.length
) {
50 for (int i
= 0; i
< parameters
.length
; i
++) {
51 PsiParameter parameter
= parameters
[i
];
52 if (!TypeConversionUtil
.isAssignable(TypeConversionUtil
.erasure(parameter
.getType()),
53 TypeConversionUtil
.erasure(superParameters
[i
].getType()))) {
57 foundMatchingConstructor
= true;
60 if (!foundMatchingConstructor
) {
61 myConflict
= CONSTRUCTOR_MATCHING_SUPER_NOT_FOUND
;
66 PsiType type
= element
.getType();
68 appendConflict("Type is unknown");
71 type
= type
.getDeepComponentType();
74 if (!TypeConversionUtil
.isAssignable(type
, newType
)) {
75 final String conflict
= "Type parameters do not agree in " + element
.getText() + ". " +
76 "Expected " + newType
.getPresentableText() + " but found " + type
.getPresentableText();
77 appendConflict(conflict
);
80 if (targetClasses
.length
> 1) {
81 final String conflict
= "Constructor " + element
.getText() + " can be replaced with any of " + StringUtil
.join(targetClasses
, new Function
<PsiClass
, String
>() {
82 public String
fun(final PsiClass psiClass
) {
83 return psiClass
.getQualifiedName();
86 appendConflict(conflict
);
90 private void appendConflict(final String conflict
) {
91 if (myConflict
== null) {
92 myConflict
= conflict
;
94 myConflict
+= "\n" + conflict
;
98 public void fixUsage() throws IncorrectOperationException
{
99 final PsiNewExpression newExpression
= (PsiNewExpression
)getElement();
100 if (newExpression
!= null) {
101 final PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(newExpression
.getProject()).getElementFactory();
103 final StringBuffer buf
= new StringBuffer();
104 buf
.append("new ").append(myNewType
.getCanonicalText());
105 final PsiArrayInitializerExpression arrayInitializer
= newExpression
.getArrayInitializer();
106 final PsiType newExpressionType
= newExpression
.getType();
107 assert newExpressionType
!= null;
108 if (arrayInitializer
!= null) {
109 for (int i
= 0; i
< newExpressionType
.getArrayDimensions(); i
++) {
112 buf
.append(arrayInitializer
.getText());
115 final PsiExpression
[] arrayDimensions
= newExpression
.getArrayDimensions();
116 if (arrayDimensions
.length
> 0) {
118 buf
.append(StringUtil
.join(arrayDimensions
, new Function
<PsiExpression
, String
>() {
119 public String
fun(PsiExpression psiExpression
) {
120 return psiExpression
.getText();
124 for (int i
= 0; i
< newExpressionType
.getArrayDimensions() - arrayDimensions
.length
; i
++) {
128 final PsiExpressionList list
= newExpression
.getArgumentList();
130 buf
.append(list
.getText());
135 newExpression
.replace(elementFactory
.createExpressionFromText(buf
.toString(), newExpression
));
139 public String
getConflictMessage() {