update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / inlineSuperClass / usageInfo / ReplaceConstructorUsageInfo.java
blobada7a7222c79b7922f845cebeaa06fe178e22f61
1 /*
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.
18 * User: anna
19 * Date: 29-Aug-2008
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) {
36 super(element);
37 myNewType = newType;
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;
44 } else {
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()))) {
54 continue constr;
57 foundMatchingConstructor = true;
60 if (!foundMatchingConstructor) {
61 myConflict = CONSTRUCTOR_MATCHING_SUPER_NOT_FOUND;
66 PsiType type = element.getType();
67 if (type == null) {
68 appendConflict("Type is unknown");
69 return;
70 } else {
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();
85 }, ", ");
86 appendConflict(conflict);
90 private void appendConflict(final String conflict) {
91 if (myConflict == null) {
92 myConflict = conflict;
93 } else {
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++) {
110 buf.append("[]");
112 buf.append(arrayInitializer.getText());
114 else {
115 final PsiExpression[] arrayDimensions = newExpression.getArrayDimensions();
116 if (arrayDimensions.length > 0) {
117 buf.append("[");
118 buf.append(StringUtil.join(arrayDimensions, new Function<PsiExpression, String>() {
119 public String fun(PsiExpression psiExpression) {
120 return psiExpression.getText();
122 }, "]["));
123 buf.append("]");
124 for (int i = 0; i < newExpressionType.getArrayDimensions() - arrayDimensions.length; i++) {
125 buf.append("[]");
127 } else {
128 final PsiExpressionList list = newExpression.getArgumentList();
129 if (list != null) {
130 buf.append(list.getText());
135 newExpression.replace(elementFactory.createExpressionFromText(buf.toString(), newExpression));
139 public String getConflictMessage() {
140 return myConflict;